11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
28 typedef struct declaration_specifiers_t declaration_specifiers_t;
29 struct declaration_specifiers_t {
30 source_position_t source_position;
31 unsigned char storage_class;
36 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
39 static token_t lookahead_buffer[MAX_LOOKAHEAD];
40 static int lookahead_bufpos;
41 static stack_entry_t *environment_stack = NULL;
42 static stack_entry_t *label_stack = NULL;
43 static context_t *global_context = NULL;
44 static context_t *context = NULL;
45 static declaration_t *last_declaration = NULL;
46 static declaration_t *current_function = NULL;
47 static struct obstack temp_obst;
48 static bool found_error;
50 static type_t *type_int = NULL;
51 static type_t *type_long_double = NULL;
52 static type_t *type_double = NULL;
53 static type_t *type_float = NULL;
54 static type_t *type_char = NULL;
55 static type_t *type_string = NULL;
56 static type_t *type_void = NULL;
57 static type_t *type_void_ptr = NULL;
59 type_t *type_size_t = NULL;
60 type_t *type_ptrdiff_t = NULL;
61 type_t *type_wchar_t = NULL;
62 type_t *type_wchar_t_ptr = NULL;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(void);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 #define STORAGE_CLASSES \
83 #define TYPE_QUALIFIERS \
89 #ifdef PROVIDE_COMPLEX
90 #define COMPLEX_SPECIFIERS \
92 #define IMAGINARY_SPECIFIERS \
95 #define COMPLEX_SPECIFIERS
96 #define IMAGINARY_SPECIFIERS
99 #define TYPE_SPECIFIERS \
117 #define DECLARATION_START \
122 #define TYPENAME_START \
126 static void *allocate_ast_zero(size_t size)
128 void *res = allocate_ast(size);
129 memset(res, 0, size);
133 static size_t get_statement_struct_size(statement_type_t type)
135 static const size_t sizes[] = {
136 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
137 [STATEMENT_RETURN] = sizeof(return_statement_t),
138 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
139 [STATEMENT_IF] = sizeof(if_statement_t),
140 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
141 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
142 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
143 [STATEMENT_BREAK] = sizeof(statement_base_t),
144 [STATEMENT_GOTO] = sizeof(goto_statement_t),
145 [STATEMENT_LABEL] = sizeof(label_statement_t),
146 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
147 [STATEMENT_WHILE] = sizeof(while_statement_t),
148 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
149 [STATEMENT_FOR] = sizeof(for_statement_t),
150 [STATEMENT_ASM] = sizeof(asm_statement_t)
152 assert(sizeof(sizes) / sizeof(sizes[0]) == STATEMENT_ASM + 1);
153 assert(type <= STATEMENT_ASM);
154 assert(sizes[type] != 0);
158 static statement_t *allocate_statement_zero(statement_type_t type)
160 size_t size = get_statement_struct_size(type);
161 statement_t *res = allocate_ast_zero(size);
163 res->base.type = type;
168 static size_t get_expression_struct_size(expression_type_t type)
170 static const size_t sizes[] = {
171 [EXPR_INVALID] = sizeof(expression_base_t),
172 [EXPR_REFERENCE] = sizeof(reference_expression_t),
173 [EXPR_CONST] = sizeof(const_expression_t),
174 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
175 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
176 [EXPR_CALL] = sizeof(call_expression_t),
177 [EXPR_UNARY] = sizeof(unary_expression_t),
178 [EXPR_BINARY] = sizeof(binary_expression_t),
179 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
180 [EXPR_SELECT] = sizeof(select_expression_t),
181 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
182 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
183 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
184 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
185 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
186 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
187 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
188 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
189 [EXPR_STATEMENT] = sizeof(statement_expression_t)
191 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
192 assert(type <= EXPR_STATEMENT);
193 assert(sizes[type] != 0);
197 static expression_t *allocate_expression_zero(expression_type_t type)
199 size_t size = get_expression_struct_size(type);
200 expression_t *res = allocate_ast_zero(size);
202 res->base.type = type;
206 static size_t get_type_struct_size(type_type_t type)
208 static const size_t sizes[] = {
209 [TYPE_ATOMIC] = sizeof(atomic_type_t),
210 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
211 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
212 [TYPE_ENUM] = sizeof(enum_type_t),
213 [TYPE_FUNCTION] = sizeof(function_type_t),
214 [TYPE_POINTER] = sizeof(pointer_type_t),
215 [TYPE_ARRAY] = sizeof(array_type_t),
216 [TYPE_BUILTIN] = sizeof(builtin_type_t),
217 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
218 [TYPE_TYPEOF] = sizeof(typeof_type_t),
220 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
221 assert(type <= TYPE_TYPEOF);
222 assert(sizes[type] != 0);
226 static type_t *allocate_type_zero(type_type_t type)
228 size_t size = get_type_struct_size(type);
229 type_t *res = obstack_alloc(type_obst, size);
230 memset(res, 0, size);
232 res->base.type = type;
236 static size_t get_initializer_size(initializer_type_t type)
238 static const size_t sizes[] = {
239 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
240 [INITIALIZER_STRING] = sizeof(initializer_string_t),
241 [INITIALIZER_LIST] = sizeof(initializer_list_t)
243 assert(type < INITIALIZER_COUNT);
244 assert(sizes[type] != 0);
248 static initializer_t *allocate_initializer(initializer_type_t type)
250 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
256 static void free_type(void *type)
258 obstack_free(type_obst, type);
262 * returns the top element of the environment stack
264 static size_t environment_top(void)
266 return ARR_LEN(environment_stack);
269 static size_t label_top(void)
271 return ARR_LEN(label_stack);
276 static inline void next_token(void)
278 token = lookahead_buffer[lookahead_bufpos];
279 lookahead_buffer[lookahead_bufpos] = lexer_token;
282 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
285 print_token(stderr, &token);
286 fprintf(stderr, "\n");
290 static inline const token_t *look_ahead(int num)
292 assert(num > 0 && num <= MAX_LOOKAHEAD);
293 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
294 return &lookahead_buffer[pos];
297 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
299 static void error(void)
302 #ifdef ABORT_ON_ERROR
307 static void parser_print_prefix_pos(const source_position_t source_position)
309 fputs(source_position.input_name, stderr);
311 fprintf(stderr, "%u", source_position.linenr);
315 static void parser_print_error_prefix_pos(
316 const source_position_t source_position)
318 parser_print_prefix_pos(source_position);
319 fputs("error: ", stderr);
323 static void parser_print_error_prefix(void)
325 parser_print_error_prefix_pos(token.source_position);
328 static void parse_error(const char *message)
330 parser_print_error_prefix();
331 fprintf(stderr, "parse error: %s\n", message);
334 static void parser_print_warning_prefix_pos(
335 const source_position_t source_position)
337 parser_print_prefix_pos(source_position);
338 fputs("warning: ", stderr);
341 static void parser_print_warning_prefix(void)
343 parser_print_warning_prefix_pos(token.source_position);
346 static void parse_warning_pos(const source_position_t source_position,
347 const char *const message)
349 parser_print_prefix_pos(source_position);
350 fprintf(stderr, "warning: %s\n", message);
353 static void parse_warning(const char *message)
355 parse_warning_pos(token.source_position, message);
358 static void parse_error_expected(const char *message, ...)
363 if(message != NULL) {
364 parser_print_error_prefix();
365 fprintf(stderr, "%s\n", message);
367 parser_print_error_prefix();
368 fputs("Parse error: got ", stderr);
369 print_token(stderr, &token);
370 fputs(", expected ", stderr);
372 va_start(args, message);
373 token_type_t token_type = va_arg(args, token_type_t);
374 while(token_type != 0) {
378 fprintf(stderr, ", ");
380 print_token_type(stderr, token_type);
381 token_type = va_arg(args, token_type_t);
384 fprintf(stderr, "\n");
387 static void print_type_quoted(type_t *type)
394 static void type_error(const char *msg, const source_position_t source_position,
397 parser_print_error_prefix_pos(source_position);
398 fprintf(stderr, "%s, but found type ", msg);
399 print_type_quoted(type);
403 static void type_error_incompatible(const char *msg,
404 const source_position_t source_position, type_t *type1, type_t *type2)
406 parser_print_error_prefix_pos(source_position);
407 fprintf(stderr, "%s, incompatible types: ", msg);
408 print_type_quoted(type1);
409 fprintf(stderr, " - ");
410 print_type_quoted(type2);
411 fprintf(stderr, ")\n");
414 static void eat_block(void)
416 if(token.type == '{')
419 while(token.type != '}') {
420 if(token.type == T_EOF)
422 if(token.type == '{') {
431 static void eat_statement(void)
433 while(token.type != ';') {
434 if(token.type == T_EOF)
436 if(token.type == '}')
438 if(token.type == '{') {
447 static void eat_brace(void)
449 if(token.type == '(')
452 while(token.type != ')') {
453 if(token.type == T_EOF)
455 if(token.type == ')' || token.type == ';' || token.type == '}') {
458 if(token.type == '(') {
462 if(token.type == '{') {
471 #define expect(expected) \
472 if(UNLIKELY(token.type != (expected))) { \
473 parse_error_expected(NULL, (expected), 0); \
479 #define expect_block(expected) \
480 if(UNLIKELY(token.type != (expected))) { \
481 parse_error_expected(NULL, (expected), 0); \
487 #define expect_void(expected) \
488 if(UNLIKELY(token.type != (expected))) { \
489 parse_error_expected(NULL, (expected), 0); \
495 static void set_context(context_t *new_context)
497 context = new_context;
499 last_declaration = new_context->declarations;
500 if(last_declaration != NULL) {
501 while(last_declaration->next != NULL) {
502 last_declaration = last_declaration->next;
508 * called when we find a 2nd declarator for an identifier we already have a
511 static bool is_compatible_declaration(declaration_t *declaration,
512 declaration_t *previous)
514 /* happens for K&R style function parameters */
515 if(previous->type == NULL) {
516 previous->type = declaration->type;
520 type_t *type1 = skip_typeref(declaration->type);
521 type_t *type2 = skip_typeref(previous->type);
523 return types_compatible(type1, type2);
526 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
528 declaration_t *declaration = symbol->declaration;
529 for( ; declaration != NULL; declaration = declaration->symbol_next) {
530 if(declaration->namespc == namespc)
537 static const char *get_namespace_prefix(namespace_t namespc)
540 case NAMESPACE_NORMAL:
542 case NAMESPACE_UNION:
544 case NAMESPACE_STRUCT:
548 case NAMESPACE_LABEL:
551 panic("invalid namespace found");
555 * pushs an environment_entry on the environment stack and links the
556 * corresponding symbol to the new entry
558 static declaration_t *stack_push(stack_entry_t **stack_ptr,
559 declaration_t *declaration,
560 context_t *parent_context)
562 symbol_t *symbol = declaration->symbol;
563 namespace_t namespc = (namespace_t)declaration->namespc;
565 /* a declaration should be only pushed once */
566 declaration->parent_context = parent_context;
568 declaration_t *previous_declaration = get_declaration(symbol, namespc);
569 assert(declaration != previous_declaration);
570 if(previous_declaration != NULL
571 && previous_declaration->parent_context == context) {
572 if(!is_compatible_declaration(declaration, previous_declaration)) {
573 parser_print_error_prefix_pos(declaration->source_position);
574 fprintf(stderr, "definition of symbol '%s%s' with type ",
575 get_namespace_prefix(namespc), symbol->string);
576 print_type_quoted(declaration->type);
578 parser_print_error_prefix_pos(
579 previous_declaration->source_position);
580 fprintf(stderr, "is incompatible with previous declaration "
582 print_type_quoted(previous_declaration->type);
585 unsigned old_storage_class = previous_declaration->storage_class;
586 unsigned new_storage_class = declaration->storage_class;
587 if (current_function == NULL) {
588 if (old_storage_class != STORAGE_CLASS_STATIC &&
589 new_storage_class == STORAGE_CLASS_STATIC) {
590 parser_print_error_prefix_pos(declaration->source_position);
592 "static declaration of '%s' follows non-static declaration\n",
594 parser_print_error_prefix_pos(previous_declaration->source_position);
595 fprintf(stderr, "previous declaration of '%s' was here\n",
598 if (old_storage_class == STORAGE_CLASS_EXTERN) {
599 if (new_storage_class == STORAGE_CLASS_NONE) {
600 previous_declaration->storage_class = STORAGE_CLASS_NONE;
603 parser_print_warning_prefix_pos(declaration->source_position);
604 fprintf(stderr, "redundant declaration for '%s'\n",
606 parser_print_warning_prefix_pos(previous_declaration->source_position);
607 fprintf(stderr, "previous declaration of '%s' was here\n",
612 if (old_storage_class == STORAGE_CLASS_EXTERN &&
613 new_storage_class == STORAGE_CLASS_EXTERN) {
614 parser_print_warning_prefix_pos(declaration->source_position);
615 fprintf(stderr, "redundant extern declaration for '%s'\n",
617 parser_print_warning_prefix_pos(previous_declaration->source_position);
618 fprintf(stderr, "previous declaration of '%s' was here\n",
621 parser_print_error_prefix_pos(declaration->source_position);
622 if (old_storage_class == new_storage_class) {
623 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
625 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
627 parser_print_error_prefix_pos(previous_declaration->source_position);
628 fprintf(stderr, "previous declaration of '%s' was here\n",
633 return previous_declaration;
636 /* remember old declaration */
638 entry.symbol = symbol;
639 entry.old_declaration = symbol->declaration;
640 entry.namespc = (unsigned short) namespc;
641 ARR_APP1(stack_entry_t, *stack_ptr, entry);
643 /* replace/add declaration into declaration list of the symbol */
644 if(symbol->declaration == NULL) {
645 symbol->declaration = declaration;
647 declaration_t *iter_last = NULL;
648 declaration_t *iter = symbol->declaration;
649 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
650 /* replace an entry? */
651 if(iter->namespc == namespc) {
652 if(iter_last == NULL) {
653 symbol->declaration = declaration;
655 iter_last->symbol_next = declaration;
657 declaration->symbol_next = iter->symbol_next;
662 assert(iter_last->symbol_next == NULL);
663 iter_last->symbol_next = declaration;
670 static declaration_t *environment_push(declaration_t *declaration)
672 assert(declaration->source_position.input_name != NULL);
673 return stack_push(&environment_stack, declaration, context);
676 static declaration_t *label_push(declaration_t *declaration)
678 return stack_push(&label_stack, declaration, ¤t_function->context);
682 * pops symbols from the environment stack until @p new_top is the top element
684 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
686 stack_entry_t *stack = *stack_ptr;
687 size_t top = ARR_LEN(stack);
690 assert(new_top <= top);
694 for(i = top; i > new_top; --i) {
695 stack_entry_t *entry = &stack[i - 1];
697 declaration_t *old_declaration = entry->old_declaration;
698 symbol_t *symbol = entry->symbol;
699 namespace_t namespc = (namespace_t)entry->namespc;
701 /* replace/remove declaration */
702 declaration_t *declaration = symbol->declaration;
703 assert(declaration != NULL);
704 if(declaration->namespc == namespc) {
705 if(old_declaration == NULL) {
706 symbol->declaration = declaration->symbol_next;
708 symbol->declaration = old_declaration;
711 declaration_t *iter_last = declaration;
712 declaration_t *iter = declaration->symbol_next;
713 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
714 /* replace an entry? */
715 if(iter->namespc == namespc) {
716 assert(iter_last != NULL);
717 iter_last->symbol_next = old_declaration;
718 old_declaration->symbol_next = iter->symbol_next;
722 assert(iter != NULL);
726 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
729 static void environment_pop_to(size_t new_top)
731 stack_pop_to(&environment_stack, new_top);
734 static void label_pop_to(size_t new_top)
736 stack_pop_to(&label_stack, new_top);
740 static int get_rank(const type_t *type)
742 assert(!is_typeref(type));
743 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
744 * and esp. footnote 108). However we can't fold constants (yet), so we
745 * can't decide wether unsigned int is possible, while int always works.
746 * (unsigned int would be preferable when possible... for stuff like
747 * struct { enum { ... } bla : 4; } ) */
748 if(type->type == TYPE_ENUM)
749 return ATOMIC_TYPE_INT;
751 assert(type->type == TYPE_ATOMIC);
752 const atomic_type_t *atomic_type = &type->atomic;
753 atomic_type_type_t atype = atomic_type->atype;
757 static type_t *promote_integer(type_t *type)
759 if(get_rank(type) < ATOMIC_TYPE_INT)
765 static expression_t *create_cast_expression(expression_t *expression,
768 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
770 cast->unary.type = UNEXPR_CAST_IMPLICIT;
771 cast->unary.value = expression;
772 cast->base.datatype = dest_type;
777 static bool is_null_pointer_constant(const expression_t *expression)
779 /* skip void* cast */
780 if(expression->type == EXPR_UNARY) {
781 const unary_expression_t *unary = &expression->unary;
782 if(unary->type == UNEXPR_CAST
783 && expression->base.datatype == type_void_ptr) {
784 expression = unary->value;
788 /* TODO: not correct yet, should be any constant integer expression
789 * which evaluates to 0 */
790 if (expression->type != EXPR_CONST)
793 type_t *const type = skip_typeref(expression->base.datatype);
794 if (!is_type_integer(type))
797 return expression->conste.v.int_value == 0;
800 static expression_t *create_implicit_cast(expression_t *expression,
803 type_t *source_type = expression->base.datatype;
805 if(source_type == NULL)
808 source_type = skip_typeref(source_type);
809 dest_type = skip_typeref(dest_type);
811 if(source_type == dest_type)
814 switch (dest_type->type) {
816 /* TODO warning for implicitly converting to enum */
818 if (source_type->type != TYPE_ATOMIC &&
819 source_type->type != TYPE_ENUM) {
820 panic("casting of non-atomic types not implemented yet");
823 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
824 type_error_incompatible("can't cast types",
825 expression->base.source_position, source_type,
830 return create_cast_expression(expression, dest_type);
833 switch (source_type->type) {
835 if (is_null_pointer_constant(expression)) {
836 return create_cast_expression(expression, dest_type);
841 if (pointers_compatible(source_type, dest_type)) {
842 return create_cast_expression(expression, dest_type);
847 array_type_t *array_type = &source_type->array;
848 pointer_type_t *pointer_type = &dest_type->pointer;
849 if (types_compatible(array_type->element_type,
850 pointer_type->points_to)) {
851 return create_cast_expression(expression, dest_type);
857 panic("casting of non-atomic types not implemented yet");
860 type_error_incompatible("can't implicitly cast types",
861 expression->base.source_position, source_type, dest_type);
865 panic("casting of non-atomic types not implemented yet");
869 /** Implements the rules from § 6.5.16.1 */
870 static void semantic_assign(type_t *orig_type_left, expression_t **right,
873 type_t *orig_type_right = (*right)->base.datatype;
875 if(orig_type_right == NULL)
878 type_t *const type_left = skip_typeref(orig_type_left);
879 type_t *const type_right = skip_typeref(orig_type_right);
881 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
882 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
883 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
884 && is_type_pointer(type_right))) {
885 *right = create_implicit_cast(*right, type_left);
889 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
890 pointer_type_t *pointer_type_left = &type_left->pointer;
891 pointer_type_t *pointer_type_right = &type_right->pointer;
892 type_t *points_to_left = pointer_type_left->points_to;
893 type_t *points_to_right = pointer_type_right->points_to;
895 points_to_left = skip_typeref(points_to_left);
896 points_to_right = skip_typeref(points_to_right);
898 /* the left type has all qualifiers from the right type */
899 unsigned missing_qualifiers
900 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
901 if(missing_qualifiers != 0) {
902 parser_print_error_prefix();
903 fprintf(stderr, "destination type ");
904 print_type_quoted(type_left);
905 fprintf(stderr, " in %s from type ", context);
906 print_type_quoted(type_right);
907 fprintf(stderr, " lacks qualifiers '");
908 print_type_qualifiers(missing_qualifiers);
909 fprintf(stderr, "' in pointed-to type\n");
913 points_to_left = get_unqualified_type(points_to_left);
914 points_to_right = get_unqualified_type(points_to_right);
916 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
917 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
918 && !types_compatible(points_to_left, points_to_right)) {
919 goto incompatible_assign_types;
922 *right = create_implicit_cast(*right, type_left);
926 if (is_type_compound(type_left)
927 && types_compatible(type_left, type_right)) {
928 *right = create_implicit_cast(*right, type_left);
932 incompatible_assign_types:
933 /* TODO: improve error message */
934 parser_print_error_prefix();
935 fprintf(stderr, "incompatible types in %s\n", context);
936 parser_print_error_prefix();
937 print_type_quoted(orig_type_left);
938 fputs(" <- ", stderr);
939 print_type_quoted(orig_type_right);
943 static expression_t *parse_constant_expression(void)
945 /* start parsing at precedence 7 (conditional expression) */
946 return parse_sub_expression(7);
949 static expression_t *parse_assignment_expression(void)
951 /* start parsing at precedence 2 (assignment expression) */
952 return parse_sub_expression(2);
955 static type_t *make_global_typedef(const char *name, type_t *type)
957 symbol_t *symbol = symbol_table_insert(name);
959 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
960 declaration->namespc = NAMESPACE_NORMAL;
961 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
962 declaration->type = type;
963 declaration->symbol = symbol;
964 declaration->source_position = builtin_source_position;
966 record_declaration(declaration);
968 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
969 typedef_type->typedeft.declaration = declaration;
974 static const char *parse_string_literals(void)
976 assert(token.type == T_STRING_LITERAL);
977 const char *result = token.v.string;
981 while(token.type == T_STRING_LITERAL) {
982 result = concat_strings(result, token.v.string);
989 static void parse_attributes(void)
993 case T___attribute__: {
1001 parse_error("EOF while parsing attribute");
1020 if(token.type != T_STRING_LITERAL) {
1021 parse_error_expected("while parsing assembler attribute",
1026 parse_string_literals();
1031 goto attributes_finished;
1035 attributes_finished:
1040 static designator_t *parse_designation(void)
1042 if(token.type != '[' && token.type != '.')
1045 designator_t *result = NULL;
1046 designator_t *last = NULL;
1049 designator_t *designator;
1050 switch(token.type) {
1052 designator = allocate_ast_zero(sizeof(designator[0]));
1054 designator->array_access = parse_constant_expression();
1058 designator = allocate_ast_zero(sizeof(designator[0]));
1060 if(token.type != T_IDENTIFIER) {
1061 parse_error_expected("while parsing designator",
1065 designator->symbol = token.v.symbol;
1073 assert(designator != NULL);
1075 last->next = designator;
1077 result = designator;
1084 static initializer_t *initializer_from_string(array_type_t *type,
1087 /* TODO: check len vs. size of array type */
1090 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1091 initializer->string.string = string;
1096 static initializer_t *initializer_from_expression(type_t *type,
1097 expression_t *expression)
1099 /* TODO check that expression is a constant expression */
1101 /* § 6.7.8.14/15 char array may be initialized by string literals */
1102 if(is_type_array(type) && expression->type == EXPR_STRING_LITERAL) {
1103 array_type_t *array_type = &type->array;
1104 type_t *element_type = array_type->element_type;
1106 if(element_type->type == TYPE_ATOMIC) {
1107 atomic_type_t *atomic_type = &element_type->atomic;
1108 atomic_type_type_t atype = atomic_type->atype;
1110 /* TODO handle wide strings */
1111 if(atype == ATOMIC_TYPE_CHAR
1112 || atype == ATOMIC_TYPE_SCHAR
1113 || atype == ATOMIC_TYPE_UCHAR) {
1115 string_literal_expression_t *literal = &expression->string;
1116 return initializer_from_string(array_type, literal->value);
1121 type_t *expression_type = skip_typeref(expression->base.datatype);
1122 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1123 semantic_assign(type, &expression, "initializer");
1125 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1126 result->value.value = expression;
1134 static initializer_t *parse_sub_initializer(type_t *type,
1135 expression_t *expression,
1136 type_t *expression_type);
1138 static initializer_t *parse_sub_initializer_elem(type_t *type)
1140 if(token.type == '{') {
1141 return parse_sub_initializer(type, NULL, NULL);
1144 expression_t *expression = parse_assignment_expression();
1145 type_t *expression_type = skip_typeref(expression->base.datatype);
1147 return parse_sub_initializer(type, expression, expression_type);
1150 static bool had_initializer_brace_warning;
1152 static initializer_t *parse_sub_initializer(type_t *type,
1153 expression_t *expression,
1154 type_t *expression_type)
1156 if(is_type_scalar(type)) {
1157 /* there might be extra {} hierarchies */
1158 if(token.type == '{') {
1160 if(!had_initializer_brace_warning) {
1161 parse_warning("braces around scalar initializer");
1162 had_initializer_brace_warning = true;
1164 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1165 if(token.type == ',') {
1167 /* TODO: warn about excessive elements */
1173 if(expression == NULL) {
1174 expression = parse_assignment_expression();
1176 return initializer_from_expression(type, expression);
1179 /* does the expression match the currently looked at object to initalize */
1180 if(expression != NULL) {
1181 initializer_t *result = initializer_from_expression(type, expression);
1186 bool read_paren = false;
1187 if(token.type == '{') {
1192 /* descend into subtype */
1193 initializer_t *result = NULL;
1194 initializer_t **elems;
1195 if(is_type_array(type)) {
1196 array_type_t *array_type = &type->array;
1197 type_t *element_type = array_type->element_type;
1198 element_type = skip_typeref(element_type);
1201 had_initializer_brace_warning = false;
1202 if(expression == NULL) {
1203 sub = parse_sub_initializer_elem(element_type);
1205 sub = parse_sub_initializer(element_type, expression,
1209 /* didn't match the subtypes -> try the parent type */
1211 assert(!read_paren);
1215 elems = NEW_ARR_F(initializer_t*, 0);
1216 ARR_APP1(initializer_t*, elems, sub);
1219 if(token.type == '}')
1222 if(token.type == '}')
1225 sub = parse_sub_initializer_elem(element_type);
1227 /* TODO error, do nicer cleanup */
1228 parse_error("member initializer didn't match");
1232 ARR_APP1(initializer_t*, elems, sub);
1235 assert(is_type_compound(type));
1236 compound_type_t *compound_type = &type->compound;
1237 context_t *context = &compound_type->declaration->context;
1239 declaration_t *first = context->declarations;
1242 type_t *first_type = first->type;
1243 first_type = skip_typeref(first_type);
1246 had_initializer_brace_warning = false;
1247 if(expression == NULL) {
1248 sub = parse_sub_initializer_elem(first_type);
1250 sub = parse_sub_initializer(first_type, expression,expression_type);
1253 /* didn't match the subtypes -> try our parent type */
1255 assert(!read_paren);
1259 elems = NEW_ARR_F(initializer_t*, 0);
1260 ARR_APP1(initializer_t*, elems, sub);
1262 declaration_t *iter = first->next;
1263 for( ; iter != NULL; iter = iter->next) {
1264 if(iter->symbol == NULL)
1266 if(iter->namespc != NAMESPACE_NORMAL)
1269 if(token.type == '}')
1272 if(token.type == '}')
1275 type_t *iter_type = iter->type;
1276 iter_type = skip_typeref(iter_type);
1278 sub = parse_sub_initializer_elem(iter_type);
1280 /* TODO error, do nicer cleanup*/
1281 parse_error("member initializer didn't match");
1285 ARR_APP1(initializer_t*, elems, sub);
1289 int len = ARR_LEN(elems);
1290 size_t elems_size = sizeof(initializer_t*) * len;
1292 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1294 init->initializer.type = INITIALIZER_LIST;
1296 memcpy(init->initializers, elems, elems_size);
1299 result = (initializer_t*) init;
1302 if(token.type == ',')
1309 static initializer_t *parse_initializer(type_t *type)
1311 initializer_t *result;
1313 type = skip_typeref(type);
1315 if(token.type != '{') {
1316 expression_t *expression = parse_assignment_expression();
1317 initializer_t *initializer = initializer_from_expression(type, expression);
1318 if(initializer == NULL) {
1319 parser_print_error_prefix();
1320 fprintf(stderr, "initializer expression '");
1321 print_expression(expression);
1322 fprintf(stderr, "', type ");
1323 print_type_quoted(expression->base.datatype);
1324 fprintf(stderr, " is incompatible with type ");
1325 print_type_quoted(type);
1326 fprintf(stderr, "\n");
1331 if(is_type_scalar(type)) {
1335 expression_t *expression = parse_assignment_expression();
1336 result = initializer_from_expression(type, expression);
1338 if(token.type == ',')
1344 result = parse_sub_initializer(type, NULL, NULL);
1352 static declaration_t *parse_compound_type_specifier(bool is_struct)
1360 symbol_t *symbol = NULL;
1361 declaration_t *declaration = NULL;
1363 if (token.type == T___attribute__) {
1368 if(token.type == T_IDENTIFIER) {
1369 symbol = token.v.symbol;
1373 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1375 declaration = get_declaration(symbol, NAMESPACE_UNION);
1377 } else if(token.type != '{') {
1379 parse_error_expected("while parsing struct type specifier",
1380 T_IDENTIFIER, '{', 0);
1382 parse_error_expected("while parsing union type specifier",
1383 T_IDENTIFIER, '{', 0);
1389 if(declaration == NULL) {
1390 declaration = allocate_ast_zero(sizeof(declaration[0]));
1393 declaration->namespc = NAMESPACE_STRUCT;
1395 declaration->namespc = NAMESPACE_UNION;
1397 declaration->source_position = token.source_position;
1398 declaration->symbol = symbol;
1399 record_declaration(declaration);
1402 if(token.type == '{') {
1403 if(declaration->init.is_defined) {
1404 assert(symbol != NULL);
1405 parser_print_error_prefix();
1406 fprintf(stderr, "multiple definition of %s %s\n",
1407 is_struct ? "struct" : "union", symbol->string);
1408 declaration->context.declarations = NULL;
1410 declaration->init.is_defined = true;
1412 int top = environment_top();
1413 context_t *last_context = context;
1414 set_context(&declaration->context);
1416 parse_compound_type_entries();
1419 assert(context == &declaration->context);
1420 set_context(last_context);
1421 environment_pop_to(top);
1427 static void parse_enum_entries(enum_type_t *const enum_type)
1431 if(token.type == '}') {
1433 parse_error("empty enum not allowed");
1438 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1440 if(token.type != T_IDENTIFIER) {
1441 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1445 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1446 entry->type = (type_t*) enum_type;
1447 entry->symbol = token.v.symbol;
1448 entry->source_position = token.source_position;
1451 if(token.type == '=') {
1453 entry->init.enum_value = parse_constant_expression();
1458 record_declaration(entry);
1460 if(token.type != ',')
1463 } while(token.type != '}');
1468 static type_t *parse_enum_specifier(void)
1472 declaration_t *declaration;
1475 if(token.type == T_IDENTIFIER) {
1476 symbol = token.v.symbol;
1479 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1480 } else if(token.type != '{') {
1481 parse_error_expected("while parsing enum type specifier",
1482 T_IDENTIFIER, '{', 0);
1489 if(declaration == NULL) {
1490 declaration = allocate_ast_zero(sizeof(declaration[0]));
1492 declaration->namespc = NAMESPACE_ENUM;
1493 declaration->source_position = token.source_position;
1494 declaration->symbol = symbol;
1497 type_t *const type = allocate_type_zero(TYPE_ENUM);
1498 type->enumt.declaration = declaration;
1500 if(token.type == '{') {
1501 if(declaration->init.is_defined) {
1502 parser_print_error_prefix();
1503 fprintf(stderr, "multiple definitions of enum %s\n",
1506 record_declaration(declaration);
1507 declaration->init.is_defined = 1;
1509 parse_enum_entries(&type->enumt);
1517 * if a symbol is a typedef to another type, return true
1519 static bool is_typedef_symbol(symbol_t *symbol)
1521 const declaration_t *const declaration =
1522 get_declaration(symbol, NAMESPACE_NORMAL);
1524 declaration != NULL &&
1525 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1528 static type_t *parse_typeof(void)
1536 expression_t *expression = NULL;
1539 switch(token.type) {
1540 case T___extension__:
1541 /* this can be a prefix to a typename or an expression */
1542 /* we simply eat it now. */
1545 } while(token.type == T___extension__);
1549 if(is_typedef_symbol(token.v.symbol)) {
1550 type = parse_typename();
1552 expression = parse_expression();
1553 type = expression->base.datatype;
1558 type = parse_typename();
1562 expression = parse_expression();
1563 type = expression->base.datatype;
1569 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1570 typeof_type->typeoft.expression = expression;
1571 typeof_type->typeoft.typeof_type = type;
1577 SPECIFIER_SIGNED = 1 << 0,
1578 SPECIFIER_UNSIGNED = 1 << 1,
1579 SPECIFIER_LONG = 1 << 2,
1580 SPECIFIER_INT = 1 << 3,
1581 SPECIFIER_DOUBLE = 1 << 4,
1582 SPECIFIER_CHAR = 1 << 5,
1583 SPECIFIER_SHORT = 1 << 6,
1584 SPECIFIER_LONG_LONG = 1 << 7,
1585 SPECIFIER_FLOAT = 1 << 8,
1586 SPECIFIER_BOOL = 1 << 9,
1587 SPECIFIER_VOID = 1 << 10,
1588 #ifdef PROVIDE_COMPLEX
1589 SPECIFIER_COMPLEX = 1 << 11,
1590 SPECIFIER_IMAGINARY = 1 << 12,
1594 static type_t *create_builtin_type(symbol_t *symbol)
1596 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1597 type->builtin.symbol = symbol;
1599 type->builtin.real_type = type_int;
1604 static type_t *get_typedef_type(symbol_t *symbol)
1606 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1607 if(declaration == NULL
1608 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1611 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1612 type->typedeft.declaration = declaration;
1617 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1619 type_t *type = NULL;
1620 unsigned type_qualifiers = 0;
1621 unsigned type_specifiers = 0;
1624 specifiers->source_position = token.source_position;
1627 switch(token.type) {
1630 #define MATCH_STORAGE_CLASS(token, class) \
1632 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1633 parse_error("multiple storage classes in declaration " \
1636 specifiers->storage_class = class; \
1640 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1641 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1642 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1643 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1644 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1647 switch (specifiers->storage_class) {
1648 case STORAGE_CLASS_NONE:
1649 specifiers->storage_class = STORAGE_CLASS_THREAD;
1652 case STORAGE_CLASS_EXTERN:
1653 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1656 case STORAGE_CLASS_STATIC:
1657 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1661 parse_error("multiple storage classes in declaration specifiers");
1667 /* type qualifiers */
1668 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1670 type_qualifiers |= qualifier; \
1674 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1675 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1676 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1678 case T___extension__:
1683 /* type specifiers */
1684 #define MATCH_SPECIFIER(token, specifier, name) \
1687 if(type_specifiers & specifier) { \
1688 parse_error("multiple " name " type specifiers given"); \
1690 type_specifiers |= specifier; \
1694 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1695 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1696 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1697 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1698 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1699 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1700 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1701 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1702 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1703 #ifdef PROVIDE_COMPLEX
1704 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1705 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1709 specifiers->is_inline = true;
1714 if(type_specifiers & SPECIFIER_LONG_LONG) {
1715 parse_error("multiple type specifiers given");
1716 } else if(type_specifiers & SPECIFIER_LONG) {
1717 type_specifiers |= SPECIFIER_LONG_LONG;
1719 type_specifiers |= SPECIFIER_LONG;
1723 /* TODO: if type != NULL for the following rules should issue
1726 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1728 type->compound.declaration = parse_compound_type_specifier(true);
1732 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1734 type->compound.declaration = parse_compound_type_specifier(false);
1738 type = parse_enum_specifier();
1741 type = parse_typeof();
1743 case T___builtin_va_list:
1744 type = create_builtin_type(token.v.symbol);
1748 case T___attribute__:
1753 case T_IDENTIFIER: {
1754 type_t *typedef_type = get_typedef_type(token.v.symbol);
1756 if(typedef_type == NULL)
1757 goto finish_specifiers;
1760 type = typedef_type;
1764 /* function specifier */
1766 goto finish_specifiers;
1773 atomic_type_type_t atomic_type;
1775 /* match valid basic types */
1776 switch(type_specifiers) {
1777 case SPECIFIER_VOID:
1778 atomic_type = ATOMIC_TYPE_VOID;
1780 case SPECIFIER_CHAR:
1781 atomic_type = ATOMIC_TYPE_CHAR;
1783 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1784 atomic_type = ATOMIC_TYPE_SCHAR;
1786 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1787 atomic_type = ATOMIC_TYPE_UCHAR;
1789 case SPECIFIER_SHORT:
1790 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1791 case SPECIFIER_SHORT | SPECIFIER_INT:
1792 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1793 atomic_type = ATOMIC_TYPE_SHORT;
1795 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1796 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1797 atomic_type = ATOMIC_TYPE_USHORT;
1800 case SPECIFIER_SIGNED:
1801 case SPECIFIER_SIGNED | SPECIFIER_INT:
1802 atomic_type = ATOMIC_TYPE_INT;
1804 case SPECIFIER_UNSIGNED:
1805 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1806 atomic_type = ATOMIC_TYPE_UINT;
1808 case SPECIFIER_LONG:
1809 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1810 case SPECIFIER_LONG | SPECIFIER_INT:
1811 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1812 atomic_type = ATOMIC_TYPE_LONG;
1814 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1815 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1816 atomic_type = ATOMIC_TYPE_ULONG;
1818 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1819 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1820 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1821 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1823 atomic_type = ATOMIC_TYPE_LONGLONG;
1825 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1826 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1828 atomic_type = ATOMIC_TYPE_ULONGLONG;
1830 case SPECIFIER_FLOAT:
1831 atomic_type = ATOMIC_TYPE_FLOAT;
1833 case SPECIFIER_DOUBLE:
1834 atomic_type = ATOMIC_TYPE_DOUBLE;
1836 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1837 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1839 case SPECIFIER_BOOL:
1840 atomic_type = ATOMIC_TYPE_BOOL;
1842 #ifdef PROVIDE_COMPLEX
1843 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1844 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1846 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1847 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1849 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1850 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1852 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1853 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1855 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1856 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1858 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1859 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1863 /* invalid specifier combination, give an error message */
1864 if(type_specifiers == 0) {
1866 parse_warning("no type specifiers in declaration, using int");
1867 atomic_type = ATOMIC_TYPE_INT;
1870 parse_error("no type specifiers given in declaration");
1872 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1873 (type_specifiers & SPECIFIER_UNSIGNED)) {
1874 parse_error("signed and unsigned specifiers gives");
1875 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1876 parse_error("only integer types can be signed or unsigned");
1878 parse_error("multiple datatypes in declaration");
1880 atomic_type = ATOMIC_TYPE_INVALID;
1883 type = allocate_type_zero(TYPE_ATOMIC);
1884 type->atomic.atype = atomic_type;
1887 if(type_specifiers != 0) {
1888 parse_error("multiple datatypes in declaration");
1892 type->base.qualifiers = type_qualifiers;
1894 type_t *result = typehash_insert(type);
1895 if(newtype && result != type) {
1899 specifiers->type = result;
1902 static type_qualifiers_t parse_type_qualifiers(void)
1904 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1907 switch(token.type) {
1908 /* type qualifiers */
1909 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1910 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1911 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1914 return type_qualifiers;
1919 static declaration_t *parse_identifier_list(void)
1921 declaration_t *declarations = NULL;
1922 declaration_t *last_declaration = NULL;
1924 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1926 declaration->source_position = token.source_position;
1927 declaration->symbol = token.v.symbol;
1930 if(last_declaration != NULL) {
1931 last_declaration->next = declaration;
1933 declarations = declaration;
1935 last_declaration = declaration;
1937 if(token.type != ',')
1940 } while(token.type == T_IDENTIFIER);
1942 return declarations;
1945 static void semantic_parameter(declaration_t *declaration)
1947 /* TODO: improve error messages */
1949 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1950 parse_error("typedef not allowed in parameter list");
1951 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1952 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1953 parse_error("parameter may only have none or register storage class");
1956 type_t *orig_type = declaration->type;
1957 if(orig_type == NULL)
1959 type_t *type = skip_typeref(orig_type);
1961 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1962 * into a pointer. § 6.7.5.3 (7) */
1963 if (is_type_array(type)) {
1964 const array_type_t *arr_type = &type->array;
1965 type_t *element_type = arr_type->element_type;
1967 type = make_pointer_type(element_type, type->base.qualifiers);
1969 declaration->type = type;
1972 if(is_type_incomplete(type)) {
1973 parser_print_error_prefix();
1974 fprintf(stderr, "incomplete type (");
1975 print_type_quoted(orig_type);
1976 fprintf(stderr, ") not allowed for parameter '%s'\n",
1977 declaration->symbol->string);
1981 static declaration_t *parse_parameter(void)
1983 declaration_specifiers_t specifiers;
1984 memset(&specifiers, 0, sizeof(specifiers));
1986 parse_declaration_specifiers(&specifiers);
1988 declaration_t *declaration = parse_declarator(&specifiers, true);
1990 semantic_parameter(declaration);
1995 static declaration_t *parse_parameters(function_type_t *type)
1997 if(token.type == T_IDENTIFIER) {
1998 symbol_t *symbol = token.v.symbol;
1999 if(!is_typedef_symbol(symbol)) {
2000 type->kr_style_parameters = true;
2001 return parse_identifier_list();
2005 if(token.type == ')') {
2006 type->unspecified_parameters = 1;
2009 if(token.type == T_void && look_ahead(1)->type == ')') {
2014 declaration_t *declarations = NULL;
2015 declaration_t *declaration;
2016 declaration_t *last_declaration = NULL;
2017 function_parameter_t *parameter;
2018 function_parameter_t *last_parameter = NULL;
2021 switch(token.type) {
2025 return declarations;
2028 case T___extension__:
2030 declaration = parse_parameter();
2032 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2033 memset(parameter, 0, sizeof(parameter[0]));
2034 parameter->type = declaration->type;
2036 if(last_parameter != NULL) {
2037 last_declaration->next = declaration;
2038 last_parameter->next = parameter;
2040 type->parameters = parameter;
2041 declarations = declaration;
2043 last_parameter = parameter;
2044 last_declaration = declaration;
2048 return declarations;
2050 if(token.type != ',')
2051 return declarations;
2061 } construct_type_type_t;
2063 typedef struct construct_type_t construct_type_t;
2064 struct construct_type_t {
2065 construct_type_type_t type;
2066 construct_type_t *next;
2069 typedef struct parsed_pointer_t parsed_pointer_t;
2070 struct parsed_pointer_t {
2071 construct_type_t construct_type;
2072 type_qualifiers_t type_qualifiers;
2075 typedef struct construct_function_type_t construct_function_type_t;
2076 struct construct_function_type_t {
2077 construct_type_t construct_type;
2078 type_t *function_type;
2081 typedef struct parsed_array_t parsed_array_t;
2082 struct parsed_array_t {
2083 construct_type_t construct_type;
2084 type_qualifiers_t type_qualifiers;
2090 typedef struct construct_base_type_t construct_base_type_t;
2091 struct construct_base_type_t {
2092 construct_type_t construct_type;
2096 static construct_type_t *parse_pointer_declarator(void)
2100 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2101 memset(pointer, 0, sizeof(pointer[0]));
2102 pointer->construct_type.type = CONSTRUCT_POINTER;
2103 pointer->type_qualifiers = parse_type_qualifiers();
2105 return (construct_type_t*) pointer;
2108 static construct_type_t *parse_array_declarator(void)
2112 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2113 memset(array, 0, sizeof(array[0]));
2114 array->construct_type.type = CONSTRUCT_ARRAY;
2116 if(token.type == T_static) {
2117 array->is_static = true;
2121 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2122 if(type_qualifiers != 0) {
2123 if(token.type == T_static) {
2124 array->is_static = true;
2128 array->type_qualifiers = type_qualifiers;
2130 if(token.type == '*' && look_ahead(1)->type == ']') {
2131 array->is_variable = true;
2133 } else if(token.type != ']') {
2134 array->size = parse_assignment_expression();
2139 return (construct_type_t*) array;
2142 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2146 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2148 declaration_t *parameters = parse_parameters(&type->function);
2149 if(declaration != NULL) {
2150 declaration->context.declarations = parameters;
2153 construct_function_type_t *construct_function_type =
2154 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2155 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2156 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2157 construct_function_type->function_type = type;
2161 return (construct_type_t*) construct_function_type;
2164 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2165 bool may_be_abstract)
2167 /* construct a single linked list of construct_type_t's which describe
2168 * how to construct the final declarator type */
2169 construct_type_t *first = NULL;
2170 construct_type_t *last = NULL;
2173 while(token.type == '*') {
2174 construct_type_t *type = parse_pointer_declarator();
2185 /* TODO: find out if this is correct */
2188 construct_type_t *inner_types = NULL;
2190 switch(token.type) {
2192 if(declaration == NULL) {
2193 parse_error("no identifier expected in typename");
2195 declaration->symbol = token.v.symbol;
2196 declaration->source_position = token.source_position;
2202 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2208 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2209 /* avoid a loop in the outermost scope, because eat_statement doesn't
2211 if(token.type == '}' && current_function == NULL) {
2219 construct_type_t *p = last;
2222 construct_type_t *type;
2223 switch(token.type) {
2225 type = parse_function_declarator(declaration);
2228 type = parse_array_declarator();
2231 goto declarator_finished;
2234 /* insert in the middle of the list (behind p) */
2236 type->next = p->next;
2247 declarator_finished:
2250 /* append inner_types at the end of the list, we don't to set last anymore
2251 * as it's not needed anymore */
2253 assert(first == NULL);
2254 first = inner_types;
2256 last->next = inner_types;
2262 static type_t *construct_declarator_type(construct_type_t *construct_list,
2265 construct_type_t *iter = construct_list;
2266 for( ; iter != NULL; iter = iter->next) {
2267 switch(iter->type) {
2268 case CONSTRUCT_INVALID:
2269 panic("invalid type construction found");
2270 case CONSTRUCT_FUNCTION: {
2271 construct_function_type_t *construct_function_type
2272 = (construct_function_type_t*) iter;
2274 type_t *function_type = construct_function_type->function_type;
2276 function_type->function.return_type = type;
2278 type = function_type;
2282 case CONSTRUCT_POINTER: {
2283 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2284 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2285 pointer_type->pointer.points_to = type;
2286 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2288 type = pointer_type;
2292 case CONSTRUCT_ARRAY: {
2293 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2294 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2296 array_type->base.qualifiers = parsed_array->type_qualifiers;
2297 array_type->array.element_type = type;
2298 array_type->array.is_static = parsed_array->is_static;
2299 array_type->array.is_variable = parsed_array->is_variable;
2300 array_type->array.size = parsed_array->size;
2307 type_t *hashed_type = typehash_insert(type);
2308 if(hashed_type != type) {
2309 /* the function type was constructed earlier freeing it here will
2310 * destroy other types... */
2311 if(iter->type != CONSTRUCT_FUNCTION) {
2321 static declaration_t *parse_declarator(
2322 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2324 type_t *type = specifiers->type;
2325 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2326 declaration->storage_class = specifiers->storage_class;
2327 declaration->is_inline = specifiers->is_inline;
2329 construct_type_t *construct_type
2330 = parse_inner_declarator(declaration, may_be_abstract);
2331 declaration->type = construct_declarator_type(construct_type, type);
2333 if(construct_type != NULL) {
2334 obstack_free(&temp_obst, construct_type);
2340 static type_t *parse_abstract_declarator(type_t *base_type)
2342 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2344 type_t *result = construct_declarator_type(construct_type, base_type);
2345 if(construct_type != NULL) {
2346 obstack_free(&temp_obst, construct_type);
2352 static declaration_t *record_declaration(declaration_t *declaration)
2354 assert(declaration->parent_context == NULL);
2355 assert(context != NULL);
2357 symbol_t *symbol = declaration->symbol;
2358 if(symbol != NULL) {
2359 declaration_t *alias = environment_push(declaration);
2360 if(alias != declaration)
2363 declaration->parent_context = context;
2366 if(last_declaration != NULL) {
2367 last_declaration->next = declaration;
2369 context->declarations = declaration;
2371 last_declaration = declaration;
2376 static void parser_error_multiple_definition(declaration_t *declaration,
2377 const source_position_t source_position)
2379 parser_print_error_prefix_pos(source_position);
2380 fprintf(stderr, "multiple definition of symbol '%s'\n",
2381 declaration->symbol->string);
2382 parser_print_error_prefix_pos(declaration->source_position);
2383 fprintf(stderr, "this is the location of the previous definition.\n");
2386 static bool is_declaration_specifier(const token_t *token,
2387 bool only_type_specifiers)
2389 switch(token->type) {
2393 return is_typedef_symbol(token->v.symbol);
2395 case T___extension__:
2398 return !only_type_specifiers;
2405 static void parse_init_declarator_rest(declaration_t *declaration)
2409 type_t *orig_type = declaration->type;
2410 type_t *type = NULL;
2411 if(orig_type != NULL)
2412 type = skip_typeref(orig_type);
2414 if(declaration->init.initializer != NULL) {
2415 parser_error_multiple_definition(declaration, token.source_position);
2418 initializer_t *initializer = parse_initializer(type);
2420 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2421 * the array type size */
2422 if(type != NULL && is_type_array(type) && initializer != NULL) {
2423 array_type_t *array_type = &type->array;
2425 if(array_type->size == NULL) {
2426 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2428 cnst->base.datatype = type_size_t;
2430 if(initializer->type == INITIALIZER_LIST) {
2431 initializer_list_t *list = &initializer->list;
2432 cnst->conste.v.int_value = list->len;
2434 assert(initializer->type == INITIALIZER_STRING);
2435 initializer_string_t *string = &initializer->string;
2436 cnst->conste.v.int_value = strlen(string->string) + 1;
2439 array_type->size = cnst;
2443 if(type != NULL && is_type_function(type)) {
2444 parser_print_error_prefix_pos(declaration->source_position);
2445 fprintf(stderr, "initializers not allowed for function types at "
2446 "declator '%s' (type ", declaration->symbol->string);
2447 print_type_quoted(orig_type);
2448 fprintf(stderr, ")\n");
2450 declaration->init.initializer = initializer;
2454 /* parse rest of a declaration without any declarator */
2455 static void parse_anonymous_declaration_rest(
2456 const declaration_specifiers_t *specifiers,
2457 parsed_declaration_func finished_declaration)
2461 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2463 declaration->type = specifiers->type;
2464 declaration->storage_class = specifiers->storage_class;
2465 declaration->source_position = specifiers->source_position;
2467 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2468 parse_warning_pos(declaration->source_position,
2469 "useless storage class in empty declaration");
2472 type_t *type = declaration->type;
2473 switch (type->type) {
2474 case TYPE_COMPOUND_STRUCT:
2475 case TYPE_COMPOUND_UNION: {
2476 const compound_type_t *compound_type = &type->compound;
2477 if (compound_type->declaration->symbol == NULL) {
2478 parse_warning_pos(declaration->source_position,
2479 "unnamed struct/union that defines no instances");
2488 parse_warning_pos(declaration->source_position,
2489 "empty declaration");
2493 finished_declaration(declaration);
2496 static void parse_declaration_rest(declaration_t *ndeclaration,
2497 const declaration_specifiers_t *specifiers,
2498 parsed_declaration_func finished_declaration)
2501 declaration_t *declaration = finished_declaration(ndeclaration);
2503 type_t *orig_type = declaration->type;
2504 type_t *type = skip_typeref(orig_type);
2506 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2507 parser_print_warning_prefix_pos(declaration->source_position);
2508 fprintf(stderr, "variable '%s' declared 'inline'\n",
2509 declaration->symbol->string);
2512 if(token.type == '=') {
2513 parse_init_declarator_rest(declaration);
2516 if(token.type != ',')
2520 ndeclaration = parse_declarator(specifiers, false);
2525 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2527 /* TODO: check that it was actually a parameter that gets a type */
2529 /* we should have a declaration for the parameter in the current
2531 return record_declaration(declaration);
2534 static void parse_declaration(parsed_declaration_func finished_declaration)
2536 declaration_specifiers_t specifiers;
2537 memset(&specifiers, 0, sizeof(specifiers));
2538 parse_declaration_specifiers(&specifiers);
2540 if(token.type == ';') {
2541 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2543 declaration_t *declaration = parse_declarator(&specifiers, false);
2544 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2548 static void parse_kr_declaration_list(declaration_t *declaration)
2550 type_t *type = skip_typeref(declaration->type);
2551 assert(is_type_function(type));
2553 if(!type->function.kr_style_parameters)
2556 /* push function parameters */
2557 int top = environment_top();
2558 context_t *last_context = context;
2559 set_context(&declaration->context);
2561 declaration_t *parameter = declaration->context.declarations;
2562 for( ; parameter != NULL; parameter = parameter->next) {
2563 environment_push(parameter);
2566 /* parse declaration list */
2567 while(is_declaration_specifier(&token, false)) {
2568 parse_declaration(finished_kr_declaration);
2571 /* pop function parameters */
2572 assert(context == &declaration->context);
2573 set_context(last_context);
2574 environment_pop_to(top);
2576 /* update function type */
2577 type_t *new_type = duplicate_type(type);
2578 new_type->function.kr_style_parameters = false;
2580 function_parameter_t *parameters = NULL;
2581 function_parameter_t *last_parameter = NULL;
2583 declaration_t *parameter_declaration = declaration->context.declarations;
2584 for( ; parameter_declaration != NULL;
2585 parameter_declaration = parameter_declaration->next) {
2586 type_t *parameter_type = parameter_declaration->type;
2587 if(parameter_type == NULL) {
2589 parser_print_error_prefix();
2590 fprintf(stderr, "no type specified for function parameter '%s'\n",
2591 parameter_declaration->symbol->string);
2593 parser_print_warning_prefix();
2594 fprintf(stderr, "no type specified for function parameter '%s', "
2595 "using int\n", parameter_declaration->symbol->string);
2596 parameter_type = type_int;
2597 parameter_declaration->type = parameter_type;
2601 semantic_parameter(parameter_declaration);
2602 parameter_type = parameter_declaration->type;
2604 function_parameter_t *function_parameter
2605 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2606 memset(function_parameter, 0, sizeof(function_parameter[0]));
2608 function_parameter->type = parameter_type;
2609 if(last_parameter != NULL) {
2610 last_parameter->next = function_parameter;
2612 parameters = function_parameter;
2614 last_parameter = function_parameter;
2616 new_type->function.parameters = parameters;
2618 type = typehash_insert(new_type);
2619 if(type != new_type) {
2620 obstack_free(type_obst, new_type);
2623 declaration->type = type;
2626 static void parse_external_declaration(void)
2628 /* function-definitions and declarations both start with declaration
2630 declaration_specifiers_t specifiers;
2631 memset(&specifiers, 0, sizeof(specifiers));
2632 parse_declaration_specifiers(&specifiers);
2634 /* must be a declaration */
2635 if(token.type == ';') {
2636 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2640 /* declarator is common to both function-definitions and declarations */
2641 declaration_t *ndeclaration = parse_declarator(&specifiers, false);
2643 /* must be a declaration */
2644 if(token.type == ',' || token.type == '=' || token.type == ';') {
2645 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2649 /* must be a function definition */
2650 parse_kr_declaration_list(ndeclaration);
2652 if(token.type != '{') {
2653 parse_error_expected("while parsing function definition", '{', 0);
2658 type_t *type = ndeclaration->type;
2664 /* note that we don't skip typerefs: the standard doesn't allow them here
2665 * (so we can't use is_type_function here) */
2666 if(type->type != TYPE_FUNCTION) {
2667 parser_print_error_prefix();
2668 fprintf(stderr, "declarator '");
2669 print_type_ext(type, ndeclaration->symbol, NULL);
2670 fprintf(stderr, "' has a body but is not a function type.\n");
2675 /* § 6.7.5.3 (14) a function definition with () means no
2676 * parameters (and not unspecified parameters) */
2677 if(type->function.unspecified_parameters) {
2678 type_t *duplicate = duplicate_type(type);
2679 duplicate->function.unspecified_parameters = false;
2681 type = typehash_insert(duplicate);
2682 if(type != duplicate) {
2683 obstack_free(type_obst, duplicate);
2685 ndeclaration->type = type;
2688 declaration_t *declaration = record_declaration(ndeclaration);
2689 if(ndeclaration != declaration) {
2690 memcpy(&declaration->context, &ndeclaration->context,
2691 sizeof(declaration->context));
2693 type = skip_typeref(declaration->type);
2695 /* push function parameters and switch context */
2696 int top = environment_top();
2697 context_t *last_context = context;
2698 set_context(&declaration->context);
2700 declaration_t *parameter = declaration->context.declarations;
2701 for( ; parameter != NULL; parameter = parameter->next) {
2702 environment_push(parameter);
2705 if(declaration->init.statement != NULL) {
2706 parser_error_multiple_definition(declaration, token.source_position);
2708 goto end_of_parse_external_declaration;
2710 /* parse function body */
2711 int label_stack_top = label_top();
2712 declaration_t *old_current_function = current_function;
2713 current_function = declaration;
2715 declaration->init.statement = parse_compound_statement();
2717 assert(current_function == declaration);
2718 current_function = old_current_function;
2719 label_pop_to(label_stack_top);
2722 end_of_parse_external_declaration:
2723 assert(context == &declaration->context);
2724 set_context(last_context);
2725 environment_pop_to(top);
2728 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2731 if(token.type == ':') {
2733 parse_constant_expression();
2734 /* TODO (bitfields) */
2736 declaration_t *declaration = parse_declarator(specifiers, true);
2738 /* TODO: check constraints for struct declarations */
2739 /* TODO: check for doubled fields */
2740 record_declaration(declaration);
2742 if(token.type == ':') {
2744 parse_constant_expression();
2745 /* TODO (bitfields) */
2749 if(token.type != ',')
2756 static void parse_compound_type_entries(void)
2760 while(token.type != '}' && token.type != T_EOF) {
2761 declaration_specifiers_t specifiers;
2762 memset(&specifiers, 0, sizeof(specifiers));
2763 parse_declaration_specifiers(&specifiers);
2765 parse_struct_declarators(&specifiers);
2767 if(token.type == T_EOF) {
2768 parse_error("EOF while parsing struct");
2773 static type_t *parse_typename(void)
2775 declaration_specifiers_t specifiers;
2776 memset(&specifiers, 0, sizeof(specifiers));
2777 parse_declaration_specifiers(&specifiers);
2778 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2779 /* TODO: improve error message, user does probably not know what a
2780 * storage class is...
2782 parse_error("typename may not have a storage class");
2785 type_t *result = parse_abstract_declarator(specifiers.type);
2793 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2794 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2795 expression_t *left);
2797 typedef struct expression_parser_function_t expression_parser_function_t;
2798 struct expression_parser_function_t {
2799 unsigned precedence;
2800 parse_expression_function parser;
2801 unsigned infix_precedence;
2802 parse_expression_infix_function infix_parser;
2805 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2807 static expression_t *make_invalid_expression(void)
2809 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2810 expression->base.source_position = token.source_position;
2814 static expression_t *expected_expression_error(void)
2816 parser_print_error_prefix();
2817 fprintf(stderr, "expected expression, got token ");
2818 print_token(stderr, &token);
2819 fprintf(stderr, "\n");
2823 return make_invalid_expression();
2826 static expression_t *parse_string_const(void)
2828 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2829 cnst->base.datatype = type_string;
2830 cnst->string.value = parse_string_literals();
2835 static expression_t *parse_wide_string_const(void)
2837 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2838 cnst->base.datatype = type_wchar_t_ptr;
2839 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2844 static expression_t *parse_int_const(void)
2846 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2847 cnst->base.datatype = token.datatype;
2848 cnst->conste.v.int_value = token.v.intvalue;
2855 static expression_t *parse_float_const(void)
2857 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2858 cnst->base.datatype = token.datatype;
2859 cnst->conste.v.float_value = token.v.floatvalue;
2866 static declaration_t *create_implicit_function(symbol_t *symbol,
2867 const source_position_t source_position)
2869 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2870 ntype->function.return_type = type_int;
2871 ntype->function.unspecified_parameters = true;
2873 type_t *type = typehash_insert(ntype);
2878 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2880 declaration->storage_class = STORAGE_CLASS_EXTERN;
2881 declaration->type = type;
2882 declaration->symbol = symbol;
2883 declaration->source_position = source_position;
2885 /* prepend the implicit definition to the global context
2886 * this is safe since the symbol wasn't declared as anything else yet
2888 assert(symbol->declaration == NULL);
2890 context_t *last_context = context;
2891 context = global_context;
2893 environment_push(declaration);
2894 declaration->next = context->declarations;
2895 context->declarations = declaration;
2897 context = last_context;
2902 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2904 function_parameter_t *parameter
2905 = obstack_alloc(type_obst, sizeof(parameter[0]));
2906 memset(parameter, 0, sizeof(parameter[0]));
2907 parameter->type = argument_type;
2909 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2910 type->function.return_type = return_type;
2911 type->function.parameters = parameter;
2913 type_t *result = typehash_insert(type);
2914 if(result != type) {
2921 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2923 switch(symbol->ID) {
2924 case T___builtin_alloca:
2925 return make_function_1_type(type_void_ptr, type_size_t);
2926 case T___builtin_nan:
2927 return make_function_1_type(type_double, type_string);
2928 case T___builtin_nanf:
2929 return make_function_1_type(type_float, type_string);
2930 case T___builtin_nand:
2931 return make_function_1_type(type_long_double, type_string);
2933 panic("not implemented builtin symbol found");
2938 * performs automatic type cast as described in § 6.3.2.1
2940 static type_t *automatic_type_conversion(type_t *orig_type)
2942 if(orig_type == NULL)
2945 type_t *type = skip_typeref(orig_type);
2946 if(is_type_array(type)) {
2947 array_type_t *array_type = &type->array;
2948 type_t *element_type = array_type->element_type;
2949 unsigned qualifiers = array_type->type.qualifiers;
2951 return make_pointer_type(element_type, qualifiers);
2954 if(is_type_function(type)) {
2955 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
2962 * reverts the automatic casts of array to pointer types and function
2963 * to function-pointer types as defined § 6.3.2.1
2965 type_t *revert_automatic_type_conversion(const expression_t *expression)
2967 if(expression->base.datatype == NULL)
2970 switch(expression->type) {
2971 case EXPR_REFERENCE: {
2972 const reference_expression_t *ref = &expression->reference;
2973 return ref->declaration->type;
2976 const select_expression_t *select = &expression->select;
2977 return select->compound_entry->type;
2980 const unary_expression_t *unary = &expression->unary;
2981 if(unary->type == UNEXPR_DEREFERENCE) {
2982 expression_t *value = unary->value;
2983 type_t *type = skip_typeref(value->base.datatype);
2984 pointer_type_t *pointer_type = &type->pointer;
2986 return pointer_type->points_to;
2990 case EXPR_BUILTIN_SYMBOL: {
2991 const builtin_symbol_expression_t *builtin
2992 = &expression->builtin_symbol;
2993 return get_builtin_symbol_type(builtin->symbol);
2995 case EXPR_ARRAY_ACCESS: {
2996 const array_access_expression_t *array_access
2997 = &expression->array_access;
2998 const expression_t *array_ref = array_access->array_ref;
2999 type_t *type_left = skip_typeref(array_ref->base.datatype);
3000 assert(is_type_pointer(type_left));
3001 pointer_type_t *pointer_type = &type_left->pointer;
3002 return pointer_type->points_to;
3009 return expression->base.datatype;
3012 static expression_t *parse_reference(void)
3014 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3016 reference_expression_t *ref = &expression->reference;
3017 ref->symbol = token.v.symbol;
3019 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3021 source_position_t source_position = token.source_position;
3024 if(declaration == NULL) {
3026 /* an implicitly defined function */
3027 if(token.type == '(') {
3028 parser_print_prefix_pos(token.source_position);
3029 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3030 ref->symbol->string);
3032 declaration = create_implicit_function(ref->symbol,
3037 parser_print_error_prefix();
3038 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3043 type_t *type = declaration->type;
3044 /* we always do the auto-type conversions; the & and sizeof parser contains
3045 * code to revert this! */
3046 type = automatic_type_conversion(type);
3048 ref->declaration = declaration;
3049 ref->expression.datatype = type;
3054 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3058 /* TODO check if explicit cast is allowed and issue warnings/errors */
3061 static expression_t *parse_cast(void)
3063 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
3065 cast->unary.type = UNEXPR_CAST;
3066 cast->base.source_position = token.source_position;
3068 type_t *type = parse_typename();
3071 expression_t *value = parse_sub_expression(20);
3073 check_cast_allowed(value, type);
3075 cast->base.datatype = type;
3076 cast->unary.value = value;
3081 static expression_t *parse_statement_expression(void)
3083 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3085 statement_t *statement = parse_compound_statement();
3086 expression->statement.statement = statement;
3087 if(statement == NULL) {
3092 assert(statement->type == STATEMENT_COMPOUND);
3093 compound_statement_t *compound_statement = &statement->compound;
3095 /* find last statement and use it's type */
3096 const statement_t *last_statement = NULL;
3097 const statement_t *iter = compound_statement->statements;
3098 for( ; iter != NULL; iter = iter->base.next) {
3099 last_statement = iter;
3102 if(last_statement->type == STATEMENT_EXPRESSION) {
3103 const expression_statement_t *expression_statement
3104 = &last_statement->expression;
3105 expression->base.datatype
3106 = expression_statement->expression->base.datatype;
3108 expression->base.datatype = type_void;
3116 static expression_t *parse_brace_expression(void)
3120 switch(token.type) {
3122 /* gcc extension: a stement expression */
3123 return parse_statement_expression();
3127 return parse_cast();
3129 if(is_typedef_symbol(token.v.symbol)) {
3130 return parse_cast();
3134 expression_t *result = parse_expression();
3140 static expression_t *parse_function_keyword(void)
3145 if (current_function == NULL) {
3146 parse_error("'__func__' used outside of a function");
3149 string_literal_expression_t *expression
3150 = allocate_ast_zero(sizeof(expression[0]));
3152 expression->expression.type = EXPR_FUNCTION;
3153 expression->expression.datatype = type_string;
3154 expression->value = "TODO: FUNCTION";
3156 return (expression_t*) expression;
3159 static expression_t *parse_pretty_function_keyword(void)
3161 eat(T___PRETTY_FUNCTION__);
3164 string_literal_expression_t *expression
3165 = allocate_ast_zero(sizeof(expression[0]));
3167 expression->expression.type = EXPR_PRETTY_FUNCTION;
3168 expression->expression.datatype = type_string;
3169 expression->value = "TODO: PRETTY FUNCTION";
3171 return (expression_t*) expression;
3174 static designator_t *parse_designator(void)
3176 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3178 if(token.type != T_IDENTIFIER) {
3179 parse_error_expected("while parsing member designator",
3184 result->symbol = token.v.symbol;
3187 designator_t *last_designator = result;
3189 if(token.type == '.') {
3191 if(token.type != T_IDENTIFIER) {
3192 parse_error_expected("while parsing member designator",
3197 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3198 designator->symbol = token.v.symbol;
3201 last_designator->next = designator;
3202 last_designator = designator;
3205 if(token.type == '[') {
3207 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3208 designator->array_access = parse_expression();
3209 if(designator->array_access == NULL) {
3215 last_designator->next = designator;
3216 last_designator = designator;
3225 static expression_t *parse_offsetof(void)
3227 eat(T___builtin_offsetof);
3229 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3230 expression->base.datatype = type_size_t;
3233 expression->offsetofe.type = parse_typename();
3235 expression->offsetofe.designator = parse_designator();
3241 static expression_t *parse_va_arg(void)
3243 eat(T___builtin_va_arg);
3245 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3248 expression->va_arge.arg = parse_assignment_expression();
3250 expression->base.datatype = parse_typename();
3256 static expression_t *parse_builtin_symbol(void)
3258 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3260 symbol_t *symbol = token.v.symbol;
3262 expression->builtin_symbol.symbol = symbol;
3265 type_t *type = get_builtin_symbol_type(symbol);
3266 type = automatic_type_conversion(type);
3268 expression->base.datatype = type;
3272 static expression_t *parse_primary_expression(void)
3274 switch(token.type) {
3276 return parse_int_const();
3277 case T_FLOATINGPOINT:
3278 return parse_float_const();
3279 case T_STRING_LITERAL: /* TODO merge */
3280 return parse_string_const();
3281 case T_WIDE_STRING_LITERAL:
3282 return parse_wide_string_const();
3284 return parse_reference();
3285 case T___FUNCTION__:
3287 return parse_function_keyword();
3288 case T___PRETTY_FUNCTION__:
3289 return parse_pretty_function_keyword();
3290 case T___builtin_offsetof:
3291 return parse_offsetof();
3292 case T___builtin_va_arg:
3293 return parse_va_arg();
3294 case T___builtin_nanf:
3295 case T___builtin_alloca:
3296 case T___builtin_expect:
3297 case T___builtin_va_start:
3298 case T___builtin_va_end:
3299 return parse_builtin_symbol();
3302 return parse_brace_expression();
3305 parser_print_error_prefix();
3306 fprintf(stderr, "unexpected token ");
3307 print_token(stderr, &token);
3308 fprintf(stderr, "\n");
3311 return make_invalid_expression();
3314 static expression_t *parse_array_expression(unsigned precedence,
3321 expression_t *inside = parse_expression();
3323 array_access_expression_t *array_access
3324 = allocate_ast_zero(sizeof(array_access[0]));
3326 array_access->expression.type = EXPR_ARRAY_ACCESS;
3328 type_t *type_left = left->base.datatype;
3329 type_t *type_inside = inside->base.datatype;
3330 type_t *return_type = NULL;
3332 if(type_left != NULL && type_inside != NULL) {
3333 type_left = skip_typeref(type_left);
3334 type_inside = skip_typeref(type_inside);
3336 if(is_type_pointer(type_left)) {
3337 pointer_type_t *pointer = &type_left->pointer;
3338 return_type = pointer->points_to;
3339 array_access->array_ref = left;
3340 array_access->index = inside;
3341 } else if(is_type_pointer(type_inside)) {
3342 pointer_type_t *pointer = &type_inside->pointer;
3343 return_type = pointer->points_to;
3344 array_access->array_ref = inside;
3345 array_access->index = left;
3346 array_access->flipped = true;
3348 parser_print_error_prefix();
3349 fprintf(stderr, "array access on object with non-pointer types ");
3350 print_type_quoted(type_left);
3351 fprintf(stderr, ", ");
3352 print_type_quoted(type_inside);
3353 fprintf(stderr, "\n");
3356 array_access->array_ref = left;
3357 array_access->index = inside;
3360 if(token.type != ']') {
3361 parse_error_expected("Problem while parsing array access", ']', 0);
3362 return (expression_t*) array_access;
3366 return_type = automatic_type_conversion(return_type);
3367 array_access->expression.datatype = return_type;
3369 return (expression_t*) array_access;
3372 static expression_t *parse_sizeof(unsigned precedence)
3376 sizeof_expression_t *sizeof_expression
3377 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3378 sizeof_expression->expression.type = EXPR_SIZEOF;
3379 sizeof_expression->expression.datatype = type_size_t;
3381 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3383 sizeof_expression->type = parse_typename();
3386 expression_t *expression = parse_sub_expression(precedence);
3387 expression->base.datatype = revert_automatic_type_conversion(expression);
3389 sizeof_expression->type = expression->base.datatype;
3390 sizeof_expression->size_expression = expression;
3393 return (expression_t*) sizeof_expression;
3396 static expression_t *parse_select_expression(unsigned precedence,
3397 expression_t *compound)
3400 assert(token.type == '.' || token.type == T_MINUSGREATER);
3402 bool is_pointer = (token.type == T_MINUSGREATER);
3405 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3406 select->select.compound = compound;
3408 if(token.type != T_IDENTIFIER) {
3409 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3412 symbol_t *symbol = token.v.symbol;
3413 select->select.symbol = symbol;
3416 type_t *orig_type = compound->base.datatype;
3417 if(orig_type == NULL)
3418 return make_invalid_expression();
3420 type_t *type = skip_typeref(orig_type);
3422 type_t *type_left = type;
3424 if(type->type != TYPE_POINTER) {
3425 parser_print_error_prefix();
3426 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3427 print_type_quoted(orig_type);
3428 fputc('\n', stderr);
3429 return make_invalid_expression();
3431 pointer_type_t *pointer_type = &type->pointer;
3432 type_left = pointer_type->points_to;
3434 type_left = skip_typeref(type_left);
3436 if(type_left->type != TYPE_COMPOUND_STRUCT
3437 && type_left->type != TYPE_COMPOUND_UNION) {
3438 parser_print_error_prefix();
3439 fprintf(stderr, "request for member '%s' in something not a struct or "
3440 "union, but ", symbol->string);
3441 print_type_quoted(type_left);
3442 fputc('\n', stderr);
3443 return make_invalid_expression();
3446 compound_type_t *compound_type = &type_left->compound;
3447 declaration_t *declaration = compound_type->declaration;
3449 if(!declaration->init.is_defined) {
3450 parser_print_error_prefix();
3451 fprintf(stderr, "request for member '%s' of incomplete type ",
3453 print_type_quoted(type_left);
3454 fputc('\n', stderr);
3455 return make_invalid_expression();
3458 declaration_t *iter = declaration->context.declarations;
3459 for( ; iter != NULL; iter = iter->next) {
3460 if(iter->symbol == symbol) {
3465 parser_print_error_prefix();
3466 print_type_quoted(type_left);
3467 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3468 return make_invalid_expression();
3471 /* we always do the auto-type conversions; the & and sizeof parser contains
3472 * code to revert this! */
3473 type_t *expression_type = automatic_type_conversion(iter->type);
3475 select->select.compound_entry = iter;
3476 select->base.datatype = expression_type;
3480 static expression_t *parse_call_expression(unsigned precedence,
3481 expression_t *expression)
3484 expression_t *result = allocate_expression_zero(EXPR_CALL);
3486 call_expression_t *call = &result->call;
3487 call->function = expression;
3489 function_type_t *function_type = NULL;
3490 type_t *orig_type = expression->base.datatype;
3491 if(orig_type != NULL) {
3492 type_t *type = skip_typeref(orig_type);
3494 if(is_type_pointer(type)) {
3495 pointer_type_t *pointer_type = &type->pointer;
3497 type = skip_typeref(pointer_type->points_to);
3499 if (is_type_function(type)) {
3500 function_type = &type->function;
3501 call->expression.datatype = function_type->return_type;
3504 if(function_type == NULL) {
3505 parser_print_error_prefix();
3506 fputs("called object '", stderr);
3507 print_expression(expression);
3508 fputs("' (type ", stderr);
3509 print_type_quoted(orig_type);
3510 fputs(") is not a pointer to a function\n", stderr);
3512 function_type = NULL;
3513 call->expression.datatype = NULL;
3517 /* parse arguments */
3520 if(token.type != ')') {
3521 call_argument_t *last_argument = NULL;
3524 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3526 argument->expression = parse_assignment_expression();
3527 if(last_argument == NULL) {
3528 call->arguments = argument;
3530 last_argument->next = argument;
3532 last_argument = argument;
3534 if(token.type != ',')
3541 if(function_type != NULL) {
3542 function_parameter_t *parameter = function_type->parameters;
3543 call_argument_t *argument = call->arguments;
3544 for( ; parameter != NULL && argument != NULL;
3545 parameter = parameter->next, argument = argument->next) {
3546 type_t *expected_type = parameter->type;
3547 /* TODO report context in error messages */
3548 argument->expression = create_implicit_cast(argument->expression,
3551 /* too few parameters */
3552 if(parameter != NULL) {
3553 parser_print_error_prefix();
3554 fprintf(stderr, "too few arguments to function '");
3555 print_expression(expression);
3556 fprintf(stderr, "'\n");
3557 } else if(argument != NULL) {
3558 /* too many parameters */
3559 if(!function_type->variadic
3560 && !function_type->unspecified_parameters) {
3561 parser_print_error_prefix();
3562 fprintf(stderr, "too many arguments to function '");
3563 print_expression(expression);
3564 fprintf(stderr, "'\n");
3566 /* do default promotion */
3567 for( ; argument != NULL; argument = argument->next) {
3568 type_t *type = argument->expression->base.datatype;
3573 type = skip_typeref(type);
3574 if(is_type_integer(type)) {
3575 type = promote_integer(type);
3576 } else if(type == type_float) {
3580 argument->expression
3581 = create_implicit_cast(argument->expression, type);
3590 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3592 static bool same_compound_type(const type_t *type1, const type_t *type2)
3594 if(!is_type_compound(type1))
3596 if(type1->type != type2->type)
3599 const compound_type_t *compound1 = &type1->compound;
3600 const compound_type_t *compound2 = &type2->compound;
3602 return compound1->declaration == compound2->declaration;
3605 static expression_t *parse_conditional_expression(unsigned precedence,
3606 expression_t *expression)
3610 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3612 conditional_expression_t *conditional = &result->conditional;
3613 conditional->condition = expression;
3616 type_t *condition_type_orig = expression->base.datatype;
3617 if(condition_type_orig != NULL) {
3618 type_t *condition_type = skip_typeref(condition_type_orig);
3619 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3620 type_error("expected a scalar type in conditional condition",
3621 expression->base.source_position, condition_type_orig);
3625 expression_t *true_expression = parse_expression();
3627 expression_t *false_expression = parse_sub_expression(precedence);
3629 conditional->true_expression = true_expression;
3630 conditional->false_expression = false_expression;
3632 type_t *orig_true_type = true_expression->base.datatype;
3633 type_t *orig_false_type = false_expression->base.datatype;
3634 if(orig_true_type == NULL || orig_false_type == NULL)
3637 type_t *true_type = skip_typeref(orig_true_type);
3638 type_t *false_type = skip_typeref(orig_false_type);
3641 type_t *result_type = NULL;
3642 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3643 result_type = semantic_arithmetic(true_type, false_type);
3645 true_expression = create_implicit_cast(true_expression, result_type);
3646 false_expression = create_implicit_cast(false_expression, result_type);
3648 conditional->true_expression = true_expression;
3649 conditional->false_expression = false_expression;
3650 conditional->expression.datatype = result_type;
3651 } else if (same_compound_type(true_type, false_type)
3652 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3653 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3654 /* just take 1 of the 2 types */
3655 result_type = true_type;
3656 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3657 && pointers_compatible(true_type, false_type)) {
3659 result_type = true_type;
3662 type_error_incompatible("while parsing conditional",
3663 expression->base.source_position, true_type,
3667 conditional->expression.datatype = result_type;
3671 static expression_t *parse_extension(unsigned precedence)
3673 eat(T___extension__);
3675 /* TODO enable extensions */
3677 return parse_sub_expression(precedence);
3680 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3682 eat(T___builtin_classify_type);
3684 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3685 result->base.datatype = type_int;
3688 expression_t *expression = parse_sub_expression(precedence);
3690 result->classify_type.type_expression = expression;
3695 static void semantic_incdec(unary_expression_t *expression)
3697 type_t *orig_type = expression->value->base.datatype;
3698 if(orig_type == NULL)
3701 type_t *type = skip_typeref(orig_type);
3702 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3703 /* TODO: improve error message */
3704 parser_print_error_prefix();
3705 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3709 expression->expression.datatype = orig_type;
3712 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3714 type_t *orig_type = expression->value->base.datatype;
3715 if(orig_type == NULL)
3718 type_t *type = skip_typeref(orig_type);
3719 if(!is_type_arithmetic(type)) {
3720 /* TODO: improve error message */
3721 parser_print_error_prefix();
3722 fprintf(stderr, "operation needs an arithmetic type\n");
3726 expression->expression.datatype = orig_type;
3729 static void semantic_unexpr_scalar(unary_expression_t *expression)
3731 type_t *orig_type = expression->value->base.datatype;
3732 if(orig_type == NULL)
3735 type_t *type = skip_typeref(orig_type);
3736 if (!is_type_scalar(type)) {
3737 parse_error("operand of ! must be of scalar type\n");
3741 expression->expression.datatype = orig_type;
3744 static void semantic_unexpr_integer(unary_expression_t *expression)
3746 type_t *orig_type = expression->value->base.datatype;
3747 if(orig_type == NULL)
3750 type_t *type = skip_typeref(orig_type);
3751 if (!is_type_integer(type)) {
3752 parse_error("operand of ~ must be of integer type\n");
3756 expression->expression.datatype = orig_type;
3759 static void semantic_dereference(unary_expression_t *expression)
3761 type_t *orig_type = expression->value->base.datatype;
3762 if(orig_type == NULL)
3765 type_t *type = skip_typeref(orig_type);
3766 if(!is_type_pointer(type)) {
3767 parser_print_error_prefix();
3768 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3769 print_type_quoted(orig_type);
3770 fputs(" given.\n", stderr);
3774 pointer_type_t *pointer_type = &type->pointer;
3775 type_t *result_type = pointer_type->points_to;
3777 result_type = automatic_type_conversion(result_type);
3778 expression->expression.datatype = result_type;
3781 static void semantic_take_addr(unary_expression_t *expression)
3783 expression_t *value = expression->value;
3784 value->base.datatype = revert_automatic_type_conversion(value);
3786 type_t *orig_type = value->base.datatype;
3787 if(orig_type == NULL)
3790 if(value->type == EXPR_REFERENCE) {
3791 reference_expression_t *reference = (reference_expression_t*) value;
3792 declaration_t *declaration = reference->declaration;
3793 if(declaration != NULL) {
3794 declaration->address_taken = 1;
3798 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3801 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3802 static expression_t *parse_##unexpression_type(unsigned precedence) \
3806 unary_expression_t *unary_expression \
3807 = allocate_ast_zero(sizeof(unary_expression[0])); \
3808 unary_expression->expression.type = EXPR_UNARY; \
3809 unary_expression->type = unexpression_type; \
3810 unary_expression->value = parse_sub_expression(precedence); \
3812 sfunc(unary_expression); \
3814 return (expression_t*) unary_expression; \
3817 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3818 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3819 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3820 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3821 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3822 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3823 semantic_unexpr_integer)
3824 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3826 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3829 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3831 static expression_t *parse_##unexpression_type(unsigned precedence, \
3832 expression_t *left) \
3834 (void) precedence; \
3837 unary_expression_t *unary_expression \
3838 = allocate_ast_zero(sizeof(unary_expression[0])); \
3839 unary_expression->expression.type = EXPR_UNARY; \
3840 unary_expression->type = unexpression_type; \
3841 unary_expression->value = left; \
3843 sfunc(unary_expression); \
3845 return (expression_t*) unary_expression; \
3848 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3850 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3853 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3855 /* TODO: handle complex + imaginary types */
3857 /* § 6.3.1.8 Usual arithmetic conversions */
3858 if(type_left == type_long_double || type_right == type_long_double) {
3859 return type_long_double;
3860 } else if(type_left == type_double || type_right == type_double) {
3862 } else if(type_left == type_float || type_right == type_float) {
3866 type_right = promote_integer(type_right);
3867 type_left = promote_integer(type_left);
3869 if(type_left == type_right)
3872 bool signed_left = is_type_signed(type_left);
3873 bool signed_right = is_type_signed(type_right);
3874 int rank_left = get_rank(type_left);
3875 int rank_right = get_rank(type_right);
3876 if(rank_left < rank_right) {
3877 if(signed_left == signed_right || !signed_right) {
3883 if(signed_left == signed_right || !signed_left) {
3891 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3893 expression_t *left = expression->left;
3894 expression_t *right = expression->right;
3895 type_t *orig_type_left = left->base.datatype;
3896 type_t *orig_type_right = right->base.datatype;
3898 if(orig_type_left == NULL || orig_type_right == NULL)
3901 type_t *type_left = skip_typeref(orig_type_left);
3902 type_t *type_right = skip_typeref(orig_type_right);
3904 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3905 /* TODO: improve error message */
3906 parser_print_error_prefix();
3907 fprintf(stderr, "operation needs arithmetic types\n");
3911 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3912 expression->left = create_implicit_cast(left, arithmetic_type);
3913 expression->right = create_implicit_cast(right, arithmetic_type);
3914 expression->expression.datatype = arithmetic_type;
3917 static void semantic_shift_op(binary_expression_t *expression)
3919 expression_t *left = expression->left;
3920 expression_t *right = expression->right;
3921 type_t *orig_type_left = left->base.datatype;
3922 type_t *orig_type_right = right->base.datatype;
3924 if(orig_type_left == NULL || orig_type_right == NULL)
3927 type_t *type_left = skip_typeref(orig_type_left);
3928 type_t *type_right = skip_typeref(orig_type_right);
3930 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3931 /* TODO: improve error message */
3932 parser_print_error_prefix();
3933 fprintf(stderr, "operation needs integer types\n");
3937 type_left = promote_integer(type_left);
3938 type_right = promote_integer(type_right);
3940 expression->left = create_implicit_cast(left, type_left);
3941 expression->right = create_implicit_cast(right, type_right);
3942 expression->expression.datatype = type_left;
3945 static void semantic_add(binary_expression_t *expression)
3947 expression_t *left = expression->left;
3948 expression_t *right = expression->right;
3949 type_t *orig_type_left = left->base.datatype;
3950 type_t *orig_type_right = right->base.datatype;
3952 if(orig_type_left == NULL || orig_type_right == NULL)
3955 type_t *type_left = skip_typeref(orig_type_left);
3956 type_t *type_right = skip_typeref(orig_type_right);
3959 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3960 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3961 expression->left = create_implicit_cast(left, arithmetic_type);
3962 expression->right = create_implicit_cast(right, arithmetic_type);
3963 expression->expression.datatype = arithmetic_type;
3965 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3966 expression->expression.datatype = type_left;
3967 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3968 expression->expression.datatype = type_right;
3970 parser_print_error_prefix();
3971 fprintf(stderr, "invalid operands to binary + (");
3972 print_type_quoted(orig_type_left);
3973 fprintf(stderr, ", ");
3974 print_type_quoted(orig_type_right);
3975 fprintf(stderr, ")\n");
3979 static void semantic_sub(binary_expression_t *expression)
3981 expression_t *left = expression->left;
3982 expression_t *right = expression->right;
3983 type_t *orig_type_left = left->base.datatype;
3984 type_t *orig_type_right = right->base.datatype;
3986 if(orig_type_left == NULL || orig_type_right == NULL)
3989 type_t *type_left = skip_typeref(orig_type_left);
3990 type_t *type_right = skip_typeref(orig_type_right);
3993 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3994 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3995 expression->left = create_implicit_cast(left, arithmetic_type);
3996 expression->right = create_implicit_cast(right, arithmetic_type);
3997 expression->expression.datatype = arithmetic_type;
3999 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4000 expression->expression.datatype = type_left;
4001 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4002 if(!pointers_compatible(type_left, type_right)) {
4003 parser_print_error_prefix();
4004 fprintf(stderr, "pointers to incompatible objects to binary - (");
4005 print_type_quoted(orig_type_left);
4006 fprintf(stderr, ", ");
4007 print_type_quoted(orig_type_right);
4008 fprintf(stderr, ")\n");
4010 expression->expression.datatype = type_ptrdiff_t;
4013 parser_print_error_prefix();
4014 fprintf(stderr, "invalid operands to binary - (");
4015 print_type_quoted(orig_type_left);
4016 fprintf(stderr, ", ");
4017 print_type_quoted(orig_type_right);
4018 fprintf(stderr, ")\n");
4022 static void semantic_comparison(binary_expression_t *expression)
4024 expression_t *left = expression->left;
4025 expression_t *right = expression->right;
4026 type_t *orig_type_left = left->base.datatype;
4027 type_t *orig_type_right = right->base.datatype;
4029 if(orig_type_left == NULL || orig_type_right == NULL)
4032 type_t *type_left = skip_typeref(orig_type_left);
4033 type_t *type_right = skip_typeref(orig_type_right);
4035 /* TODO non-arithmetic types */
4036 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4037 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4038 expression->left = create_implicit_cast(left, arithmetic_type);
4039 expression->right = create_implicit_cast(right, arithmetic_type);
4040 expression->expression.datatype = arithmetic_type;
4041 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4042 /* TODO check compatibility */
4043 } else if (is_type_pointer(type_left)) {
4044 expression->right = create_implicit_cast(right, type_left);
4045 } else if (is_type_pointer(type_right)) {
4046 expression->left = create_implicit_cast(left, type_right);
4048 type_error_incompatible("invalid operands in comparison",
4049 token.source_position, type_left, type_right);
4051 expression->expression.datatype = type_int;
4054 static void semantic_arithmetic_assign(binary_expression_t *expression)
4056 expression_t *left = expression->left;
4057 expression_t *right = expression->right;
4058 type_t *orig_type_left = left->base.datatype;
4059 type_t *orig_type_right = right->base.datatype;
4061 if(orig_type_left == NULL || orig_type_right == NULL)
4064 type_t *type_left = skip_typeref(orig_type_left);
4065 type_t *type_right = skip_typeref(orig_type_right);
4067 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4068 /* TODO: improve error message */
4069 parser_print_error_prefix();
4070 fprintf(stderr, "operation needs arithmetic types\n");
4074 /* combined instructions are tricky. We can't create an implicit cast on
4075 * the left side, because we need the uncasted form for the store.
4076 * The ast2firm pass has to know that left_type must be right_type
4077 * for the arithmeitc operation and create a cast by itself */
4078 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4079 expression->right = create_implicit_cast(right, arithmetic_type);
4080 expression->expression.datatype = type_left;
4083 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4085 expression_t *left = expression->left;
4086 expression_t *right = expression->right;
4087 type_t *orig_type_left = left->base.datatype;
4088 type_t *orig_type_right = right->base.datatype;
4090 if(orig_type_left == NULL || orig_type_right == NULL)
4093 type_t *type_left = skip_typeref(orig_type_left);
4094 type_t *type_right = skip_typeref(orig_type_right);
4096 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4097 /* combined instructions are tricky. We can't create an implicit cast on
4098 * the left side, because we need the uncasted form for the store.
4099 * The ast2firm pass has to know that left_type must be right_type
4100 * for the arithmeitc operation and create a cast by itself */
4101 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4102 expression->right = create_implicit_cast(right, arithmetic_type);
4103 expression->expression.datatype = type_left;
4104 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4105 expression->expression.datatype = type_left;
4107 parser_print_error_prefix();
4108 fputs("Incompatible types ", stderr);
4109 print_type_quoted(orig_type_left);
4110 fputs(" and ", stderr);
4111 print_type_quoted(orig_type_right);
4112 fputs(" in assignment\n", stderr);
4117 static void semantic_logical_op(binary_expression_t *expression)
4119 expression_t *left = expression->left;
4120 expression_t *right = expression->right;
4121 type_t *orig_type_left = left->base.datatype;
4122 type_t *orig_type_right = right->base.datatype;
4124 if(orig_type_left == NULL || orig_type_right == NULL)
4127 type_t *type_left = skip_typeref(orig_type_left);
4128 type_t *type_right = skip_typeref(orig_type_right);
4130 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4131 /* TODO: improve error message */
4132 parser_print_error_prefix();
4133 fprintf(stderr, "operation needs scalar types\n");
4137 expression->expression.datatype = type_int;
4140 static bool has_const_fields(type_t *type)
4147 static void semantic_binexpr_assign(binary_expression_t *expression)
4149 expression_t *left = expression->left;
4150 type_t *orig_type_left = left->base.datatype;
4152 if(orig_type_left == NULL)
4155 type_t *type_left = revert_automatic_type_conversion(left);
4156 type_left = skip_typeref(orig_type_left);
4158 /* must be a modifiable lvalue */
4159 if (is_type_array(type_left)) {
4160 parser_print_error_prefix();
4161 fprintf(stderr, "Cannot assign to arrays ('");
4162 print_expression(left);
4163 fprintf(stderr, "')\n");
4166 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4167 parser_print_error_prefix();
4168 fprintf(stderr, "assignment to readonly location '");
4169 print_expression(left);
4170 fprintf(stderr, "' (type ");
4171 print_type_quoted(orig_type_left);
4172 fprintf(stderr, ")\n");
4175 if(is_type_incomplete(type_left)) {
4176 parser_print_error_prefix();
4177 fprintf(stderr, "left-hand side of assignment '");
4178 print_expression(left);
4179 fprintf(stderr, "' has incomplete type ");
4180 print_type_quoted(orig_type_left);
4181 fprintf(stderr, "\n");
4184 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4185 parser_print_error_prefix();
4186 fprintf(stderr, "can't assign to '");
4187 print_expression(left);
4188 fprintf(stderr, "' because compound type ");
4189 print_type_quoted(orig_type_left);
4190 fprintf(stderr, " has readonly fields\n");
4194 semantic_assign(orig_type_left, &expression->right, "assignment");
4196 expression->expression.datatype = orig_type_left;
4199 static void semantic_comma(binary_expression_t *expression)
4201 expression->expression.datatype = expression->right->base.datatype;
4204 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4205 static expression_t *parse_##binexpression_type(unsigned precedence, \
4206 expression_t *left) \
4210 expression_t *right = parse_sub_expression(precedence + lr); \
4212 binary_expression_t *binexpr \
4213 = allocate_ast_zero(sizeof(binexpr[0])); \
4214 binexpr->expression.type = EXPR_BINARY; \
4215 binexpr->type = binexpression_type; \
4216 binexpr->left = left; \
4217 binexpr->right = right; \
4220 return (expression_t*) binexpr; \
4223 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
4224 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
4225 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
4226 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
4227 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
4228 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
4229 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
4230 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
4231 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
4232 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
4233 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
4234 semantic_comparison, 1)
4235 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
4236 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
4237 semantic_comparison, 1)
4238 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
4239 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
4240 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
4241 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
4242 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
4243 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
4244 semantic_shift_op, 1)
4245 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
4246 semantic_shift_op, 1)
4247 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
4248 semantic_arithmetic_addsubb_assign, 0)
4249 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
4250 semantic_arithmetic_addsubb_assign, 0)
4251 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
4252 semantic_arithmetic_assign, 0)
4253 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
4254 semantic_arithmetic_assign, 0)
4255 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
4256 semantic_arithmetic_assign, 0)
4257 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
4258 semantic_arithmetic_assign, 0)
4259 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
4260 semantic_arithmetic_assign, 0)
4261 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
4262 semantic_arithmetic_assign, 0)
4263 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
4264 semantic_arithmetic_assign, 0)
4265 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
4266 semantic_arithmetic_assign, 0)
4268 static expression_t *parse_sub_expression(unsigned precedence)
4270 if(token.type < 0) {
4271 return expected_expression_error();
4274 expression_parser_function_t *parser
4275 = &expression_parsers[token.type];
4276 source_position_t source_position = token.source_position;
4279 if(parser->parser != NULL) {
4280 left = parser->parser(parser->precedence);
4282 left = parse_primary_expression();
4284 assert(left != NULL);
4285 left->base.source_position = source_position;
4288 if(token.type < 0) {
4289 return expected_expression_error();
4292 parser = &expression_parsers[token.type];
4293 if(parser->infix_parser == NULL)
4295 if(parser->infix_precedence < precedence)
4298 left = parser->infix_parser(parser->infix_precedence, left);
4300 assert(left != NULL);
4301 assert(left->type != EXPR_UNKNOWN);
4302 left->base.source_position = source_position;
4308 static expression_t *parse_expression(void)
4310 return parse_sub_expression(1);
4315 static void register_expression_parser(parse_expression_function parser,
4316 int token_type, unsigned precedence)
4318 expression_parser_function_t *entry = &expression_parsers[token_type];
4320 if(entry->parser != NULL) {
4321 fprintf(stderr, "for token ");
4322 print_token_type(stderr, (token_type_t) token_type);
4323 fprintf(stderr, "\n");
4324 panic("trying to register multiple expression parsers for a token");
4326 entry->parser = parser;
4327 entry->precedence = precedence;
4330 static void register_expression_infix_parser(
4331 parse_expression_infix_function parser, int token_type,
4332 unsigned precedence)
4334 expression_parser_function_t *entry = &expression_parsers[token_type];
4336 if(entry->infix_parser != NULL) {
4337 fprintf(stderr, "for token ");
4338 print_token_type(stderr, (token_type_t) token_type);
4339 fprintf(stderr, "\n");
4340 panic("trying to register multiple infix expression parsers for a "
4343 entry->infix_parser = parser;
4344 entry->infix_precedence = precedence;
4347 static void init_expression_parsers(void)
4349 memset(&expression_parsers, 0, sizeof(expression_parsers));
4351 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4352 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4353 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4354 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4355 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4356 T_GREATERGREATER, 16);
4357 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4358 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4359 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4360 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4361 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4362 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4363 T_GREATEREQUAL, 14);
4364 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4365 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4366 T_EXCLAMATIONMARKEQUAL, 13);
4367 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4368 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4369 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4370 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4371 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4372 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4373 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4374 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4375 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4376 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4377 T_ASTERISKEQUAL, 2);
4378 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4379 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4381 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4382 T_LESSLESSEQUAL, 2);
4383 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4384 T_GREATERGREATEREQUAL, 2);
4385 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4387 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4389 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4392 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4394 register_expression_infix_parser(parse_array_expression, '[', 30);
4395 register_expression_infix_parser(parse_call_expression, '(', 30);
4396 register_expression_infix_parser(parse_select_expression, '.', 30);
4397 register_expression_infix_parser(parse_select_expression,
4398 T_MINUSGREATER, 30);
4399 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4401 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4404 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4405 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4406 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4407 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4408 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4409 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4410 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4411 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4412 register_expression_parser(parse_sizeof, T_sizeof, 25);
4413 register_expression_parser(parse_extension, T___extension__, 25);
4414 register_expression_parser(parse_builtin_classify_type,
4415 T___builtin_classify_type, 25);
4418 static asm_constraint_t *parse_asm_constraints(void)
4420 asm_constraint_t *result = NULL;
4421 asm_constraint_t *last = NULL;
4423 while(token.type == T_STRING_LITERAL || token.type == '[') {
4424 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4425 memset(constraint, 0, sizeof(constraint[0]));
4427 if(token.type == '[') {
4429 if(token.type != T_IDENTIFIER) {
4430 parse_error_expected("while parsing asm constraint",
4434 constraint->symbol = token.v.symbol;
4439 constraint->constraints = parse_string_literals();
4441 constraint->expression = parse_expression();
4445 last->next = constraint;
4447 result = constraint;
4451 if(token.type != ',')
4459 static asm_clobber_t *parse_asm_clobbers(void)
4461 asm_clobber_t *result = NULL;
4462 asm_clobber_t *last = NULL;
4464 while(token.type == T_STRING_LITERAL) {
4465 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4466 clobber->clobber = parse_string_literals();
4469 last->next = clobber;
4475 if(token.type != ',')
4483 static statement_t *parse_asm_statement(void)
4487 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4488 statement->base.source_position = token.source_position;
4490 asm_statement_t *asm_statement = &statement->asms;
4492 if(token.type == T_volatile) {
4494 asm_statement->is_volatile = true;
4498 asm_statement->asm_text = parse_string_literals();
4500 if(token.type != ':')
4504 asm_statement->inputs = parse_asm_constraints();
4505 if(token.type != ':')
4509 asm_statement->outputs = parse_asm_constraints();
4510 if(token.type != ':')
4514 asm_statement->clobbers = parse_asm_clobbers();
4522 static statement_t *parse_case_statement(void)
4526 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4528 statement->base.source_position = token.source_position;
4529 statement->case_label.expression = parse_expression();
4532 statement->case_label.label_statement = parse_statement();
4537 static statement_t *parse_default_statement(void)
4541 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4543 statement->base.source_position = token.source_position;
4546 statement->label.label_statement = parse_statement();
4551 static declaration_t *get_label(symbol_t *symbol)
4553 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4554 assert(current_function != NULL);
4555 /* if we found a label in the same function, then we already created the
4557 if(candidate != NULL
4558 && candidate->parent_context == ¤t_function->context) {
4562 /* otherwise we need to create a new one */
4563 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4564 declaration->namespc = NAMESPACE_LABEL;
4565 declaration->symbol = symbol;
4567 label_push(declaration);
4572 static statement_t *parse_label_statement(void)
4574 assert(token.type == T_IDENTIFIER);
4575 symbol_t *symbol = token.v.symbol;
4578 declaration_t *label = get_label(symbol);
4580 /* if source position is already set then the label is defined twice,
4581 * otherwise it was just mentioned in a goto so far */
4582 if(label->source_position.input_name != NULL) {
4583 parser_print_error_prefix();
4584 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4585 parser_print_error_prefix_pos(label->source_position);
4586 fprintf(stderr, "previous definition of '%s' was here\n",
4589 label->source_position = token.source_position;
4592 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4594 label_statement->statement.type = STATEMENT_LABEL;
4595 label_statement->statement.source_position = token.source_position;
4596 label_statement->label = label;
4600 if(token.type == '}') {
4601 parse_error("label at end of compound statement");
4602 return (statement_t*) label_statement;
4604 label_statement->label_statement = parse_statement();
4607 return (statement_t*) label_statement;
4610 static statement_t *parse_if(void)
4614 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4615 statement->statement.type = STATEMENT_IF;
4616 statement->statement.source_position = token.source_position;
4619 statement->condition = parse_expression();
4622 statement->true_statement = parse_statement();
4623 if(token.type == T_else) {
4625 statement->false_statement = parse_statement();
4628 return (statement_t*) statement;
4631 static statement_t *parse_switch(void)
4635 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4636 statement->statement.type = STATEMENT_SWITCH;
4637 statement->statement.source_position = token.source_position;
4640 statement->expression = parse_expression();
4642 statement->body = parse_statement();
4644 return (statement_t*) statement;
4647 static statement_t *parse_while(void)
4651 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4652 statement->statement.type = STATEMENT_WHILE;
4653 statement->statement.source_position = token.source_position;
4656 statement->condition = parse_expression();
4658 statement->body = parse_statement();
4660 return (statement_t*) statement;
4663 static statement_t *parse_do(void)
4667 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4668 statement->statement.type = STATEMENT_DO_WHILE;
4669 statement->statement.source_position = token.source_position;
4671 statement->body = parse_statement();
4674 statement->condition = parse_expression();
4678 return (statement_t*) statement;
4681 static statement_t *parse_for(void)
4685 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4686 statement->statement.type = STATEMENT_FOR;
4687 statement->statement.source_position = token.source_position;
4691 int top = environment_top();
4692 context_t *last_context = context;
4693 set_context(&statement->context);
4695 if(token.type != ';') {
4696 if(is_declaration_specifier(&token, false)) {
4697 parse_declaration(record_declaration);
4699 statement->initialisation = parse_expression();
4706 if(token.type != ';') {
4707 statement->condition = parse_expression();
4710 if(token.type != ')') {
4711 statement->step = parse_expression();
4714 statement->body = parse_statement();
4716 assert(context == &statement->context);
4717 set_context(last_context);
4718 environment_pop_to(top);
4720 return (statement_t*) statement;
4723 static statement_t *parse_goto(void)
4727 if(token.type != T_IDENTIFIER) {
4728 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4732 symbol_t *symbol = token.v.symbol;
4735 declaration_t *label = get_label(symbol);
4737 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4739 statement->statement.type = STATEMENT_GOTO;
4740 statement->statement.source_position = token.source_position;
4742 statement->label = label;
4746 return (statement_t*) statement;
4749 static statement_t *parse_continue(void)
4754 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4755 statement->type = STATEMENT_CONTINUE;
4756 statement->base.source_position = token.source_position;
4761 static statement_t *parse_break(void)
4766 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4767 statement->type = STATEMENT_BREAK;
4768 statement->base.source_position = token.source_position;
4773 static statement_t *parse_return(void)
4777 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4779 statement->statement.type = STATEMENT_RETURN;
4780 statement->statement.source_position = token.source_position;
4782 assert(is_type_function(current_function->type));
4783 function_type_t *function_type = ¤t_function->type->function;
4784 type_t *return_type = function_type->return_type;
4786 expression_t *return_value = NULL;
4787 if(token.type != ';') {
4788 return_value = parse_expression();
4792 if(return_type == NULL)
4793 return (statement_t*) statement;
4795 return_type = skip_typeref(return_type);
4797 if(return_value != NULL) {
4798 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4800 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4801 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4802 parse_warning("'return' with a value, in function returning void");
4803 return_value = NULL;
4805 if(return_type != NULL) {
4806 semantic_assign(return_type, &return_value, "'return'");
4810 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4811 parse_warning("'return' without value, in function returning "
4815 statement->return_value = return_value;
4817 return (statement_t*) statement;
4820 static statement_t *parse_declaration_statement(void)
4822 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4824 statement->base.source_position = token.source_position;
4826 declaration_t *before = last_declaration;
4827 parse_declaration(record_declaration);
4829 if(before == NULL) {
4830 statement->declaration.declarations_begin = context->declarations;
4832 statement->declaration.declarations_begin = before->next;
4834 statement->declaration.declarations_end = last_declaration;
4839 static statement_t *parse_expression_statement(void)
4841 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4843 statement->base.source_position = token.source_position;
4844 statement->expression.expression = parse_expression();
4851 static statement_t *parse_statement(void)
4853 statement_t *statement = NULL;
4855 /* declaration or statement */
4856 switch(token.type) {
4858 statement = parse_asm_statement();
4862 statement = parse_case_statement();
4866 statement = parse_default_statement();
4870 statement = parse_compound_statement();
4874 statement = parse_if();
4878 statement = parse_switch();
4882 statement = parse_while();
4886 statement = parse_do();
4890 statement = parse_for();
4894 statement = parse_goto();
4898 statement = parse_continue();
4902 statement = parse_break();
4906 statement = parse_return();
4915 if(look_ahead(1)->type == ':') {
4916 statement = parse_label_statement();
4920 if(is_typedef_symbol(token.v.symbol)) {
4921 statement = parse_declaration_statement();
4925 statement = parse_expression_statement();
4928 case T___extension__:
4929 /* this can be a prefix to a declaration or an expression statement */
4930 /* we simply eat it now and parse the rest with tail recursion */
4933 } while(token.type == T___extension__);
4934 statement = parse_statement();
4938 statement = parse_declaration_statement();
4942 statement = parse_expression_statement();
4946 assert(statement == NULL
4947 || statement->base.source_position.input_name != NULL);
4952 static statement_t *parse_compound_statement(void)
4954 compound_statement_t *compound_statement
4955 = allocate_ast_zero(sizeof(compound_statement[0]));
4956 compound_statement->statement.type = STATEMENT_COMPOUND;
4957 compound_statement->statement.source_position = token.source_position;
4961 int top = environment_top();
4962 context_t *last_context = context;
4963 set_context(&compound_statement->context);
4965 statement_t *last_statement = NULL;
4967 while(token.type != '}' && token.type != T_EOF) {
4968 statement_t *statement = parse_statement();
4969 if(statement == NULL)
4972 if(last_statement != NULL) {
4973 last_statement->base.next = statement;
4975 compound_statement->statements = statement;
4978 while(statement->base.next != NULL)
4979 statement = statement->base.next;
4981 last_statement = statement;
4984 if(token.type != '}') {
4985 parser_print_error_prefix_pos(
4986 compound_statement->statement.source_position);
4987 fprintf(stderr, "end of file while looking for closing '}'\n");
4991 assert(context == &compound_statement->context);
4992 set_context(last_context);
4993 environment_pop_to(top);
4995 return (statement_t*) compound_statement;
4998 static void initialize_builtins(void)
5000 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5001 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5002 type_size_t = make_global_typedef("__SIZE_TYPE__",
5003 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
5004 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
5005 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
5008 static translation_unit_t *parse_translation_unit(void)
5010 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5012 assert(global_context == NULL);
5013 global_context = &unit->context;
5015 assert(context == NULL);
5016 set_context(&unit->context);
5018 initialize_builtins();
5020 while(token.type != T_EOF) {
5021 parse_external_declaration();
5024 assert(context == &unit->context);
5026 last_declaration = NULL;
5028 assert(global_context == &unit->context);
5029 global_context = NULL;
5034 translation_unit_t *parse(void)
5036 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5037 label_stack = NEW_ARR_F(stack_entry_t, 0);
5038 found_error = false;
5040 type_set_output(stderr);
5041 ast_set_output(stderr);
5043 lookahead_bufpos = 0;
5044 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5047 translation_unit_t *unit = parse_translation_unit();
5049 DEL_ARR_F(environment_stack);
5050 DEL_ARR_F(label_stack);
5058 void init_parser(void)
5060 init_expression_parsers();
5061 obstack_init(&temp_obst);
5063 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5064 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5065 TYPE_QUALIFIER_NONE);
5066 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5067 TYPE_QUALIFIER_NONE);
5068 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5069 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5070 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5071 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5072 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5075 void exit_parser(void)
5077 obstack_free(&temp_obst, NULL);