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_ptr_t = 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_CALL] = sizeof(call_expression_t),
176 [EXPR_UNARY] = sizeof(unary_expression_t),
177 [EXPR_BINARY] = sizeof(binary_expression_t),
178 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
179 [EXPR_SELECT] = sizeof(select_expression_t),
180 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
181 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
182 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
183 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
184 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
185 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
186 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
187 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
188 [EXPR_STATEMENT] = sizeof(statement_expression_t)
190 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
191 assert(type <= EXPR_STATEMENT);
192 assert(sizes[type] != 0);
196 static expression_t *allocate_expression_zero(expression_type_t type)
198 size_t size = get_expression_struct_size(type);
199 expression_t *res = allocate_ast_zero(size);
201 res->base.type = type;
205 static size_t get_type_struct_size(type_type_t type)
207 static const size_t sizes[] = {
208 [TYPE_ATOMIC] = sizeof(atomic_type_t),
209 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
210 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
211 [TYPE_ENUM] = sizeof(enum_type_t),
212 [TYPE_FUNCTION] = sizeof(function_type_t),
213 [TYPE_POINTER] = sizeof(pointer_type_t),
214 [TYPE_ARRAY] = sizeof(array_type_t),
215 [TYPE_BUILTIN] = sizeof(builtin_type_t),
216 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
217 [TYPE_TYPEOF] = sizeof(typeof_type_t),
219 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
220 assert(type <= TYPE_TYPEOF);
221 assert(sizes[type] != 0);
225 static type_t *allocate_type_zero(type_type_t type)
227 size_t size = get_type_struct_size(type);
228 type_t *res = obstack_alloc(type_obst, size);
229 memset(res, 0, size);
231 res->base.type = type;
235 static size_t get_initializer_size(initializer_type_t type)
237 static const size_t sizes[] = {
238 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
239 [INITIALIZER_STRING] = sizeof(initializer_string_t),
240 [INITIALIZER_LIST] = sizeof(initializer_list_t)
242 assert(type < INITIALIZER_COUNT);
243 assert(sizes[type] != 0);
247 static initializer_t *allocate_initializer(initializer_type_t type)
249 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
255 static void free_type(void *type)
257 obstack_free(type_obst, type);
261 * returns the top element of the environment stack
263 static size_t environment_top(void)
265 return ARR_LEN(environment_stack);
268 static size_t label_top(void)
270 return ARR_LEN(label_stack);
275 static inline void next_token(void)
277 token = lookahead_buffer[lookahead_bufpos];
278 lookahead_buffer[lookahead_bufpos] = lexer_token;
281 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
284 print_token(stderr, &token);
285 fprintf(stderr, "\n");
289 static inline const token_t *look_ahead(int num)
291 assert(num > 0 && num <= MAX_LOOKAHEAD);
292 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
293 return &lookahead_buffer[pos];
296 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
298 static void error(void)
301 #ifdef ABORT_ON_ERROR
306 static void parser_print_prefix_pos(const source_position_t source_position)
308 fputs(source_position.input_name, stderr);
310 fprintf(stderr, "%u", source_position.linenr);
314 static void parser_print_error_prefix_pos(
315 const source_position_t source_position)
317 parser_print_prefix_pos(source_position);
318 fputs("error: ", stderr);
322 static void parser_print_error_prefix(void)
324 parser_print_error_prefix_pos(token.source_position);
327 static void parse_error(const char *message)
329 parser_print_error_prefix();
330 fprintf(stderr, "parse error: %s\n", message);
333 static void parser_print_warning_prefix_pos(
334 const source_position_t source_position)
336 parser_print_prefix_pos(source_position);
337 fputs("warning: ", stderr);
340 static void parser_print_warning_prefix(void)
342 parser_print_warning_prefix_pos(token.source_position);
345 static void parse_warning_pos(const source_position_t source_position,
346 const char *const message)
348 parser_print_prefix_pos(source_position);
349 fprintf(stderr, "warning: %s\n", message);
352 static void parse_warning(const char *message)
354 parse_warning_pos(token.source_position, message);
357 static void parse_error_expected(const char *message, ...)
362 if(message != NULL) {
363 parser_print_error_prefix();
364 fprintf(stderr, "%s\n", message);
366 parser_print_error_prefix();
367 fputs("Parse error: got ", stderr);
368 print_token(stderr, &token);
369 fputs(", expected ", stderr);
371 va_start(args, message);
372 token_type_t token_type = va_arg(args, token_type_t);
373 while(token_type != 0) {
377 fprintf(stderr, ", ");
379 print_token_type(stderr, token_type);
380 token_type = va_arg(args, token_type_t);
383 fprintf(stderr, "\n");
386 static void print_type_quoted(type_t *type)
393 static void type_error(const char *msg, const source_position_t source_position,
396 parser_print_error_prefix_pos(source_position);
397 fprintf(stderr, "%s, but found type ", msg);
398 print_type_quoted(type);
402 static void type_error_incompatible(const char *msg,
403 const source_position_t source_position, type_t *type1, type_t *type2)
405 parser_print_error_prefix_pos(source_position);
406 fprintf(stderr, "%s, incompatible types: ", msg);
407 print_type_quoted(type1);
408 fprintf(stderr, " - ");
409 print_type_quoted(type2);
410 fprintf(stderr, ")\n");
413 static void eat_block(void)
415 if(token.type == '{')
418 while(token.type != '}') {
419 if(token.type == T_EOF)
421 if(token.type == '{') {
430 static void eat_statement(void)
432 while(token.type != ';') {
433 if(token.type == T_EOF)
435 if(token.type == '}')
437 if(token.type == '{') {
446 static void eat_brace(void)
448 if(token.type == '(')
451 while(token.type != ')') {
452 if(token.type == T_EOF)
454 if(token.type == ')' || token.type == ';' || token.type == '}') {
457 if(token.type == '(') {
461 if(token.type == '{') {
470 #define expect(expected) \
471 if(UNLIKELY(token.type != (expected))) { \
472 parse_error_expected(NULL, (expected), 0); \
478 #define expect_block(expected) \
479 if(UNLIKELY(token.type != (expected))) { \
480 parse_error_expected(NULL, (expected), 0); \
486 #define expect_void(expected) \
487 if(UNLIKELY(token.type != (expected))) { \
488 parse_error_expected(NULL, (expected), 0); \
494 static void set_context(context_t *new_context)
496 context = new_context;
498 last_declaration = new_context->declarations;
499 if(last_declaration != NULL) {
500 while(last_declaration->next != NULL) {
501 last_declaration = last_declaration->next;
507 * called when we find a 2nd declarator for an identifier we already have a
510 static bool is_compatible_declaration(declaration_t *declaration,
511 declaration_t *previous)
513 /* happens for K&R style function parameters */
514 if(previous->type == NULL) {
515 previous->type = declaration->type;
519 if (declaration->type->type == TYPE_FUNCTION &&
520 previous->type->type == TYPE_FUNCTION &&
521 previous->type->function.unspecified_parameters) {
522 function_type_t* const prev_func = &previous->type->function;
523 function_type_t* const decl_func = &declaration->type->function;
524 if (prev_func->unspecified_parameters &&
525 prev_func->result_type == decl_func->result_type) {
526 declaration->type = previous->type;
530 /* TODO: not correct yet */
531 return declaration->type == previous->type;
534 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
536 declaration_t *declaration = symbol->declaration;
537 for( ; declaration != NULL; declaration = declaration->symbol_next) {
538 if(declaration->namespc == namespc)
545 static const char *get_namespace_prefix(namespace_t namespc)
548 case NAMESPACE_NORMAL:
550 case NAMESPACE_UNION:
552 case NAMESPACE_STRUCT:
556 case NAMESPACE_LABEL:
559 panic("invalid namespace found");
563 * pushs an environment_entry on the environment stack and links the
564 * corresponding symbol to the new entry
566 static declaration_t *stack_push(stack_entry_t **stack_ptr,
567 declaration_t *declaration,
568 context_t *parent_context)
570 symbol_t *symbol = declaration->symbol;
571 namespace_t namespc = (namespace_t)declaration->namespc;
573 /* a declaration should be only pushed once */
574 declaration->parent_context = parent_context;
576 declaration_t *previous_declaration = get_declaration(symbol, namespc);
577 assert(declaration != previous_declaration);
578 if(previous_declaration != NULL
579 && previous_declaration->parent_context == context) {
580 if(!is_compatible_declaration(declaration, previous_declaration)) {
581 parser_print_error_prefix_pos(declaration->source_position);
582 fprintf(stderr, "definition of symbol %s%s with type ",
583 get_namespace_prefix(namespc), symbol->string);
584 print_type_quoted(declaration->type);
586 parser_print_error_prefix_pos(
587 previous_declaration->source_position);
588 fprintf(stderr, "is incompatible with previous declaration "
590 print_type_quoted(previous_declaration->type);
593 unsigned old_storage_class = previous_declaration->storage_class;
594 unsigned new_storage_class = declaration->storage_class;
595 if (current_function == NULL) {
596 if (old_storage_class != STORAGE_CLASS_STATIC &&
597 new_storage_class == STORAGE_CLASS_STATIC) {
598 parser_print_error_prefix_pos(declaration->source_position);
600 "static declaration of '%s' follows non-static declaration\n",
602 parser_print_error_prefix_pos(previous_declaration->source_position);
603 fprintf(stderr, "previous declaration of '%s' was here\n",
606 if (old_storage_class == STORAGE_CLASS_EXTERN) {
607 if (new_storage_class == STORAGE_CLASS_NONE) {
608 previous_declaration->storage_class = STORAGE_CLASS_NONE;
611 parser_print_warning_prefix_pos(declaration->source_position);
612 fprintf(stderr, "redundant declaration for '%s'\n",
614 parser_print_warning_prefix_pos(previous_declaration->source_position);
615 fprintf(stderr, "previous declaration of '%s' was here\n",
620 if (old_storage_class == STORAGE_CLASS_EXTERN &&
621 new_storage_class == STORAGE_CLASS_EXTERN) {
622 parser_print_warning_prefix_pos(declaration->source_position);
623 fprintf(stderr, "redundant extern declaration for '%s'\n",
625 parser_print_warning_prefix_pos(previous_declaration->source_position);
626 fprintf(stderr, "previous declaration of '%s' was here\n",
629 parser_print_error_prefix_pos(declaration->source_position);
630 if (old_storage_class == new_storage_class) {
631 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
633 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
635 parser_print_error_prefix_pos(previous_declaration->source_position);
636 fprintf(stderr, "previous declaration of '%s' was here\n",
641 return previous_declaration;
644 /* remember old declaration */
646 entry.symbol = symbol;
647 entry.old_declaration = symbol->declaration;
648 entry.namespc = (unsigned short) namespc;
649 ARR_APP1(stack_entry_t, *stack_ptr, entry);
651 /* replace/add declaration into declaration list of the symbol */
652 if(symbol->declaration == NULL) {
653 symbol->declaration = declaration;
655 declaration_t *iter_last = NULL;
656 declaration_t *iter = symbol->declaration;
657 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
658 /* replace an entry? */
659 if(iter->namespc == namespc) {
660 if(iter_last == NULL) {
661 symbol->declaration = declaration;
663 iter_last->symbol_next = declaration;
665 declaration->symbol_next = iter->symbol_next;
670 assert(iter_last->symbol_next == NULL);
671 iter_last->symbol_next = declaration;
678 static declaration_t *environment_push(declaration_t *declaration)
680 assert(declaration->source_position.input_name != NULL);
681 return stack_push(&environment_stack, declaration, context);
684 static declaration_t *label_push(declaration_t *declaration)
686 return stack_push(&label_stack, declaration, ¤t_function->context);
690 * pops symbols from the environment stack until @p new_top is the top element
692 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
694 stack_entry_t *stack = *stack_ptr;
695 size_t top = ARR_LEN(stack);
698 assert(new_top <= top);
702 for(i = top; i > new_top; --i) {
703 stack_entry_t *entry = &stack[i - 1];
705 declaration_t *old_declaration = entry->old_declaration;
706 symbol_t *symbol = entry->symbol;
707 namespace_t namespc = (namespace_t)entry->namespc;
709 /* replace/remove declaration */
710 declaration_t *declaration = symbol->declaration;
711 assert(declaration != NULL);
712 if(declaration->namespc == namespc) {
713 if(old_declaration == NULL) {
714 symbol->declaration = declaration->symbol_next;
716 symbol->declaration = old_declaration;
719 declaration_t *iter_last = declaration;
720 declaration_t *iter = declaration->symbol_next;
721 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
722 /* replace an entry? */
723 if(iter->namespc == namespc) {
724 assert(iter_last != NULL);
725 iter_last->symbol_next = old_declaration;
726 old_declaration->symbol_next = iter->symbol_next;
730 assert(iter != NULL);
734 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
737 static void environment_pop_to(size_t new_top)
739 stack_pop_to(&environment_stack, new_top);
742 static void label_pop_to(size_t new_top)
744 stack_pop_to(&label_stack, new_top);
748 static int get_rank(const type_t *type)
750 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
751 * and esp. footnote 108). However we can't fold constants (yet), so we
752 * can't decide wether unsigned int is possible, while int always works.
753 * (unsigned int would be preferable when possible... for stuff like
754 * struct { enum { ... } bla : 4; } ) */
755 if(type->type == TYPE_ENUM)
756 return ATOMIC_TYPE_INT;
758 assert(type->type == TYPE_ATOMIC);
759 const atomic_type_t *atomic_type = &type->atomic;
760 atomic_type_type_t atype = atomic_type->atype;
764 static type_t *promote_integer(type_t *type)
766 if(get_rank(type) < ATOMIC_TYPE_INT)
772 static expression_t *create_cast_expression(expression_t *expression,
775 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
777 cast->unary.type = UNEXPR_CAST_IMPLICIT;
778 cast->unary.value = expression;
779 cast->base.datatype = dest_type;
784 static bool is_null_pointer_constant(const expression_t *expression)
786 /* skip void* cast */
787 if(expression->type == EXPR_UNARY) {
788 const unary_expression_t *unary = &expression->unary;
789 if(unary->type == UNEXPR_CAST
790 && expression->base.datatype == type_void_ptr) {
791 expression = unary->value;
795 /* TODO: not correct yet, should be any constant integer expression
796 * which evaluates to 0 */
797 if (expression->type != EXPR_CONST)
800 type_t *const type = skip_typeref(expression->base.datatype);
801 if (!is_type_integer(type))
804 return expression->conste.v.int_value == 0;
807 static expression_t *create_implicit_cast(expression_t *expression,
810 type_t *source_type = expression->base.datatype;
812 if(source_type == NULL)
815 source_type = skip_typeref(source_type);
816 dest_type = skip_typeref(dest_type);
818 if(source_type == dest_type)
821 switch (dest_type->type) {
823 /* TODO warning for implicitly converting to enum */
825 if (source_type->type != TYPE_ATOMIC &&
826 source_type->type != TYPE_ENUM) {
827 panic("casting of non-atomic types not implemented yet");
830 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
831 type_error_incompatible("can't cast types",
832 expression->base.source_position, source_type,
837 return create_cast_expression(expression, dest_type);
840 switch (source_type->type) {
842 if (is_null_pointer_constant(expression)) {
843 return create_cast_expression(expression, dest_type);
848 if (pointers_compatible(source_type, dest_type)) {
849 return create_cast_expression(expression, dest_type);
854 array_type_t *array_type = &source_type->array;
855 pointer_type_t *pointer_type = &dest_type->pointer;
856 if (types_compatible(array_type->element_type,
857 pointer_type->points_to)) {
858 return create_cast_expression(expression, dest_type);
864 panic("casting of non-atomic types not implemented yet");
867 type_error_incompatible("can't implicitly cast types",
868 expression->base.source_position, source_type, dest_type);
872 panic("casting of non-atomic types not implemented yet");
876 /** Implements the rules from § 6.5.16.1 */
877 static void semantic_assign(type_t *orig_type_left, expression_t **right,
880 type_t *orig_type_right = (*right)->base.datatype;
882 if(orig_type_right == NULL)
885 type_t *const type_left = skip_typeref(orig_type_left);
886 type_t *const type_right = skip_typeref(orig_type_right);
888 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
889 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
890 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
891 && is_type_pointer(type_right))) {
892 *right = create_implicit_cast(*right, type_left);
896 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
897 pointer_type_t *pointer_type_left = &type_left->pointer;
898 pointer_type_t *pointer_type_right = &type_right->pointer;
899 type_t *points_to_left = pointer_type_left->points_to;
900 type_t *points_to_right = pointer_type_right->points_to;
902 points_to_left = skip_typeref(points_to_left);
903 points_to_right = skip_typeref(points_to_right);
905 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
906 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
907 && !types_compatible(points_to_left, points_to_right)) {
908 goto incompatible_assign_types;
911 /* the left type has all qualifiers from the right type */
912 unsigned missing_qualifiers
913 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
914 if(missing_qualifiers != 0) {
915 parser_print_error_prefix();
916 fprintf(stderr, "destination type ");
917 print_type_quoted(type_left);
918 fprintf(stderr, " in %s from type ", context);
919 print_type_quoted(type_right);
920 fprintf(stderr, " lacks qualifiers '");
921 print_type_qualifiers(missing_qualifiers);
922 fprintf(stderr, "' in pointed-to type\n");
926 *right = create_implicit_cast(*right, type_left);
930 if (is_type_compound(type_left)
931 && types_compatible(type_left, type_right)) {
932 *right = create_implicit_cast(*right, type_left);
936 incompatible_assign_types:
937 /* TODO: improve error message */
938 parser_print_error_prefix();
939 fprintf(stderr, "incompatible types in %s\n", context);
940 parser_print_error_prefix();
941 print_type_quoted(orig_type_left);
942 fputs(" <- ", stderr);
943 print_type_quoted(orig_type_right);
947 static expression_t *parse_constant_expression(void)
949 /* start parsing at precedence 7 (conditional expression) */
950 return parse_sub_expression(7);
953 static expression_t *parse_assignment_expression(void)
955 /* start parsing at precedence 2 (assignment expression) */
956 return parse_sub_expression(2);
959 static type_t *make_global_typedef(const char *name, type_t *type)
961 symbol_t *symbol = symbol_table_insert(name);
963 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
964 declaration->namespc = NAMESPACE_NORMAL;
965 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
966 declaration->type = type;
967 declaration->symbol = symbol;
968 declaration->source_position = builtin_source_position;
970 record_declaration(declaration);
972 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
973 typedef_type->typedeft.declaration = declaration;
978 static const char *parse_string_literals(void)
980 assert(token.type == T_STRING_LITERAL);
981 const char *result = token.v.string;
985 while(token.type == T_STRING_LITERAL) {
986 result = concat_strings(result, token.v.string);
993 static void parse_attributes(void)
997 case T___attribute__: {
1003 switch(token.type) {
1005 parse_error("EOF while parsing attribute");
1024 if(token.type != T_STRING_LITERAL) {
1025 parse_error_expected("while parsing assembler attribute",
1030 parse_string_literals();
1035 goto attributes_finished;
1039 attributes_finished:
1044 static designator_t *parse_designation(void)
1046 if(token.type != '[' && token.type != '.')
1049 designator_t *result = NULL;
1050 designator_t *last = NULL;
1053 designator_t *designator;
1054 switch(token.type) {
1056 designator = allocate_ast_zero(sizeof(designator[0]));
1058 designator->array_access = parse_constant_expression();
1062 designator = allocate_ast_zero(sizeof(designator[0]));
1064 if(token.type != T_IDENTIFIER) {
1065 parse_error_expected("while parsing designator",
1069 designator->symbol = token.v.symbol;
1077 assert(designator != NULL);
1079 last->next = designator;
1081 result = designator;
1088 static initializer_t *initializer_from_string(array_type_t *type,
1091 /* TODO: check len vs. size of array type */
1094 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1095 initializer->string.string = string;
1100 static initializer_t *initializer_from_expression(type_t *type,
1101 expression_t *expression)
1103 /* TODO check that expression is a constant expression */
1105 /* § 6.7.8.14/15 char array may be initialized by string literals */
1106 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
1107 array_type_t *array_type = &type->array;
1108 type_t *element_type = array_type->element_type;
1110 if(element_type->type == TYPE_ATOMIC) {
1111 atomic_type_t *atomic_type = &element_type->atomic;
1112 atomic_type_type_t atype = atomic_type->atype;
1114 /* TODO handle wide strings */
1115 if(atype == ATOMIC_TYPE_CHAR
1116 || atype == ATOMIC_TYPE_SCHAR
1117 || atype == ATOMIC_TYPE_UCHAR) {
1119 string_literal_expression_t *literal = &expression->string;
1120 return initializer_from_string(array_type, literal->value);
1125 semantic_assign(type, &expression, "initializer");
1127 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1128 result->value.value = expression;
1133 static initializer_t *parse_sub_initializer(type_t *type,
1134 expression_t *expression,
1135 type_t *expression_type);
1137 static initializer_t *parse_sub_initializer_elem(type_t *type)
1139 if(token.type == '{') {
1140 return parse_sub_initializer(type, NULL, NULL);
1143 expression_t *expression = parse_assignment_expression();
1144 type_t *expression_type = skip_typeref(expression->base.datatype);
1146 return parse_sub_initializer(type, expression, expression_type);
1149 static bool had_initializer_brace_warning;
1151 static initializer_t *parse_sub_initializer(type_t *type,
1152 expression_t *expression,
1153 type_t *expression_type)
1155 if(is_type_scalar(type)) {
1156 /* there might be extra {} hierarchies */
1157 if(token.type == '{') {
1159 if(!had_initializer_brace_warning) {
1160 parse_warning("braces around scalar initializer");
1161 had_initializer_brace_warning = true;
1163 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1164 if(token.type == ',') {
1166 /* TODO: warn about excessive elements */
1172 if(expression == NULL) {
1173 expression = parse_assignment_expression();
1175 return initializer_from_expression(type, expression);
1178 /* TODO: ignore qualifiers, comparing pointers is probably
1180 if(expression != NULL && expression_type == type) {
1181 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1184 semantic_assign(type, &expression, "initializer");
1186 result->value.value = expression;
1191 bool read_paren = false;
1192 if(token.type == '{') {
1197 /* descend into subtype */
1198 initializer_t *result = NULL;
1199 initializer_t **elems;
1200 if(type->type == TYPE_ARRAY) {
1201 array_type_t *array_type = &type->array;
1202 type_t *element_type = array_type->element_type;
1203 element_type = skip_typeref(element_type);
1206 had_initializer_brace_warning = false;
1207 if(expression == NULL) {
1208 sub = parse_sub_initializer_elem(element_type);
1210 sub = parse_sub_initializer(element_type, expression,
1214 /* didn't match the subtypes -> try the parent type */
1216 assert(!read_paren);
1220 elems = NEW_ARR_F(initializer_t*, 0);
1221 ARR_APP1(initializer_t*, elems, sub);
1224 if(token.type == '}')
1227 if(token.type == '}')
1230 sub = parse_sub_initializer(element_type, NULL, NULL);
1232 /* TODO error, do nicer cleanup */
1233 parse_error("member initializer didn't match");
1237 ARR_APP1(initializer_t*, elems, sub);
1240 assert(type->type == TYPE_COMPOUND_STRUCT
1241 || type->type == TYPE_COMPOUND_UNION);
1242 compound_type_t *compound_type = &type->compound;
1243 context_t *context = &compound_type->declaration->context;
1245 declaration_t *first = context->declarations;
1248 type_t *first_type = first->type;
1249 first_type = skip_typeref(first_type);
1252 had_initializer_brace_warning = false;
1253 if(expression == NULL) {
1254 sub = parse_sub_initializer_elem(first_type);
1256 sub = parse_sub_initializer(first_type, expression,expression_type);
1259 /* didn't match the subtypes -> try our parent type */
1261 assert(!read_paren);
1265 elems = NEW_ARR_F(initializer_t*, 0);
1266 ARR_APP1(initializer_t*, elems, sub);
1268 declaration_t *iter = first->next;
1269 for( ; iter != NULL; iter = iter->next) {
1270 if(iter->symbol == NULL)
1272 if(iter->namespc != NAMESPACE_NORMAL)
1275 if(token.type == '}')
1278 if(token.type == '}')
1281 type_t *iter_type = iter->type;
1282 iter_type = skip_typeref(iter_type);
1284 sub = parse_sub_initializer(iter_type, NULL, NULL);
1286 /* TODO error, do nicer cleanup*/
1287 parse_error("member initializer didn't match");
1291 ARR_APP1(initializer_t*, elems, sub);
1295 int len = ARR_LEN(elems);
1296 size_t elems_size = sizeof(initializer_t*) * len;
1298 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1300 init->initializer.type = INITIALIZER_LIST;
1302 memcpy(init->initializers, elems, elems_size);
1305 result = (initializer_t*) init;
1308 if(token.type == ',')
1315 static initializer_t *parse_initializer(type_t *type)
1317 initializer_t *result;
1319 type = skip_typeref(type);
1321 if(token.type != '{') {
1322 expression_t *expression = parse_assignment_expression();
1323 return initializer_from_expression(type, expression);
1326 if(is_type_scalar(type)) {
1330 expression_t *expression = parse_assignment_expression();
1331 result = initializer_from_expression(type, expression);
1333 if(token.type == ',')
1339 result = parse_sub_initializer(type, NULL, NULL);
1347 static declaration_t *parse_compound_type_specifier(bool is_struct)
1355 symbol_t *symbol = NULL;
1356 declaration_t *declaration = NULL;
1358 if (token.type == T___attribute__) {
1363 if(token.type == T_IDENTIFIER) {
1364 symbol = token.v.symbol;
1368 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1370 declaration = get_declaration(symbol, NAMESPACE_UNION);
1372 } else if(token.type != '{') {
1374 parse_error_expected("while parsing struct type specifier",
1375 T_IDENTIFIER, '{', 0);
1377 parse_error_expected("while parsing union type specifier",
1378 T_IDENTIFIER, '{', 0);
1384 if(declaration == NULL) {
1385 declaration = allocate_ast_zero(sizeof(declaration[0]));
1388 declaration->namespc = NAMESPACE_STRUCT;
1390 declaration->namespc = NAMESPACE_UNION;
1392 declaration->source_position = token.source_position;
1393 declaration->symbol = symbol;
1394 record_declaration(declaration);
1397 if(token.type == '{') {
1398 if(declaration->init.is_defined) {
1399 assert(symbol != NULL);
1400 parser_print_error_prefix();
1401 fprintf(stderr, "multiple definition of %s %s\n",
1402 is_struct ? "struct" : "union", symbol->string);
1403 declaration->context.declarations = NULL;
1405 declaration->init.is_defined = true;
1407 int top = environment_top();
1408 context_t *last_context = context;
1409 set_context(&declaration->context);
1411 parse_compound_type_entries();
1414 assert(context == &declaration->context);
1415 set_context(last_context);
1416 environment_pop_to(top);
1422 static void parse_enum_entries(enum_type_t *const enum_type)
1426 if(token.type == '}') {
1428 parse_error("empty enum not allowed");
1433 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1435 if(token.type != T_IDENTIFIER) {
1436 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1440 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1441 entry->type = (type_t*) enum_type;
1442 entry->symbol = token.v.symbol;
1443 entry->source_position = token.source_position;
1446 if(token.type == '=') {
1448 entry->init.enum_value = parse_constant_expression();
1453 record_declaration(entry);
1455 if(token.type != ',')
1458 } while(token.type != '}');
1463 static type_t *parse_enum_specifier(void)
1467 declaration_t *declaration;
1470 if(token.type == T_IDENTIFIER) {
1471 symbol = token.v.symbol;
1474 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1475 } else if(token.type != '{') {
1476 parse_error_expected("while parsing enum type specifier",
1477 T_IDENTIFIER, '{', 0);
1484 if(declaration == NULL) {
1485 declaration = allocate_ast_zero(sizeof(declaration[0]));
1487 declaration->namespc = NAMESPACE_ENUM;
1488 declaration->source_position = token.source_position;
1489 declaration->symbol = symbol;
1492 type_t *const type = allocate_type_zero(TYPE_ENUM);
1493 type->enumt.declaration = declaration;
1495 if(token.type == '{') {
1496 if(declaration->init.is_defined) {
1497 parser_print_error_prefix();
1498 fprintf(stderr, "multiple definitions of enum %s\n",
1501 record_declaration(declaration);
1502 declaration->init.is_defined = 1;
1504 parse_enum_entries(&type->enumt);
1512 * if a symbol is a typedef to another type, return true
1514 static bool is_typedef_symbol(symbol_t *symbol)
1516 const declaration_t *const declaration =
1517 get_declaration(symbol, NAMESPACE_NORMAL);
1519 declaration != NULL &&
1520 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1523 static type_t *parse_typeof(void)
1531 expression_t *expression = NULL;
1534 switch(token.type) {
1535 case T___extension__:
1536 /* this can be a prefix to a typename or an expression */
1537 /* we simply eat it now. */
1540 } while(token.type == T___extension__);
1544 if(is_typedef_symbol(token.v.symbol)) {
1545 type = parse_typename();
1547 expression = parse_expression();
1548 type = expression->base.datatype;
1553 type = parse_typename();
1557 expression = parse_expression();
1558 type = expression->base.datatype;
1564 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1565 typeof_type->typeoft.expression = expression;
1566 typeof_type->typeoft.typeof_type = type;
1572 SPECIFIER_SIGNED = 1 << 0,
1573 SPECIFIER_UNSIGNED = 1 << 1,
1574 SPECIFIER_LONG = 1 << 2,
1575 SPECIFIER_INT = 1 << 3,
1576 SPECIFIER_DOUBLE = 1 << 4,
1577 SPECIFIER_CHAR = 1 << 5,
1578 SPECIFIER_SHORT = 1 << 6,
1579 SPECIFIER_LONG_LONG = 1 << 7,
1580 SPECIFIER_FLOAT = 1 << 8,
1581 SPECIFIER_BOOL = 1 << 9,
1582 SPECIFIER_VOID = 1 << 10,
1583 #ifdef PROVIDE_COMPLEX
1584 SPECIFIER_COMPLEX = 1 << 11,
1585 SPECIFIER_IMAGINARY = 1 << 12,
1589 static type_t *create_builtin_type(symbol_t *symbol)
1591 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1592 type->builtin.symbol = symbol;
1594 type->builtin.real_type = type_int;
1599 static type_t *get_typedef_type(symbol_t *symbol)
1601 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1602 if(declaration == NULL
1603 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1606 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1607 type->typedeft.declaration = declaration;
1612 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1614 type_t *type = NULL;
1615 unsigned type_qualifiers = 0;
1616 unsigned type_specifiers = 0;
1619 specifiers->source_position = token.source_position;
1622 switch(token.type) {
1625 #define MATCH_STORAGE_CLASS(token, class) \
1627 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1628 parse_error("multiple storage classes in declaration " \
1631 specifiers->storage_class = class; \
1635 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1636 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1637 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1638 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1639 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1642 switch (specifiers->storage_class) {
1643 case STORAGE_CLASS_NONE:
1644 specifiers->storage_class = STORAGE_CLASS_THREAD;
1647 case STORAGE_CLASS_EXTERN:
1648 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1651 case STORAGE_CLASS_STATIC:
1652 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1656 parse_error("multiple storage classes in declaration specifiers");
1662 /* type qualifiers */
1663 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1665 type_qualifiers |= qualifier; \
1669 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1670 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1671 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1673 case T___extension__:
1678 /* type specifiers */
1679 #define MATCH_SPECIFIER(token, specifier, name) \
1682 if(type_specifiers & specifier) { \
1683 parse_error("multiple " name " type specifiers given"); \
1685 type_specifiers |= specifier; \
1689 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1690 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1691 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1692 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1693 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1694 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1695 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1696 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1697 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1698 #ifdef PROVIDE_COMPLEX
1699 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1700 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1704 specifiers->is_inline = true;
1709 if(type_specifiers & SPECIFIER_LONG_LONG) {
1710 parse_error("multiple type specifiers given");
1711 } else if(type_specifiers & SPECIFIER_LONG) {
1712 type_specifiers |= SPECIFIER_LONG_LONG;
1714 type_specifiers |= SPECIFIER_LONG;
1718 /* TODO: if type != NULL for the following rules should issue
1721 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1723 type->compound.declaration = parse_compound_type_specifier(true);
1727 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1729 type->compound.declaration = parse_compound_type_specifier(false);
1733 type = parse_enum_specifier();
1736 type = parse_typeof();
1738 case T___builtin_va_list:
1739 type = create_builtin_type(token.v.symbol);
1743 case T___attribute__:
1748 case T_IDENTIFIER: {
1749 type_t *typedef_type = get_typedef_type(token.v.symbol);
1751 if(typedef_type == NULL)
1752 goto finish_specifiers;
1755 type = typedef_type;
1759 /* function specifier */
1761 goto finish_specifiers;
1768 atomic_type_type_t atomic_type;
1770 /* match valid basic types */
1771 switch(type_specifiers) {
1772 case SPECIFIER_VOID:
1773 atomic_type = ATOMIC_TYPE_VOID;
1775 case SPECIFIER_CHAR:
1776 atomic_type = ATOMIC_TYPE_CHAR;
1778 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1779 atomic_type = ATOMIC_TYPE_SCHAR;
1781 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1782 atomic_type = ATOMIC_TYPE_UCHAR;
1784 case SPECIFIER_SHORT:
1785 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1786 case SPECIFIER_SHORT | SPECIFIER_INT:
1787 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1788 atomic_type = ATOMIC_TYPE_SHORT;
1790 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1791 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1792 atomic_type = ATOMIC_TYPE_USHORT;
1795 case SPECIFIER_SIGNED:
1796 case SPECIFIER_SIGNED | SPECIFIER_INT:
1797 atomic_type = ATOMIC_TYPE_INT;
1799 case SPECIFIER_UNSIGNED:
1800 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1801 atomic_type = ATOMIC_TYPE_UINT;
1803 case SPECIFIER_LONG:
1804 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1805 case SPECIFIER_LONG | SPECIFIER_INT:
1806 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1807 atomic_type = ATOMIC_TYPE_LONG;
1809 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1810 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1811 atomic_type = ATOMIC_TYPE_ULONG;
1813 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1814 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1815 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1816 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1818 atomic_type = ATOMIC_TYPE_LONGLONG;
1820 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1821 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1823 atomic_type = ATOMIC_TYPE_ULONGLONG;
1825 case SPECIFIER_FLOAT:
1826 atomic_type = ATOMIC_TYPE_FLOAT;
1828 case SPECIFIER_DOUBLE:
1829 atomic_type = ATOMIC_TYPE_DOUBLE;
1831 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1832 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1834 case SPECIFIER_BOOL:
1835 atomic_type = ATOMIC_TYPE_BOOL;
1837 #ifdef PROVIDE_COMPLEX
1838 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1839 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1841 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1842 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1844 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1845 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1847 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1848 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1850 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1851 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1853 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1854 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1858 /* invalid specifier combination, give an error message */
1859 if(type_specifiers == 0) {
1861 parse_warning("no type specifiers in declaration, using int");
1862 atomic_type = ATOMIC_TYPE_INT;
1865 parse_error("no type specifiers given in declaration");
1867 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1868 (type_specifiers & SPECIFIER_UNSIGNED)) {
1869 parse_error("signed and unsigned specifiers gives");
1870 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1871 parse_error("only integer types can be signed or unsigned");
1873 parse_error("multiple datatypes in declaration");
1875 atomic_type = ATOMIC_TYPE_INVALID;
1878 type = allocate_type_zero(TYPE_ATOMIC);
1879 type->atomic.atype = atomic_type;
1882 if(type_specifiers != 0) {
1883 parse_error("multiple datatypes in declaration");
1887 type->base.qualifiers = type_qualifiers;
1889 type_t *result = typehash_insert(type);
1890 if(newtype && result != type) {
1894 specifiers->type = result;
1897 static type_qualifiers_t parse_type_qualifiers(void)
1899 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1902 switch(token.type) {
1903 /* type qualifiers */
1904 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1905 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1906 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1909 return type_qualifiers;
1914 static declaration_t *parse_identifier_list(void)
1916 declaration_t *declarations = NULL;
1917 declaration_t *last_declaration = NULL;
1919 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1921 declaration->source_position = token.source_position;
1922 declaration->symbol = token.v.symbol;
1925 if(last_declaration != NULL) {
1926 last_declaration->next = declaration;
1928 declarations = declaration;
1930 last_declaration = declaration;
1932 if(token.type != ',')
1935 } while(token.type == T_IDENTIFIER);
1937 return declarations;
1940 static declaration_t *parse_parameter(void)
1942 declaration_specifiers_t specifiers;
1943 memset(&specifiers, 0, sizeof(specifiers));
1945 parse_declaration_specifiers(&specifiers);
1947 declaration_t *declaration = parse_declarator(&specifiers, true);
1949 /* TODO check declaration constraints for parameters */
1950 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1951 parse_error("typedef not allowed in parameter list");
1954 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1956 if (declaration->type->type == TYPE_ARRAY) {
1957 const array_type_t *const arr_type = &declaration->type->array;
1958 type_t *element_type = arr_type->element_type;
1959 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1965 static declaration_t *parse_parameters(function_type_t *type)
1967 if(token.type == T_IDENTIFIER) {
1968 symbol_t *symbol = token.v.symbol;
1969 if(!is_typedef_symbol(symbol)) {
1970 type->kr_style_parameters = true;
1971 return parse_identifier_list();
1975 if(token.type == ')') {
1976 type->unspecified_parameters = 1;
1979 if(token.type == T_void && look_ahead(1)->type == ')') {
1984 declaration_t *declarations = NULL;
1985 declaration_t *declaration;
1986 declaration_t *last_declaration = NULL;
1987 function_parameter_t *parameter;
1988 function_parameter_t *last_parameter = NULL;
1991 switch(token.type) {
1995 return declarations;
1998 case T___extension__:
2000 declaration = parse_parameter();
2002 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2003 memset(parameter, 0, sizeof(parameter[0]));
2004 parameter->type = declaration->type;
2006 if(last_parameter != NULL) {
2007 last_declaration->next = declaration;
2008 last_parameter->next = parameter;
2010 type->parameters = parameter;
2011 declarations = declaration;
2013 last_parameter = parameter;
2014 last_declaration = declaration;
2018 return declarations;
2020 if(token.type != ',')
2021 return declarations;
2031 } construct_type_type_t;
2033 typedef struct construct_type_t construct_type_t;
2034 struct construct_type_t {
2035 construct_type_type_t type;
2036 construct_type_t *next;
2039 typedef struct parsed_pointer_t parsed_pointer_t;
2040 struct parsed_pointer_t {
2041 construct_type_t construct_type;
2042 type_qualifiers_t type_qualifiers;
2045 typedef struct construct_function_type_t construct_function_type_t;
2046 struct construct_function_type_t {
2047 construct_type_t construct_type;
2048 type_t *function_type;
2051 typedef struct parsed_array_t parsed_array_t;
2052 struct parsed_array_t {
2053 construct_type_t construct_type;
2054 type_qualifiers_t type_qualifiers;
2060 typedef struct construct_base_type_t construct_base_type_t;
2061 struct construct_base_type_t {
2062 construct_type_t construct_type;
2066 static construct_type_t *parse_pointer_declarator(void)
2070 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2071 memset(pointer, 0, sizeof(pointer[0]));
2072 pointer->construct_type.type = CONSTRUCT_POINTER;
2073 pointer->type_qualifiers = parse_type_qualifiers();
2075 return (construct_type_t*) pointer;
2078 static construct_type_t *parse_array_declarator(void)
2082 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2083 memset(array, 0, sizeof(array[0]));
2084 array->construct_type.type = CONSTRUCT_ARRAY;
2086 if(token.type == T_static) {
2087 array->is_static = true;
2091 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2092 if(type_qualifiers != 0) {
2093 if(token.type == T_static) {
2094 array->is_static = true;
2098 array->type_qualifiers = type_qualifiers;
2100 if(token.type == '*' && look_ahead(1)->type == ']') {
2101 array->is_variable = true;
2103 } else if(token.type != ']') {
2104 array->size = parse_assignment_expression();
2109 return (construct_type_t*) array;
2112 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2116 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2118 declaration_t *parameters = parse_parameters(&type->function);
2119 if(declaration != NULL) {
2120 declaration->context.declarations = parameters;
2123 construct_function_type_t *construct_function_type =
2124 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2125 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2126 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2127 construct_function_type->function_type = type;
2131 return (construct_type_t*) construct_function_type;
2134 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2135 bool may_be_abstract)
2137 /* construct a single linked list of construct_type_t's which describe
2138 * how to construct the final declarator type */
2139 construct_type_t *first = NULL;
2140 construct_type_t *last = NULL;
2143 while(token.type == '*') {
2144 construct_type_t *type = parse_pointer_declarator();
2155 /* TODO: find out if this is correct */
2158 construct_type_t *inner_types = NULL;
2160 switch(token.type) {
2162 if(declaration == NULL) {
2163 parse_error("no identifier expected in typename");
2165 declaration->symbol = token.v.symbol;
2166 declaration->source_position = token.source_position;
2172 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2178 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2179 /* avoid a loop in the outermost scope, because eat_statement doesn't
2181 if(token.type == '}' && current_function == NULL) {
2189 construct_type_t *p = last;
2192 construct_type_t *type;
2193 switch(token.type) {
2195 type = parse_function_declarator(declaration);
2198 type = parse_array_declarator();
2201 goto declarator_finished;
2204 /* insert in the middle of the list (behind p) */
2206 type->next = p->next;
2217 declarator_finished:
2220 /* append inner_types at the end of the list, we don't to set last anymore
2221 * as it's not needed anymore */
2223 assert(first == NULL);
2224 first = inner_types;
2226 last->next = inner_types;
2232 static type_t *construct_declarator_type(construct_type_t *construct_list,
2235 construct_type_t *iter = construct_list;
2236 for( ; iter != NULL; iter = iter->next) {
2237 switch(iter->type) {
2238 case CONSTRUCT_INVALID:
2239 panic("invalid type construction found");
2240 case CONSTRUCT_FUNCTION: {
2241 construct_function_type_t *construct_function_type
2242 = (construct_function_type_t*) iter;
2244 type_t *function_type = construct_function_type->function_type;
2246 function_type->function.result_type = type;
2248 type = function_type;
2252 case CONSTRUCT_POINTER: {
2253 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2254 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2255 pointer_type->pointer.points_to = type;
2256 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2258 type = pointer_type;
2262 case CONSTRUCT_ARRAY: {
2263 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2264 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2266 array_type->base.qualifiers = parsed_array->type_qualifiers;
2267 array_type->array.element_type = type;
2268 array_type->array.is_static = parsed_array->is_static;
2269 array_type->array.is_variable = parsed_array->is_variable;
2270 array_type->array.size = parsed_array->size;
2277 type_t *hashed_type = typehash_insert(type);
2278 if(hashed_type != type) {
2279 /* the function type was constructed earlier freeing it here will
2280 * destroy other types... */
2281 if(iter->type != CONSTRUCT_FUNCTION) {
2291 static declaration_t *parse_declarator(
2292 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2294 type_t *type = specifiers->type;
2295 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2296 declaration->storage_class = specifiers->storage_class;
2297 declaration->is_inline = specifiers->is_inline;
2299 construct_type_t *construct_type
2300 = parse_inner_declarator(declaration, may_be_abstract);
2301 declaration->type = construct_declarator_type(construct_type, type);
2303 if(construct_type != NULL) {
2304 obstack_free(&temp_obst, construct_type);
2310 static type_t *parse_abstract_declarator(type_t *base_type)
2312 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2314 type_t *result = construct_declarator_type(construct_type, base_type);
2315 if(construct_type != NULL) {
2316 obstack_free(&temp_obst, construct_type);
2322 static declaration_t *record_declaration(declaration_t *declaration)
2324 assert(declaration->parent_context == NULL);
2325 assert(context != NULL);
2327 symbol_t *symbol = declaration->symbol;
2328 if(symbol != NULL) {
2329 declaration_t *alias = environment_push(declaration);
2330 if(alias != declaration)
2333 declaration->parent_context = context;
2336 if(last_declaration != NULL) {
2337 last_declaration->next = declaration;
2339 context->declarations = declaration;
2341 last_declaration = declaration;
2346 static void parser_error_multiple_definition(declaration_t *declaration,
2347 const source_position_t source_position)
2349 parser_print_error_prefix_pos(source_position);
2350 fprintf(stderr, "multiple definition of symbol '%s'\n",
2351 declaration->symbol->string);
2352 parser_print_error_prefix_pos(declaration->source_position);
2353 fprintf(stderr, "this is the location of the previous definition.\n");
2356 static bool is_declaration_specifier(const token_t *token,
2357 bool only_type_specifiers)
2359 switch(token->type) {
2363 return is_typedef_symbol(token->v.symbol);
2365 case T___extension__:
2368 return !only_type_specifiers;
2375 static void parse_init_declarator_rest(declaration_t *declaration)
2379 type_t *orig_type = declaration->type;
2380 type_t *type = NULL;
2381 if(orig_type != NULL)
2382 type = skip_typeref(orig_type);
2384 if(declaration->init.initializer != NULL) {
2385 parser_error_multiple_definition(declaration, token.source_position);
2388 initializer_t *initializer = parse_initializer(type);
2390 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2391 * the array type size */
2392 if(type != NULL && type->type == TYPE_ARRAY && initializer != NULL) {
2393 array_type_t *array_type = &type->array;
2395 if(array_type->size == NULL) {
2396 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2398 cnst->base.datatype = type_size_t;
2400 if(initializer->type == INITIALIZER_LIST) {
2401 initializer_list_t *list = &initializer->list;
2402 cnst->conste.v.int_value = list->len;
2404 assert(initializer->type == INITIALIZER_STRING);
2405 initializer_string_t *string = &initializer->string;
2406 cnst->conste.v.int_value = strlen(string->string) + 1;
2409 array_type->size = cnst;
2413 if(type != NULL && type->type == TYPE_FUNCTION) {
2414 parser_print_error_prefix_pos(declaration->source_position);
2415 fprintf(stderr, "initializers not allowed for function types at "
2416 "declator '%s' (type ", declaration->symbol->string);
2417 print_type_quoted(orig_type);
2418 fprintf(stderr, ")\n");
2420 declaration->init.initializer = initializer;
2424 /* parse rest of a declaration without any declarator */
2425 static void parse_anonymous_declaration_rest(
2426 const declaration_specifiers_t *specifiers,
2427 parsed_declaration_func finished_declaration)
2431 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2433 declaration->type = specifiers->type;
2434 declaration->storage_class = specifiers->storage_class;
2435 declaration->source_position = specifiers->source_position;
2437 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2438 parse_warning_pos(declaration->source_position,
2439 "useless storage class in empty declaration");
2442 type_t *type = declaration->type;
2443 switch (type->type) {
2444 case TYPE_COMPOUND_STRUCT:
2445 case TYPE_COMPOUND_UNION: {
2446 const compound_type_t *compound_type = &type->compound;
2447 if (compound_type->declaration->symbol == NULL) {
2448 parse_warning_pos(declaration->source_position,
2449 "unnamed struct/union that defines no instances");
2458 parse_warning_pos(declaration->source_position,
2459 "empty declaration");
2463 finished_declaration(declaration);
2466 static void parse_declaration_rest(declaration_t *ndeclaration,
2467 const declaration_specifiers_t *specifiers,
2468 parsed_declaration_func finished_declaration)
2471 declaration_t *declaration = finished_declaration(ndeclaration);
2473 type_t *orig_type = declaration->type;
2474 type_t *type = skip_typeref(orig_type);
2476 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2477 parser_print_warning_prefix_pos(declaration->source_position);
2478 fprintf(stderr, "variable '%s' declared 'inline'\n",
2479 declaration->symbol->string);
2482 if(token.type == '=') {
2483 parse_init_declarator_rest(declaration);
2486 if(token.type != ',')
2490 ndeclaration = parse_declarator(specifiers, false);
2495 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2497 /* TODO: check that it was actually a parameter that gets a type */
2499 /* we should have a declaration for the parameter in the current
2501 return record_declaration(declaration);
2504 static void parse_declaration(parsed_declaration_func finished_declaration)
2506 declaration_specifiers_t specifiers;
2507 memset(&specifiers, 0, sizeof(specifiers));
2508 parse_declaration_specifiers(&specifiers);
2510 if(token.type == ';') {
2511 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2513 declaration_t *declaration = parse_declarator(&specifiers, false);
2514 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2518 static void parse_kr_declaration_list(declaration_t *declaration)
2520 type_t *type = skip_typeref(declaration->type);
2521 assert(type->type == TYPE_FUNCTION);
2523 if(!type->function.kr_style_parameters)
2526 /* push function parameters */
2527 int top = environment_top();
2528 context_t *last_context = context;
2529 set_context(&declaration->context);
2531 declaration_t *parameter = declaration->context.declarations;
2532 for( ; parameter != NULL; parameter = parameter->next) {
2533 environment_push(parameter);
2536 /* parse declaration list */
2537 while(is_declaration_specifier(&token, false)) {
2538 parse_declaration(finished_kr_declaration);
2541 /* pop function parameters */
2542 assert(context == &declaration->context);
2543 set_context(last_context);
2544 environment_pop_to(top);
2546 /* update function type */
2547 type_t *new_type = duplicate_type(type);
2548 new_type->function.kr_style_parameters = false;
2550 function_parameter_t *parameters = NULL;
2551 function_parameter_t *last_parameter = NULL;
2553 declaration_t *parameter_declaration = declaration->context.declarations;
2554 for( ; parameter_declaration != NULL;
2555 parameter_declaration = parameter_declaration->next) {
2556 type_t *parameter_type = parameter_declaration->type;
2557 if(parameter_type == NULL) {
2559 parser_print_error_prefix();
2560 fprintf(stderr, "no type specified for function parameter '%s'\n",
2561 parameter_declaration->symbol->string);
2563 parser_print_warning_prefix();
2564 fprintf(stderr, "no type specified for function parameter '%s', "
2565 "using int\n", parameter_declaration->symbol->string);
2566 parameter_type = type_int;
2567 parameter_declaration->type = parameter_type;
2571 function_parameter_t *function_parameter
2572 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2573 memset(function_parameter, 0, sizeof(function_parameter[0]));
2575 function_parameter->type = parameter_type;
2576 if(last_parameter != NULL) {
2577 last_parameter->next = function_parameter;
2579 parameters = function_parameter;
2581 last_parameter = function_parameter;
2583 new_type->function.parameters = parameters;
2585 type = typehash_insert(new_type);
2586 if(type != new_type) {
2587 obstack_free(type_obst, new_type);
2590 declaration->type = type;
2593 static void parse_external_declaration(void)
2595 /* function-definitions and declarations both start with declaration
2597 declaration_specifiers_t specifiers;
2598 memset(&specifiers, 0, sizeof(specifiers));
2599 parse_declaration_specifiers(&specifiers);
2601 /* must be a declaration */
2602 if(token.type == ';') {
2603 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2607 /* declarator is common to both function-definitions and declarations */
2608 declaration_t *ndeclaration = parse_declarator(&specifiers, false);
2610 /* must be a declaration */
2611 if(token.type == ',' || token.type == '=' || token.type == ';') {
2612 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2616 /* must be a function definition */
2617 parse_kr_declaration_list(ndeclaration);
2619 declaration_t *declaration = record_declaration(ndeclaration);
2620 if(ndeclaration != declaration) {
2621 memcpy(&declaration->context, &ndeclaration->context,
2622 sizeof(declaration->context));
2625 /* push function parameters and switch context */
2626 int top = environment_top();
2627 context_t *last_context = context;
2628 set_context(&declaration->context);
2630 declaration_t *parameter = declaration->context.declarations;
2631 for( ; parameter != NULL; parameter = parameter->next) {
2632 environment_push(parameter);
2638 if(token.type != '{') {
2639 parse_error_expected("while parsing function definition", '{', 0);
2641 goto end_of_parse_external_declaration;
2644 orig_type = declaration->type;
2645 if(orig_type == NULL) {
2647 goto end_of_parse_external_declaration;
2650 type = skip_typeref(orig_type);
2652 if(type->type != TYPE_FUNCTION) {
2653 parser_print_error_prefix();
2654 fprintf(stderr, "declarator '");
2655 print_type_ext(orig_type, declaration->symbol, NULL);
2656 fprintf(stderr, "' has a body but is not a function type.\n");
2658 goto end_of_parse_external_declaration;
2661 /* § 6.7.5.3 (14) a function definition with () means no
2662 * parameters (and not unspecified parameters) */
2663 if(type->function.unspecified_parameters) {
2664 type_t *duplicate = duplicate_type(type);
2665 duplicate->function.unspecified_parameters = false;
2667 type = typehash_insert(duplicate);
2668 if(type != duplicate) {
2669 obstack_free(type_obst, duplicate);
2671 declaration->type = type;
2674 if(declaration->init.statement != NULL) {
2675 parser_error_multiple_definition(declaration, token.source_position);
2677 goto end_of_parse_external_declaration;
2680 /* parse function body */
2682 int label_stack_top = label_top();
2683 declaration_t *old_current_function = current_function;
2684 current_function = declaration;
2686 declaration->init.statement = parse_compound_statement();
2688 assert(current_function == declaration);
2689 current_function = old_current_function;
2690 label_pop_to(label_stack_top);
2693 end_of_parse_external_declaration:
2694 assert(context == &declaration->context);
2695 set_context(last_context);
2696 environment_pop_to(top);
2699 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2702 if(token.type == ':') {
2704 parse_constant_expression();
2705 /* TODO (bitfields) */
2707 declaration_t *declaration = parse_declarator(specifiers, true);
2709 /* TODO: check constraints for struct declarations */
2710 /* TODO: check for doubled fields */
2711 record_declaration(declaration);
2713 if(token.type == ':') {
2715 parse_constant_expression();
2716 /* TODO (bitfields) */
2720 if(token.type != ',')
2727 static void parse_compound_type_entries(void)
2731 while(token.type != '}' && token.type != T_EOF) {
2732 declaration_specifiers_t specifiers;
2733 memset(&specifiers, 0, sizeof(specifiers));
2734 parse_declaration_specifiers(&specifiers);
2736 parse_struct_declarators(&specifiers);
2738 if(token.type == T_EOF) {
2739 parse_error("EOF while parsing struct");
2744 static type_t *parse_typename(void)
2746 declaration_specifiers_t specifiers;
2747 memset(&specifiers, 0, sizeof(specifiers));
2748 parse_declaration_specifiers(&specifiers);
2749 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2750 /* TODO: improve error message, user does probably not know what a
2751 * storage class is...
2753 parse_error("typename may not have a storage class");
2756 type_t *result = parse_abstract_declarator(specifiers.type);
2764 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2765 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2766 expression_t *left);
2768 typedef struct expression_parser_function_t expression_parser_function_t;
2769 struct expression_parser_function_t {
2770 unsigned precedence;
2771 parse_expression_function parser;
2772 unsigned infix_precedence;
2773 parse_expression_infix_function infix_parser;
2776 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2778 static expression_t *make_invalid_expression(void)
2780 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2781 expression->base.source_position = token.source_position;
2785 static expression_t *expected_expression_error(void)
2787 parser_print_error_prefix();
2788 fprintf(stderr, "expected expression, got token ");
2789 print_token(stderr, &token);
2790 fprintf(stderr, "\n");
2794 return make_invalid_expression();
2797 static expression_t *parse_string_const(void)
2799 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2800 cnst->base.datatype = type_string;
2801 cnst->string.value = parse_string_literals();
2806 static expression_t *parse_int_const(void)
2808 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2809 cnst->base.datatype = token.datatype;
2810 cnst->conste.v.int_value = token.v.intvalue;
2817 static expression_t *parse_float_const(void)
2819 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2820 cnst->base.datatype = token.datatype;
2821 cnst->conste.v.float_value = token.v.floatvalue;
2828 static declaration_t *create_implicit_function(symbol_t *symbol,
2829 const source_position_t source_position)
2831 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2832 ntype->function.result_type = type_int;
2833 ntype->function.unspecified_parameters = true;
2835 type_t *type = typehash_insert(ntype);
2840 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2842 declaration->storage_class = STORAGE_CLASS_EXTERN;
2843 declaration->type = type;
2844 declaration->symbol = symbol;
2845 declaration->source_position = source_position;
2847 /* prepend the implicit definition to the global context
2848 * this is safe since the symbol wasn't declared as anything else yet
2850 assert(symbol->declaration == NULL);
2852 context_t *last_context = context;
2853 context = global_context;
2855 environment_push(declaration);
2856 declaration->next = context->declarations;
2857 context->declarations = declaration;
2859 context = last_context;
2864 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2866 function_parameter_t *parameter
2867 = obstack_alloc(type_obst, sizeof(parameter[0]));
2868 memset(parameter, 0, sizeof(parameter[0]));
2869 parameter->type = argument_type;
2871 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2872 type->function.result_type = result_type;
2873 type->function.parameters = parameter;
2875 type_t *result = typehash_insert(type);
2876 if(result != type) {
2883 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2885 switch(symbol->ID) {
2886 case T___builtin_alloca:
2887 return make_function_1_type(type_void_ptr, type_size_t);
2888 case T___builtin_nan:
2889 return make_function_1_type(type_double, type_string);
2890 case T___builtin_nanf:
2891 return make_function_1_type(type_float, type_string);
2892 case T___builtin_nand:
2893 return make_function_1_type(type_long_double, type_string);
2895 panic("not implemented builtin symbol found");
2900 * performs automatic type cast as described in § 6.3.2.1
2902 static type_t *automatic_type_conversion(type_t *type)
2907 if(type->type == TYPE_ARRAY) {
2908 array_type_t *array_type = &type->array;
2909 type_t *element_type = array_type->element_type;
2910 unsigned qualifiers = array_type->type.qualifiers;
2912 return make_pointer_type(element_type, qualifiers);
2915 if(type->type == TYPE_FUNCTION) {
2916 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2923 * reverts the automatic casts of array to pointer types and function
2924 * to function-pointer types as defined § 6.3.2.1
2926 type_t *revert_automatic_type_conversion(const expression_t *expression)
2928 if(expression->base.datatype == NULL)
2931 switch(expression->type) {
2932 case EXPR_REFERENCE: {
2933 const reference_expression_t *ref = &expression->reference;
2934 return ref->declaration->type;
2937 const select_expression_t *select = &expression->select;
2938 return select->compound_entry->type;
2941 const unary_expression_t *unary = &expression->unary;
2942 if(unary->type == UNEXPR_DEREFERENCE) {
2943 expression_t *value = unary->value;
2944 type_t *type = skip_typeref(value->base.datatype);
2945 pointer_type_t *pointer_type = &type->pointer;
2947 return pointer_type->points_to;
2951 case EXPR_BUILTIN_SYMBOL: {
2952 const builtin_symbol_expression_t *builtin
2953 = &expression->builtin_symbol;
2954 return get_builtin_symbol_type(builtin->symbol);
2956 case EXPR_ARRAY_ACCESS: {
2957 const array_access_expression_t *array_access
2958 = &expression->array_access;
2959 const expression_t *array_ref = array_access->array_ref;
2960 type_t *type_left = skip_typeref(array_ref->base.datatype);
2961 assert(is_type_pointer(type_left));
2962 pointer_type_t *pointer_type = &type_left->pointer;
2963 return pointer_type->points_to;
2970 return expression->base.datatype;
2973 static expression_t *parse_reference(void)
2975 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2977 reference_expression_t *ref = &expression->reference;
2978 ref->symbol = token.v.symbol;
2980 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2982 source_position_t source_position = token.source_position;
2985 if(declaration == NULL) {
2987 /* an implicitly defined function */
2988 if(token.type == '(') {
2989 parser_print_prefix_pos(token.source_position);
2990 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2991 ref->symbol->string);
2993 declaration = create_implicit_function(ref->symbol,
2998 parser_print_error_prefix();
2999 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3004 type_t *type = declaration->type;
3005 /* we always do the auto-type conversions; the & and sizeof parser contains
3006 * code to revert this! */
3007 type = automatic_type_conversion(type);
3009 ref->declaration = declaration;
3010 ref->expression.datatype = type;
3015 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3019 /* TODO check if explicit cast is allowed and issue warnings/errors */
3022 static expression_t *parse_cast(void)
3024 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
3026 cast->unary.type = UNEXPR_CAST;
3027 cast->base.source_position = token.source_position;
3029 type_t *type = parse_typename();
3032 expression_t *value = parse_sub_expression(20);
3034 check_cast_allowed(value, type);
3036 cast->base.datatype = type;
3037 cast->unary.value = value;
3042 static expression_t *parse_statement_expression(void)
3044 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3046 statement_t *statement = parse_compound_statement();
3047 expression->statement.statement = statement;
3048 if(statement == NULL) {
3053 assert(statement->type == STATEMENT_COMPOUND);
3054 compound_statement_t *compound_statement = &statement->compound;
3056 /* find last statement and use it's type */
3057 const statement_t *last_statement = NULL;
3058 const statement_t *iter = compound_statement->statements;
3059 for( ; iter != NULL; iter = iter->base.next) {
3060 last_statement = iter;
3063 if(last_statement->type == STATEMENT_EXPRESSION) {
3064 const expression_statement_t *expression_statement
3065 = &last_statement->expression;
3066 expression->base.datatype
3067 = expression_statement->expression->base.datatype;
3069 expression->base.datatype = type_void;
3077 static expression_t *parse_brace_expression(void)
3081 switch(token.type) {
3083 /* gcc extension: a stement expression */
3084 return parse_statement_expression();
3088 return parse_cast();
3090 if(is_typedef_symbol(token.v.symbol)) {
3091 return parse_cast();
3095 expression_t *result = parse_expression();
3101 static expression_t *parse_function_keyword(void)
3106 if (current_function == NULL) {
3107 parse_error("'__func__' used outside of a function");
3110 string_literal_expression_t *expression
3111 = allocate_ast_zero(sizeof(expression[0]));
3113 expression->expression.type = EXPR_FUNCTION;
3114 expression->expression.datatype = type_string;
3115 expression->value = "TODO: FUNCTION";
3117 return (expression_t*) expression;
3120 static expression_t *parse_pretty_function_keyword(void)
3122 eat(T___PRETTY_FUNCTION__);
3125 string_literal_expression_t *expression
3126 = allocate_ast_zero(sizeof(expression[0]));
3128 expression->expression.type = EXPR_PRETTY_FUNCTION;
3129 expression->expression.datatype = type_string;
3130 expression->value = "TODO: PRETTY FUNCTION";
3132 return (expression_t*) expression;
3135 static designator_t *parse_designator(void)
3137 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3139 if(token.type != T_IDENTIFIER) {
3140 parse_error_expected("while parsing member designator",
3145 result->symbol = token.v.symbol;
3148 designator_t *last_designator = result;
3150 if(token.type == '.') {
3152 if(token.type != T_IDENTIFIER) {
3153 parse_error_expected("while parsing member designator",
3158 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3159 designator->symbol = token.v.symbol;
3162 last_designator->next = designator;
3163 last_designator = designator;
3166 if(token.type == '[') {
3168 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3169 designator->array_access = parse_expression();
3170 if(designator->array_access == NULL) {
3176 last_designator->next = designator;
3177 last_designator = designator;
3186 static expression_t *parse_offsetof(void)
3188 eat(T___builtin_offsetof);
3190 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3191 expression->base.datatype = type_size_t;
3194 expression->offsetofe.type = parse_typename();
3196 expression->offsetofe.designator = parse_designator();
3202 static expression_t *parse_va_arg(void)
3204 eat(T___builtin_va_arg);
3206 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3209 expression->va_arge.arg = parse_assignment_expression();
3211 expression->base.datatype = parse_typename();
3217 static expression_t *parse_builtin_symbol(void)
3219 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3221 symbol_t *symbol = token.v.symbol;
3223 expression->builtin_symbol.symbol = symbol;
3226 type_t *type = get_builtin_symbol_type(symbol);
3227 type = automatic_type_conversion(type);
3229 expression->base.datatype = type;
3233 static expression_t *parse_primary_expression(void)
3235 switch(token.type) {
3237 return parse_int_const();
3238 case T_FLOATINGPOINT:
3239 return parse_float_const();
3240 case T_STRING_LITERAL:
3241 return parse_string_const();
3243 return parse_reference();
3244 case T___FUNCTION__:
3246 return parse_function_keyword();
3247 case T___PRETTY_FUNCTION__:
3248 return parse_pretty_function_keyword();
3249 case T___builtin_offsetof:
3250 return parse_offsetof();
3251 case T___builtin_va_arg:
3252 return parse_va_arg();
3253 case T___builtin_nanf:
3254 case T___builtin_alloca:
3255 case T___builtin_expect:
3256 case T___builtin_va_start:
3257 case T___builtin_va_end:
3258 return parse_builtin_symbol();
3261 return parse_brace_expression();
3264 parser_print_error_prefix();
3265 fprintf(stderr, "unexpected token ");
3266 print_token(stderr, &token);
3267 fprintf(stderr, "\n");
3270 return make_invalid_expression();
3273 static expression_t *parse_array_expression(unsigned precedence,
3280 expression_t *inside = parse_expression();
3282 array_access_expression_t *array_access
3283 = allocate_ast_zero(sizeof(array_access[0]));
3285 array_access->expression.type = EXPR_ARRAY_ACCESS;
3287 type_t *type_left = left->base.datatype;
3288 type_t *type_inside = inside->base.datatype;
3289 type_t *result_type = NULL;
3291 if(type_left != NULL && type_inside != NULL) {
3292 type_left = skip_typeref(type_left);
3293 type_inside = skip_typeref(type_inside);
3295 if(is_type_pointer(type_left)) {
3296 pointer_type_t *pointer = &type_left->pointer;
3297 result_type = pointer->points_to;
3298 array_access->array_ref = left;
3299 array_access->index = inside;
3300 } else if(is_type_pointer(type_inside)) {
3301 pointer_type_t *pointer = &type_inside->pointer;
3302 result_type = pointer->points_to;
3303 array_access->array_ref = inside;
3304 array_access->index = left;
3305 array_access->flipped = true;
3307 parser_print_error_prefix();
3308 fprintf(stderr, "array access on object with non-pointer types ");
3309 print_type_quoted(type_left);
3310 fprintf(stderr, ", ");
3311 print_type_quoted(type_inside);
3312 fprintf(stderr, "\n");
3315 array_access->array_ref = left;
3316 array_access->index = inside;
3319 if(token.type != ']') {
3320 parse_error_expected("Problem while parsing array access", ']', 0);
3321 return (expression_t*) array_access;
3325 result_type = automatic_type_conversion(result_type);
3326 array_access->expression.datatype = result_type;
3328 return (expression_t*) array_access;
3331 static expression_t *parse_sizeof(unsigned precedence)
3335 sizeof_expression_t *sizeof_expression
3336 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3337 sizeof_expression->expression.type = EXPR_SIZEOF;
3338 sizeof_expression->expression.datatype = type_size_t;
3340 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3342 sizeof_expression->type = parse_typename();
3345 expression_t *expression = parse_sub_expression(precedence);
3346 expression->base.datatype = revert_automatic_type_conversion(expression);
3348 sizeof_expression->type = expression->base.datatype;
3349 sizeof_expression->size_expression = expression;
3352 return (expression_t*) sizeof_expression;
3355 static expression_t *parse_select_expression(unsigned precedence,
3356 expression_t *compound)
3359 assert(token.type == '.' || token.type == T_MINUSGREATER);
3361 bool is_pointer = (token.type == T_MINUSGREATER);
3364 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3365 select->select.compound = compound;
3367 if(token.type != T_IDENTIFIER) {
3368 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3371 symbol_t *symbol = token.v.symbol;
3372 select->select.symbol = symbol;
3375 type_t *orig_type = compound->base.datatype;
3376 if(orig_type == NULL)
3377 return make_invalid_expression();
3379 type_t *type = skip_typeref(orig_type);
3381 type_t *type_left = type;
3383 if(type->type != TYPE_POINTER) {
3384 parser_print_error_prefix();
3385 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3386 print_type_quoted(orig_type);
3387 fputc('\n', stderr);
3388 return make_invalid_expression();
3390 pointer_type_t *pointer_type = &type->pointer;
3391 type_left = pointer_type->points_to;
3393 type_left = skip_typeref(type_left);
3395 if(type_left->type != TYPE_COMPOUND_STRUCT
3396 && type_left->type != TYPE_COMPOUND_UNION) {
3397 parser_print_error_prefix();
3398 fprintf(stderr, "request for member '%s' in something not a struct or "
3399 "union, but ", symbol->string);
3400 print_type_quoted(type_left);
3401 fputc('\n', stderr);
3402 return make_invalid_expression();
3405 compound_type_t *compound_type = &type_left->compound;
3406 declaration_t *declaration = compound_type->declaration;
3408 if(!declaration->init.is_defined) {
3409 parser_print_error_prefix();
3410 fprintf(stderr, "request for member '%s' of incomplete type ",
3412 print_type_quoted(type_left);
3413 fputc('\n', stderr);
3414 return make_invalid_expression();
3417 declaration_t *iter = declaration->context.declarations;
3418 for( ; iter != NULL; iter = iter->next) {
3419 if(iter->symbol == symbol) {
3424 parser_print_error_prefix();
3425 print_type_quoted(type_left);
3426 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3427 return make_invalid_expression();
3430 /* we always do the auto-type conversions; the & and sizeof parser contains
3431 * code to revert this! */
3432 type_t *expression_type = automatic_type_conversion(iter->type);
3434 select->select.compound_entry = iter;
3435 select->base.datatype = expression_type;
3439 static expression_t *parse_call_expression(unsigned precedence,
3440 expression_t *expression)
3443 expression_t *result = allocate_expression_zero(EXPR_CALL);
3445 call_expression_t *call = &result->call;
3446 call->function = expression;
3448 function_type_t *function_type = NULL;
3449 type_t *orig_type = expression->base.datatype;
3450 if(orig_type != NULL) {
3451 type_t *type = skip_typeref(orig_type);
3453 if(is_type_pointer(type)) {
3454 pointer_type_t *pointer_type = &type->pointer;
3456 type = skip_typeref(pointer_type->points_to);
3458 if (type->type == TYPE_FUNCTION) {
3459 function_type = &type->function;
3460 call->expression.datatype = function_type->result_type;
3463 if(function_type == NULL) {
3464 parser_print_error_prefix();
3465 fputs("called object '", stderr);
3466 print_expression(expression);
3467 fputs("' (type ", stderr);
3468 print_type_quoted(orig_type);
3469 fputs(") is not a pointer to a function\n", stderr);
3471 function_type = NULL;
3472 call->expression.datatype = NULL;
3476 /* parse arguments */
3479 if(token.type != ')') {
3480 call_argument_t *last_argument = NULL;
3483 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3485 argument->expression = parse_assignment_expression();
3486 if(last_argument == NULL) {
3487 call->arguments = argument;
3489 last_argument->next = argument;
3491 last_argument = argument;
3493 if(token.type != ',')
3500 if(function_type != NULL) {
3501 function_parameter_t *parameter = function_type->parameters;
3502 call_argument_t *argument = call->arguments;
3503 for( ; parameter != NULL && argument != NULL;
3504 parameter = parameter->next, argument = argument->next) {
3505 type_t *expected_type = parameter->type;
3506 /* TODO report context in error messages */
3507 argument->expression = create_implicit_cast(argument->expression,
3510 /* too few parameters */
3511 if(parameter != NULL) {
3512 parser_print_error_prefix();
3513 fprintf(stderr, "too few arguments to function '");
3514 print_expression(expression);
3515 fprintf(stderr, "'\n");
3516 } else if(argument != NULL) {
3517 /* too many parameters */
3518 if(!function_type->variadic
3519 && !function_type->unspecified_parameters) {
3520 parser_print_error_prefix();
3521 fprintf(stderr, "too many arguments to function '");
3522 print_expression(expression);
3523 fprintf(stderr, "'\n");
3525 /* do default promotion */
3526 for( ; argument != NULL; argument = argument->next) {
3527 type_t *type = argument->expression->base.datatype;
3532 type = skip_typeref(type);
3533 if(is_type_integer(type)) {
3534 type = promote_integer(type);
3535 } else if(type == type_float) {
3539 argument->expression
3540 = create_implicit_cast(argument->expression, type);
3549 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3551 static bool same_compound_type(const type_t *type1, const type_t *type2)
3553 if(!is_type_compound(type1))
3555 if(type1->type != type2->type)
3558 const compound_type_t *compound1 = &type1->compound;
3559 const compound_type_t *compound2 = &type2->compound;
3561 return compound1->declaration == compound2->declaration;
3564 static expression_t *parse_conditional_expression(unsigned precedence,
3565 expression_t *expression)
3569 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3571 conditional_expression_t *conditional = &result->conditional;
3572 conditional->condition = expression;
3575 type_t *condition_type_orig = expression->base.datatype;
3576 if(condition_type_orig != NULL) {
3577 type_t *condition_type = skip_typeref(condition_type_orig);
3578 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3579 type_error("expected a scalar type in conditional condition",
3580 expression->base.source_position, condition_type_orig);
3584 expression_t *true_expression = parse_expression();
3586 expression_t *false_expression = parse_sub_expression(precedence);
3588 conditional->true_expression = true_expression;
3589 conditional->false_expression = false_expression;
3591 type_t *orig_true_type = true_expression->base.datatype;
3592 type_t *orig_false_type = false_expression->base.datatype;
3593 if(orig_true_type == NULL || orig_false_type == NULL)
3596 type_t *true_type = skip_typeref(orig_true_type);
3597 type_t *false_type = skip_typeref(orig_false_type);
3600 type_t *result_type = NULL;
3601 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3602 result_type = semantic_arithmetic(true_type, false_type);
3604 true_expression = create_implicit_cast(true_expression, result_type);
3605 false_expression = create_implicit_cast(false_expression, result_type);
3607 conditional->true_expression = true_expression;
3608 conditional->false_expression = false_expression;
3609 conditional->expression.datatype = result_type;
3610 } else if (same_compound_type(true_type, false_type)
3611 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3612 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3613 /* just take 1 of the 2 types */
3614 result_type = true_type;
3615 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3616 && pointers_compatible(true_type, false_type)) {
3618 result_type = true_type;
3621 type_error_incompatible("while parsing conditional",
3622 expression->base.source_position, true_type,
3626 conditional->expression.datatype = result_type;
3630 static expression_t *parse_extension(unsigned precedence)
3632 eat(T___extension__);
3634 /* TODO enable extensions */
3636 return parse_sub_expression(precedence);
3639 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3641 eat(T___builtin_classify_type);
3643 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3644 result->base.datatype = type_int;
3647 expression_t *expression = parse_sub_expression(precedence);
3649 result->classify_type.type_expression = expression;
3654 static void semantic_incdec(unary_expression_t *expression)
3656 type_t *orig_type = expression->value->base.datatype;
3657 if(orig_type == NULL)
3660 type_t *type = skip_typeref(orig_type);
3661 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3662 /* TODO: improve error message */
3663 parser_print_error_prefix();
3664 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3668 expression->expression.datatype = orig_type;
3671 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3673 type_t *orig_type = expression->value->base.datatype;
3674 if(orig_type == NULL)
3677 type_t *type = skip_typeref(orig_type);
3678 if(!is_type_arithmetic(type)) {
3679 /* TODO: improve error message */
3680 parser_print_error_prefix();
3681 fprintf(stderr, "operation needs an arithmetic type\n");
3685 expression->expression.datatype = orig_type;
3688 static void semantic_unexpr_scalar(unary_expression_t *expression)
3690 type_t *orig_type = expression->value->base.datatype;
3691 if(orig_type == NULL)
3694 type_t *type = skip_typeref(orig_type);
3695 if (!is_type_scalar(type)) {
3696 parse_error("operand of ! must be of scalar type\n");
3700 expression->expression.datatype = orig_type;
3703 static void semantic_unexpr_integer(unary_expression_t *expression)
3705 type_t *orig_type = expression->value->base.datatype;
3706 if(orig_type == NULL)
3709 type_t *type = skip_typeref(orig_type);
3710 if (!is_type_integer(type)) {
3711 parse_error("operand of ~ must be of integer type\n");
3715 expression->expression.datatype = orig_type;
3718 static void semantic_dereference(unary_expression_t *expression)
3720 type_t *orig_type = expression->value->base.datatype;
3721 if(orig_type == NULL)
3724 type_t *type = skip_typeref(orig_type);
3725 if(!is_type_pointer(type)) {
3726 parser_print_error_prefix();
3727 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3728 print_type_quoted(orig_type);
3729 fputs(" given.\n", stderr);
3733 pointer_type_t *pointer_type = &type->pointer;
3734 type_t *result_type = pointer_type->points_to;
3736 result_type = automatic_type_conversion(result_type);
3737 expression->expression.datatype = result_type;
3740 static void semantic_take_addr(unary_expression_t *expression)
3742 expression_t *value = expression->value;
3743 value->base.datatype = revert_automatic_type_conversion(value);
3745 type_t *orig_type = value->base.datatype;
3746 if(orig_type == NULL)
3749 if(value->type == EXPR_REFERENCE) {
3750 reference_expression_t *reference = (reference_expression_t*) value;
3751 declaration_t *declaration = reference->declaration;
3752 if(declaration != NULL) {
3753 declaration->address_taken = 1;
3757 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3760 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3761 static expression_t *parse_##unexpression_type(unsigned precedence) \
3765 unary_expression_t *unary_expression \
3766 = allocate_ast_zero(sizeof(unary_expression[0])); \
3767 unary_expression->expression.type = EXPR_UNARY; \
3768 unary_expression->type = unexpression_type; \
3769 unary_expression->value = parse_sub_expression(precedence); \
3771 sfunc(unary_expression); \
3773 return (expression_t*) unary_expression; \
3776 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3777 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3778 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3779 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3780 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3781 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3782 semantic_unexpr_integer)
3783 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3785 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3788 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3790 static expression_t *parse_##unexpression_type(unsigned precedence, \
3791 expression_t *left) \
3793 (void) precedence; \
3796 unary_expression_t *unary_expression \
3797 = allocate_ast_zero(sizeof(unary_expression[0])); \
3798 unary_expression->expression.type = EXPR_UNARY; \
3799 unary_expression->type = unexpression_type; \
3800 unary_expression->value = left; \
3802 sfunc(unary_expression); \
3804 return (expression_t*) unary_expression; \
3807 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3809 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3812 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3814 /* TODO: handle complex + imaginary types */
3816 /* § 6.3.1.8 Usual arithmetic conversions */
3817 if(type_left == type_long_double || type_right == type_long_double) {
3818 return type_long_double;
3819 } else if(type_left == type_double || type_right == type_double) {
3821 } else if(type_left == type_float || type_right == type_float) {
3825 type_right = promote_integer(type_right);
3826 type_left = promote_integer(type_left);
3828 if(type_left == type_right)
3831 bool signed_left = is_type_signed(type_left);
3832 bool signed_right = is_type_signed(type_right);
3833 int rank_left = get_rank(type_left);
3834 int rank_right = get_rank(type_right);
3835 if(rank_left < rank_right) {
3836 if(signed_left == signed_right || !signed_right) {
3842 if(signed_left == signed_right || !signed_left) {
3850 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3852 expression_t *left = expression->left;
3853 expression_t *right = expression->right;
3854 type_t *orig_type_left = left->base.datatype;
3855 type_t *orig_type_right = right->base.datatype;
3857 if(orig_type_left == NULL || orig_type_right == NULL)
3860 type_t *type_left = skip_typeref(orig_type_left);
3861 type_t *type_right = skip_typeref(orig_type_right);
3863 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3864 /* TODO: improve error message */
3865 parser_print_error_prefix();
3866 fprintf(stderr, "operation needs arithmetic types\n");
3870 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3871 expression->left = create_implicit_cast(left, arithmetic_type);
3872 expression->right = create_implicit_cast(right, arithmetic_type);
3873 expression->expression.datatype = arithmetic_type;
3876 static void semantic_shift_op(binary_expression_t *expression)
3878 expression_t *left = expression->left;
3879 expression_t *right = expression->right;
3880 type_t *orig_type_left = left->base.datatype;
3881 type_t *orig_type_right = right->base.datatype;
3883 if(orig_type_left == NULL || orig_type_right == NULL)
3886 type_t *type_left = skip_typeref(orig_type_left);
3887 type_t *type_right = skip_typeref(orig_type_right);
3889 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3890 /* TODO: improve error message */
3891 parser_print_error_prefix();
3892 fprintf(stderr, "operation needs integer types\n");
3896 type_left = promote_integer(type_left);
3897 type_right = promote_integer(type_right);
3899 expression->left = create_implicit_cast(left, type_left);
3900 expression->right = create_implicit_cast(right, type_right);
3901 expression->expression.datatype = type_left;
3904 static void semantic_add(binary_expression_t *expression)
3906 expression_t *left = expression->left;
3907 expression_t *right = expression->right;
3908 type_t *orig_type_left = left->base.datatype;
3909 type_t *orig_type_right = right->base.datatype;
3911 if(orig_type_left == NULL || orig_type_right == NULL)
3914 type_t *type_left = skip_typeref(orig_type_left);
3915 type_t *type_right = skip_typeref(orig_type_right);
3918 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3919 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3920 expression->left = create_implicit_cast(left, arithmetic_type);
3921 expression->right = create_implicit_cast(right, arithmetic_type);
3922 expression->expression.datatype = arithmetic_type;
3924 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3925 expression->expression.datatype = type_left;
3926 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3927 expression->expression.datatype = type_right;
3929 parser_print_error_prefix();
3930 fprintf(stderr, "invalid operands to binary + (");
3931 print_type_quoted(orig_type_left);
3932 fprintf(stderr, ", ");
3933 print_type_quoted(orig_type_right);
3934 fprintf(stderr, ")\n");
3938 static void semantic_sub(binary_expression_t *expression)
3940 expression_t *left = expression->left;
3941 expression_t *right = expression->right;
3942 type_t *orig_type_left = left->base.datatype;
3943 type_t *orig_type_right = right->base.datatype;
3945 if(orig_type_left == NULL || orig_type_right == NULL)
3948 type_t *type_left = skip_typeref(orig_type_left);
3949 type_t *type_right = skip_typeref(orig_type_right);
3952 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3953 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3954 expression->left = create_implicit_cast(left, arithmetic_type);
3955 expression->right = create_implicit_cast(right, arithmetic_type);
3956 expression->expression.datatype = arithmetic_type;
3958 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3959 expression->expression.datatype = type_left;
3960 } else if(type_left->type == TYPE_POINTER &&
3961 type_right->type == TYPE_POINTER) {
3962 if(!pointers_compatible(type_left, type_right)) {
3963 parser_print_error_prefix();
3964 fprintf(stderr, "pointers to incompatible objects to binary - (");
3965 print_type_quoted(orig_type_left);
3966 fprintf(stderr, ", ");
3967 print_type_quoted(orig_type_right);
3968 fprintf(stderr, ")\n");
3970 expression->expression.datatype = type_ptrdiff_t;
3973 parser_print_error_prefix();
3974 fprintf(stderr, "invalid operands to binary - (");
3975 print_type_quoted(orig_type_left);
3976 fprintf(stderr, ", ");
3977 print_type_quoted(orig_type_right);
3978 fprintf(stderr, ")\n");
3982 static void semantic_comparison(binary_expression_t *expression)
3984 expression_t *left = expression->left;
3985 expression_t *right = expression->right;
3986 type_t *orig_type_left = left->base.datatype;
3987 type_t *orig_type_right = right->base.datatype;
3989 if(orig_type_left == NULL || orig_type_right == NULL)
3992 type_t *type_left = skip_typeref(orig_type_left);
3993 type_t *type_right = skip_typeref(orig_type_right);
3995 /* TODO non-arithmetic types */
3996 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3997 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3998 expression->left = create_implicit_cast(left, arithmetic_type);
3999 expression->right = create_implicit_cast(right, arithmetic_type);
4000 expression->expression.datatype = arithmetic_type;
4001 } else if (type_left->type == TYPE_POINTER &&
4002 type_right->type == TYPE_POINTER) {
4003 /* TODO check compatibility */
4004 } else if (type_left->type == TYPE_POINTER) {
4005 expression->right = create_implicit_cast(right, type_left);
4006 } else if (type_right->type == TYPE_POINTER) {
4007 expression->left = create_implicit_cast(left, type_right);
4009 type_error_incompatible("invalid operands in comparison",
4010 token.source_position, type_left, type_right);
4012 expression->expression.datatype = type_int;
4015 static void semantic_arithmetic_assign(binary_expression_t *expression)
4017 expression_t *left = expression->left;
4018 expression_t *right = expression->right;
4019 type_t *orig_type_left = left->base.datatype;
4020 type_t *orig_type_right = right->base.datatype;
4022 if(orig_type_left == NULL || orig_type_right == NULL)
4025 type_t *type_left = skip_typeref(orig_type_left);
4026 type_t *type_right = skip_typeref(orig_type_right);
4028 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4029 /* TODO: improve error message */
4030 parser_print_error_prefix();
4031 fprintf(stderr, "operation needs arithmetic types\n");
4035 /* combined instructions are tricky. We can't create an implicit cast on
4036 * the left side, because we need the uncasted form for the store.
4037 * The ast2firm pass has to know that left_type must be right_type
4038 * for the arithmeitc operation and create a cast by itself */
4039 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4040 expression->right = create_implicit_cast(right, arithmetic_type);
4041 expression->expression.datatype = type_left;
4044 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4046 expression_t *left = expression->left;
4047 expression_t *right = expression->right;
4048 type_t *orig_type_left = left->base.datatype;
4049 type_t *orig_type_right = right->base.datatype;
4051 if(orig_type_left == NULL || orig_type_right == NULL)
4054 type_t *type_left = skip_typeref(orig_type_left);
4055 type_t *type_right = skip_typeref(orig_type_right);
4057 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4058 /* combined instructions are tricky. We can't create an implicit cast on
4059 * the left side, because we need the uncasted form for the store.
4060 * The ast2firm pass has to know that left_type must be right_type
4061 * for the arithmeitc operation and create a cast by itself */
4062 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4063 expression->right = create_implicit_cast(right, arithmetic_type);
4064 expression->expression.datatype = type_left;
4065 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
4066 expression->expression.datatype = type_left;
4068 parser_print_error_prefix();
4069 fputs("Incompatible types ", stderr);
4070 print_type_quoted(orig_type_left);
4071 fputs(" and ", stderr);
4072 print_type_quoted(orig_type_right);
4073 fputs(" in assignment\n", stderr);
4078 static void semantic_logical_op(binary_expression_t *expression)
4080 expression_t *left = expression->left;
4081 expression_t *right = expression->right;
4082 type_t *orig_type_left = left->base.datatype;
4083 type_t *orig_type_right = right->base.datatype;
4085 if(orig_type_left == NULL || orig_type_right == NULL)
4088 type_t *type_left = skip_typeref(orig_type_left);
4089 type_t *type_right = skip_typeref(orig_type_right);
4091 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4092 /* TODO: improve error message */
4093 parser_print_error_prefix();
4094 fprintf(stderr, "operation needs scalar types\n");
4098 expression->expression.datatype = type_int;
4101 static bool has_const_fields(type_t *type)
4108 static void semantic_binexpr_assign(binary_expression_t *expression)
4110 expression_t *left = expression->left;
4111 type_t *orig_type_left = left->base.datatype;
4113 if(orig_type_left == NULL)
4116 type_t *type_left = revert_automatic_type_conversion(left);
4117 type_left = skip_typeref(orig_type_left);
4119 /* must be a modifiable lvalue */
4120 if (type_left->type == TYPE_ARRAY) {
4121 parser_print_error_prefix();
4122 fprintf(stderr, "Cannot assign to arrays ('");
4123 print_expression(left);
4124 fprintf(stderr, "')\n");
4127 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4128 parser_print_error_prefix();
4129 fprintf(stderr, "assignment to readonly location '");
4130 print_expression(left);
4131 fprintf(stderr, "' (type ");
4132 print_type_quoted(orig_type_left);
4133 fprintf(stderr, ")\n");
4136 if(is_type_incomplete(type_left)) {
4137 parser_print_error_prefix();
4138 fprintf(stderr, "left-hand side of assignment '");
4139 print_expression(left);
4140 fprintf(stderr, "' has incomplete type ");
4141 print_type_quoted(orig_type_left);
4142 fprintf(stderr, "\n");
4145 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4146 parser_print_error_prefix();
4147 fprintf(stderr, "can't assign to '");
4148 print_expression(left);
4149 fprintf(stderr, "' because compound type ");
4150 print_type_quoted(orig_type_left);
4151 fprintf(stderr, " has readonly fields\n");
4155 semantic_assign(orig_type_left, &expression->right, "assignment");
4157 expression->expression.datatype = orig_type_left;
4160 static void semantic_comma(binary_expression_t *expression)
4162 expression->expression.datatype = expression->right->base.datatype;
4165 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4166 static expression_t *parse_##binexpression_type(unsigned precedence, \
4167 expression_t *left) \
4171 expression_t *right = parse_sub_expression(precedence + lr); \
4173 binary_expression_t *binexpr \
4174 = allocate_ast_zero(sizeof(binexpr[0])); \
4175 binexpr->expression.type = EXPR_BINARY; \
4176 binexpr->type = binexpression_type; \
4177 binexpr->left = left; \
4178 binexpr->right = right; \
4181 return (expression_t*) binexpr; \
4184 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
4185 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
4186 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
4187 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
4188 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
4189 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
4190 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
4191 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
4192 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
4193 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
4194 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
4195 semantic_comparison, 1)
4196 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
4197 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
4198 semantic_comparison, 1)
4199 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
4200 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
4201 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
4202 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
4203 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
4204 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
4205 semantic_shift_op, 1)
4206 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
4207 semantic_shift_op, 1)
4208 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
4209 semantic_arithmetic_addsubb_assign, 0)
4210 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
4211 semantic_arithmetic_addsubb_assign, 0)
4212 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
4213 semantic_arithmetic_assign, 0)
4214 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
4215 semantic_arithmetic_assign, 0)
4216 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
4217 semantic_arithmetic_assign, 0)
4218 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
4219 semantic_arithmetic_assign, 0)
4220 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
4221 semantic_arithmetic_assign, 0)
4222 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
4223 semantic_arithmetic_assign, 0)
4224 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
4225 semantic_arithmetic_assign, 0)
4226 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
4227 semantic_arithmetic_assign, 0)
4229 static expression_t *parse_sub_expression(unsigned precedence)
4231 if(token.type < 0) {
4232 return expected_expression_error();
4235 expression_parser_function_t *parser
4236 = &expression_parsers[token.type];
4237 source_position_t source_position = token.source_position;
4240 if(parser->parser != NULL) {
4241 left = parser->parser(parser->precedence);
4243 left = parse_primary_expression();
4245 assert(left != NULL);
4246 left->base.source_position = source_position;
4249 if(token.type < 0) {
4250 return expected_expression_error();
4253 parser = &expression_parsers[token.type];
4254 if(parser->infix_parser == NULL)
4256 if(parser->infix_precedence < precedence)
4259 left = parser->infix_parser(parser->infix_precedence, left);
4261 assert(left != NULL);
4262 assert(left->type != EXPR_UNKNOWN);
4263 left->base.source_position = source_position;
4269 static expression_t *parse_expression(void)
4271 return parse_sub_expression(1);
4276 static void register_expression_parser(parse_expression_function parser,
4277 int token_type, unsigned precedence)
4279 expression_parser_function_t *entry = &expression_parsers[token_type];
4281 if(entry->parser != NULL) {
4282 fprintf(stderr, "for token ");
4283 print_token_type(stderr, (token_type_t) token_type);
4284 fprintf(stderr, "\n");
4285 panic("trying to register multiple expression parsers for a token");
4287 entry->parser = parser;
4288 entry->precedence = precedence;
4291 static void register_expression_infix_parser(
4292 parse_expression_infix_function parser, int token_type,
4293 unsigned precedence)
4295 expression_parser_function_t *entry = &expression_parsers[token_type];
4297 if(entry->infix_parser != NULL) {
4298 fprintf(stderr, "for token ");
4299 print_token_type(stderr, (token_type_t) token_type);
4300 fprintf(stderr, "\n");
4301 panic("trying to register multiple infix expression parsers for a "
4304 entry->infix_parser = parser;
4305 entry->infix_precedence = precedence;
4308 static void init_expression_parsers(void)
4310 memset(&expression_parsers, 0, sizeof(expression_parsers));
4312 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4313 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4314 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4315 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4316 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4317 T_GREATERGREATER, 16);
4318 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4319 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4320 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4321 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4322 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4323 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4324 T_GREATEREQUAL, 14);
4325 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4326 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4327 T_EXCLAMATIONMARKEQUAL, 13);
4328 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4329 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4330 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4331 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4332 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4333 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4334 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4335 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4336 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4337 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4338 T_ASTERISKEQUAL, 2);
4339 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4340 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4342 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4343 T_LESSLESSEQUAL, 2);
4344 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4345 T_GREATERGREATEREQUAL, 2);
4346 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4348 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4350 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4353 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4355 register_expression_infix_parser(parse_array_expression, '[', 30);
4356 register_expression_infix_parser(parse_call_expression, '(', 30);
4357 register_expression_infix_parser(parse_select_expression, '.', 30);
4358 register_expression_infix_parser(parse_select_expression,
4359 T_MINUSGREATER, 30);
4360 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4362 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4365 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4366 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4367 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4368 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4369 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4370 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4371 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4372 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4373 register_expression_parser(parse_sizeof, T_sizeof, 25);
4374 register_expression_parser(parse_extension, T___extension__, 25);
4375 register_expression_parser(parse_builtin_classify_type,
4376 T___builtin_classify_type, 25);
4379 static asm_constraint_t *parse_asm_constraints(void)
4381 asm_constraint_t *result = NULL;
4382 asm_constraint_t *last = NULL;
4384 while(token.type == T_STRING_LITERAL || token.type == '[') {
4385 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4386 memset(constraint, 0, sizeof(constraint[0]));
4388 if(token.type == '[') {
4390 if(token.type != T_IDENTIFIER) {
4391 parse_error_expected("while parsing asm constraint",
4395 constraint->symbol = token.v.symbol;
4400 constraint->constraints = parse_string_literals();
4402 constraint->expression = parse_expression();
4406 last->next = constraint;
4408 result = constraint;
4412 if(token.type != ',')
4420 static asm_clobber_t *parse_asm_clobbers(void)
4422 asm_clobber_t *result = NULL;
4423 asm_clobber_t *last = NULL;
4425 while(token.type == T_STRING_LITERAL) {
4426 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4427 clobber->clobber = parse_string_literals();
4430 last->next = clobber;
4436 if(token.type != ',')
4444 static statement_t *parse_asm_statement(void)
4448 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4449 statement->base.source_position = token.source_position;
4451 asm_statement_t *asm_statement = &statement->asms;
4453 if(token.type == T_volatile) {
4455 asm_statement->is_volatile = true;
4459 asm_statement->asm_text = parse_string_literals();
4461 if(token.type != ':')
4465 asm_statement->inputs = parse_asm_constraints();
4466 if(token.type != ':')
4470 asm_statement->outputs = parse_asm_constraints();
4471 if(token.type != ':')
4475 asm_statement->clobbers = parse_asm_clobbers();
4483 static statement_t *parse_case_statement(void)
4487 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4489 statement->base.source_position = token.source_position;
4490 statement->case_label.expression = parse_expression();
4493 statement->case_label.label_statement = parse_statement();
4498 static statement_t *parse_default_statement(void)
4502 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4504 statement->base.source_position = token.source_position;
4507 statement->label.label_statement = parse_statement();
4512 static declaration_t *get_label(symbol_t *symbol)
4514 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4515 assert(current_function != NULL);
4516 /* if we found a label in the same function, then we already created the
4518 if(candidate != NULL
4519 && candidate->parent_context == ¤t_function->context) {
4523 /* otherwise we need to create a new one */
4524 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4525 declaration->namespc = NAMESPACE_LABEL;
4526 declaration->symbol = symbol;
4528 label_push(declaration);
4533 static statement_t *parse_label_statement(void)
4535 assert(token.type == T_IDENTIFIER);
4536 symbol_t *symbol = token.v.symbol;
4539 declaration_t *label = get_label(symbol);
4541 /* if source position is already set then the label is defined twice,
4542 * otherwise it was just mentioned in a goto so far */
4543 if(label->source_position.input_name != NULL) {
4544 parser_print_error_prefix();
4545 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4546 parser_print_error_prefix_pos(label->source_position);
4547 fprintf(stderr, "previous definition of '%s' was here\n",
4550 label->source_position = token.source_position;
4553 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4555 label_statement->statement.type = STATEMENT_LABEL;
4556 label_statement->statement.source_position = token.source_position;
4557 label_statement->label = label;
4561 if(token.type == '}') {
4562 parse_error("label at end of compound statement");
4563 return (statement_t*) label_statement;
4565 label_statement->label_statement = parse_statement();
4568 return (statement_t*) label_statement;
4571 static statement_t *parse_if(void)
4575 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4576 statement->statement.type = STATEMENT_IF;
4577 statement->statement.source_position = token.source_position;
4580 statement->condition = parse_expression();
4583 statement->true_statement = parse_statement();
4584 if(token.type == T_else) {
4586 statement->false_statement = parse_statement();
4589 return (statement_t*) statement;
4592 static statement_t *parse_switch(void)
4596 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4597 statement->statement.type = STATEMENT_SWITCH;
4598 statement->statement.source_position = token.source_position;
4601 statement->expression = parse_expression();
4603 statement->body = parse_statement();
4605 return (statement_t*) statement;
4608 static statement_t *parse_while(void)
4612 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4613 statement->statement.type = STATEMENT_WHILE;
4614 statement->statement.source_position = token.source_position;
4617 statement->condition = parse_expression();
4619 statement->body = parse_statement();
4621 return (statement_t*) statement;
4624 static statement_t *parse_do(void)
4628 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4629 statement->statement.type = STATEMENT_DO_WHILE;
4630 statement->statement.source_position = token.source_position;
4632 statement->body = parse_statement();
4635 statement->condition = parse_expression();
4639 return (statement_t*) statement;
4642 static statement_t *parse_for(void)
4646 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4647 statement->statement.type = STATEMENT_FOR;
4648 statement->statement.source_position = token.source_position;
4652 int top = environment_top();
4653 context_t *last_context = context;
4654 set_context(&statement->context);
4656 if(token.type != ';') {
4657 if(is_declaration_specifier(&token, false)) {
4658 parse_declaration(record_declaration);
4660 statement->initialisation = parse_expression();
4667 if(token.type != ';') {
4668 statement->condition = parse_expression();
4671 if(token.type != ')') {
4672 statement->step = parse_expression();
4675 statement->body = parse_statement();
4677 assert(context == &statement->context);
4678 set_context(last_context);
4679 environment_pop_to(top);
4681 return (statement_t*) statement;
4684 static statement_t *parse_goto(void)
4688 if(token.type != T_IDENTIFIER) {
4689 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4693 symbol_t *symbol = token.v.symbol;
4696 declaration_t *label = get_label(symbol);
4698 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4700 statement->statement.type = STATEMENT_GOTO;
4701 statement->statement.source_position = token.source_position;
4703 statement->label = label;
4707 return (statement_t*) statement;
4710 static statement_t *parse_continue(void)
4715 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4716 statement->type = STATEMENT_CONTINUE;
4717 statement->base.source_position = token.source_position;
4722 static statement_t *parse_break(void)
4727 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4728 statement->type = STATEMENT_BREAK;
4729 statement->base.source_position = token.source_position;
4734 static statement_t *parse_return(void)
4738 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4740 statement->statement.type = STATEMENT_RETURN;
4741 statement->statement.source_position = token.source_position;
4743 assert(current_function->type->type == TYPE_FUNCTION);
4744 function_type_t *function_type = ¤t_function->type->function;
4745 type_t *return_type = function_type->result_type;
4747 expression_t *return_value = NULL;
4748 if(token.type != ';') {
4749 return_value = parse_expression();
4753 if(return_type == NULL)
4754 return (statement_t*) statement;
4756 return_type = skip_typeref(return_type);
4758 if(return_value != NULL) {
4759 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4761 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4762 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4763 parse_warning("'return' with a value, in function returning void");
4764 return_value = NULL;
4766 if(return_type != NULL) {
4767 semantic_assign(return_type, &return_value, "'return'");
4771 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4772 parse_warning("'return' without value, in function returning "
4776 statement->return_value = return_value;
4778 return (statement_t*) statement;
4781 static statement_t *parse_declaration_statement(void)
4783 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4785 statement->base.source_position = token.source_position;
4787 declaration_t *before = last_declaration;
4788 parse_declaration(record_declaration);
4790 if(before == NULL) {
4791 statement->declaration.declarations_begin = context->declarations;
4793 statement->declaration.declarations_begin = before->next;
4795 statement->declaration.declarations_end = last_declaration;
4800 static statement_t *parse_expression_statement(void)
4802 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4804 statement->base.source_position = token.source_position;
4805 statement->expression.expression = parse_expression();
4812 static statement_t *parse_statement(void)
4814 statement_t *statement = NULL;
4816 /* declaration or statement */
4817 switch(token.type) {
4819 statement = parse_asm_statement();
4823 statement = parse_case_statement();
4827 statement = parse_default_statement();
4831 statement = parse_compound_statement();
4835 statement = parse_if();
4839 statement = parse_switch();
4843 statement = parse_while();
4847 statement = parse_do();
4851 statement = parse_for();
4855 statement = parse_goto();
4859 statement = parse_continue();
4863 statement = parse_break();
4867 statement = parse_return();
4876 if(look_ahead(1)->type == ':') {
4877 statement = parse_label_statement();
4881 if(is_typedef_symbol(token.v.symbol)) {
4882 statement = parse_declaration_statement();
4886 statement = parse_expression_statement();
4889 case T___extension__:
4890 /* this can be a prefix to a declaration or an expression statement */
4891 /* we simply eat it now and parse the rest with tail recursion */
4894 } while(token.type == T___extension__);
4895 statement = parse_statement();
4899 statement = parse_declaration_statement();
4903 statement = parse_expression_statement();
4907 assert(statement == NULL
4908 || statement->base.source_position.input_name != NULL);
4913 static statement_t *parse_compound_statement(void)
4915 compound_statement_t *compound_statement
4916 = allocate_ast_zero(sizeof(compound_statement[0]));
4917 compound_statement->statement.type = STATEMENT_COMPOUND;
4918 compound_statement->statement.source_position = token.source_position;
4922 int top = environment_top();
4923 context_t *last_context = context;
4924 set_context(&compound_statement->context);
4926 statement_t *last_statement = NULL;
4928 while(token.type != '}' && token.type != T_EOF) {
4929 statement_t *statement = parse_statement();
4930 if(statement == NULL)
4933 if(last_statement != NULL) {
4934 last_statement->base.next = statement;
4936 compound_statement->statements = statement;
4939 while(statement->base.next != NULL)
4940 statement = statement->base.next;
4942 last_statement = statement;
4945 if(token.type != '}') {
4946 parser_print_error_prefix_pos(
4947 compound_statement->statement.source_position);
4948 fprintf(stderr, "end of file while looking for closing '}'\n");
4952 assert(context == &compound_statement->context);
4953 set_context(last_context);
4954 environment_pop_to(top);
4956 return (statement_t*) compound_statement;
4959 static void initialize_builtins(void)
4961 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
4962 type_wchar_ptr_t = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
4963 type_size_t = make_global_typedef("__SIZE_TYPE__",
4964 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
4965 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
4966 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
4969 static translation_unit_t *parse_translation_unit(void)
4971 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4973 assert(global_context == NULL);
4974 global_context = &unit->context;
4976 assert(context == NULL);
4977 set_context(&unit->context);
4979 initialize_builtins();
4981 while(token.type != T_EOF) {
4982 parse_external_declaration();
4985 assert(context == &unit->context);
4987 last_declaration = NULL;
4989 assert(global_context == &unit->context);
4990 global_context = NULL;
4995 translation_unit_t *parse(void)
4997 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4998 label_stack = NEW_ARR_F(stack_entry_t, 0);
4999 found_error = false;
5001 type_set_output(stderr);
5002 ast_set_output(stderr);
5004 lookahead_bufpos = 0;
5005 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5008 translation_unit_t *unit = parse_translation_unit();
5010 DEL_ARR_F(environment_stack);
5011 DEL_ARR_F(label_stack);
5019 void init_parser(void)
5021 init_expression_parsers();
5022 obstack_init(&temp_obst);
5024 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5025 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5026 TYPE_QUALIFIER_NONE);
5027 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5028 TYPE_QUALIFIER_NONE);
5029 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5030 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5031 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5032 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5033 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5036 void exit_parser(void)
5038 obstack_free(&temp_obst, NULL);