12 #include "type_hash.h"
14 #include "adt/bitfiddle.h"
15 #include "adt/error.h"
16 #include "adt/array.h"
18 //#define PRINT_TOKENS
19 //#define ABORT_ON_ERROR
20 #define MAX_LOOKAHEAD 2
24 declaration_t *old_declaration;
26 unsigned short namespc;
29 typedef struct declaration_specifiers_t declaration_specifiers_t;
30 struct declaration_specifiers_t {
31 source_position_t source_position;
32 unsigned char storage_class;
34 decl_modifiers_t decl_modifiers;
38 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
41 static token_t lookahead_buffer[MAX_LOOKAHEAD];
42 static int lookahead_bufpos;
43 static stack_entry_t *environment_stack = NULL;
44 static stack_entry_t *label_stack = NULL;
45 static context_t *global_context = NULL;
46 static context_t *context = NULL;
47 static declaration_t *last_declaration = NULL;
48 static declaration_t *current_function = NULL;
49 static struct obstack temp_obst;
50 static bool found_error;
52 static type_t *type_valist;
54 static statement_t *parse_compound_statement(void);
55 static statement_t *parse_statement(void);
57 static expression_t *parse_sub_expression(unsigned precedence);
58 static expression_t *parse_expression(void);
59 static type_t *parse_typename(void);
61 static void parse_compound_type_entries(void);
62 static declaration_t *parse_declarator(
63 const declaration_specifiers_t *specifiers, bool may_be_abstract);
64 static declaration_t *record_declaration(declaration_t *declaration);
66 static void semantic_comparison(binary_expression_t *expression);
68 #define STORAGE_CLASSES \
75 #define TYPE_QUALIFIERS \
82 #ifdef PROVIDE_COMPLEX
83 #define COMPLEX_SPECIFIERS \
85 #define IMAGINARY_SPECIFIERS \
88 #define COMPLEX_SPECIFIERS
89 #define IMAGINARY_SPECIFIERS
92 #define TYPE_SPECIFIERS \
107 case T___builtin_va_list: \
111 #define DECLARATION_START \
116 #define TYPENAME_START \
120 static void *allocate_ast_zero(size_t size)
122 void *res = allocate_ast(size);
123 memset(res, 0, size);
127 static size_t get_statement_struct_size(statement_type_t type)
129 static const size_t sizes[] = {
130 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
131 [STATEMENT_RETURN] = sizeof(return_statement_t),
132 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
133 [STATEMENT_IF] = sizeof(if_statement_t),
134 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
135 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
136 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
137 [STATEMENT_BREAK] = sizeof(statement_base_t),
138 [STATEMENT_GOTO] = sizeof(goto_statement_t),
139 [STATEMENT_LABEL] = sizeof(label_statement_t),
140 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
141 [STATEMENT_WHILE] = sizeof(while_statement_t),
142 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
143 [STATEMENT_FOR] = sizeof(for_statement_t),
144 [STATEMENT_ASM] = sizeof(asm_statement_t)
146 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
147 assert(sizes[type] != 0);
151 static statement_t *allocate_statement_zero(statement_type_t type)
153 size_t size = get_statement_struct_size(type);
154 statement_t *res = allocate_ast_zero(size);
156 res->base.type = type;
161 static size_t get_expression_struct_size(expression_type_t type)
163 static const size_t sizes[] = {
164 [EXPR_INVALID] = sizeof(expression_base_t),
165 [EXPR_REFERENCE] = sizeof(reference_expression_t),
166 [EXPR_CONST] = sizeof(const_expression_t),
167 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
168 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
169 [EXPR_CALL] = sizeof(call_expression_t),
170 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
171 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
172 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
173 [EXPR_SELECT] = sizeof(select_expression_t),
174 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
175 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
176 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
177 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
178 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
179 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
180 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
181 [EXPR_VA_START] = sizeof(va_start_expression_t),
182 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
183 [EXPR_STATEMENT] = sizeof(statement_expression_t),
185 if(type >= EXPR_UNARY_FIRST && type <= EXPR_UNARY_LAST) {
186 return sizes[EXPR_UNARY_FIRST];
188 if(type >= EXPR_BINARY_FIRST && type <= EXPR_BINARY_LAST) {
189 return sizes[EXPR_BINARY_FIRST];
191 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
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_WIDE_STRING] = sizeof(initializer_wide_string_t),
241 [INITIALIZER_LIST] = sizeof(initializer_list_t)
243 assert(type < sizeof(sizes) / sizeof(*sizes));
244 assert(sizes[type] != 0);
248 static initializer_t *allocate_initializer(initializer_type_t type)
250 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
256 static void free_type(void *type)
258 obstack_free(type_obst, type);
262 * returns the top element of the environment stack
264 static size_t environment_top(void)
266 return ARR_LEN(environment_stack);
269 static size_t label_top(void)
271 return ARR_LEN(label_stack);
276 static inline void next_token(void)
278 token = lookahead_buffer[lookahead_bufpos];
279 lookahead_buffer[lookahead_bufpos] = lexer_token;
282 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
285 print_token(stderr, &token);
286 fprintf(stderr, "\n");
290 static inline const token_t *look_ahead(int num)
292 assert(num > 0 && num <= MAX_LOOKAHEAD);
293 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
294 return &lookahead_buffer[pos];
297 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
299 static void error(void)
302 #ifdef ABORT_ON_ERROR
307 static void parser_print_prefix_pos(const source_position_t source_position)
309 fputs(source_position.input_name, stderr);
311 fprintf(stderr, "%u", source_position.linenr);
315 static void parser_print_error_prefix_pos(
316 const source_position_t source_position)
318 parser_print_prefix_pos(source_position);
319 fputs("error: ", stderr);
323 static void parser_print_error_prefix(void)
325 parser_print_error_prefix_pos(token.source_position);
328 static void parse_error(const char *message)
330 parser_print_error_prefix();
331 fprintf(stderr, "parse error: %s\n", message);
334 static void parser_print_warning_prefix_pos(
335 const source_position_t source_position)
337 parser_print_prefix_pos(source_position);
338 fputs("warning: ", stderr);
341 static void parser_print_warning_prefix(void)
343 parser_print_warning_prefix_pos(token.source_position);
346 static void parse_warning_pos(const source_position_t source_position,
347 const char *const message)
349 parser_print_prefix_pos(source_position);
350 fprintf(stderr, "warning: %s\n", message);
353 static void parse_warning(const char *message)
355 parse_warning_pos(token.source_position, message);
358 static void parse_error_expected(const char *message, ...)
363 if(message != NULL) {
364 parser_print_error_prefix();
365 fprintf(stderr, "%s\n", message);
367 parser_print_error_prefix();
368 fputs("Parse error: got ", stderr);
369 print_token(stderr, &token);
370 fputs(", expected ", stderr);
372 va_start(args, message);
373 token_type_t token_type = va_arg(args, token_type_t);
374 while(token_type != 0) {
378 fprintf(stderr, ", ");
380 print_token_type(stderr, token_type);
381 token_type = va_arg(args, token_type_t);
384 fprintf(stderr, "\n");
387 static void print_type_quoted(type_t *type)
394 static void type_error(const char *msg, const source_position_t source_position,
397 parser_print_error_prefix_pos(source_position);
398 fprintf(stderr, "%s, but found type ", msg);
399 print_type_quoted(type);
403 static void type_error_incompatible(const char *msg,
404 const source_position_t source_position, type_t *type1, type_t *type2)
406 parser_print_error_prefix_pos(source_position);
407 fprintf(stderr, "%s, incompatible types: ", msg);
408 print_type_quoted(type1);
409 fprintf(stderr, " - ");
410 print_type_quoted(type2);
411 fprintf(stderr, ")\n");
414 static void eat_block(void)
416 if(token.type == '{')
419 while(token.type != '}') {
420 if(token.type == T_EOF)
422 if(token.type == '{') {
431 static void eat_statement(void)
433 while(token.type != ';') {
434 if(token.type == T_EOF)
436 if(token.type == '}')
438 if(token.type == '{') {
447 static void eat_paren(void)
449 if(token.type == '(')
452 while(token.type != ')') {
453 if(token.type == T_EOF)
455 if(token.type == ')' || token.type == ';' || token.type == '}') {
458 if(token.type == '(') {
462 if(token.type == '{') {
471 #define expect(expected) \
472 if(UNLIKELY(token.type != (expected))) { \
473 parse_error_expected(NULL, (expected), 0); \
479 #define expect_block(expected) \
480 if(UNLIKELY(token.type != (expected))) { \
481 parse_error_expected(NULL, (expected), 0); \
487 #define expect_void(expected) \
488 if(UNLIKELY(token.type != (expected))) { \
489 parse_error_expected(NULL, (expected), 0); \
495 static void set_context(context_t *new_context)
497 context = new_context;
499 last_declaration = new_context->declarations;
500 if(last_declaration != NULL) {
501 while(last_declaration->next != NULL) {
502 last_declaration = last_declaration->next;
508 * called when we find a 2nd declarator for an identifier we already have a
511 static bool is_compatible_declaration(declaration_t *declaration,
512 declaration_t *previous)
514 /* happens for K&R style function parameters */
515 if(previous->type == NULL) {
516 previous->type = declaration->type;
520 type_t *type1 = skip_typeref(declaration->type);
521 type_t *type2 = skip_typeref(previous->type);
523 return types_compatible(type1, type2);
526 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
528 declaration_t *declaration = symbol->declaration;
529 for( ; declaration != NULL; declaration = declaration->symbol_next) {
530 if(declaration->namespc == namespc)
537 static const char *get_namespace_prefix(namespace_t namespc)
540 case NAMESPACE_NORMAL:
542 case NAMESPACE_UNION:
544 case NAMESPACE_STRUCT:
548 case NAMESPACE_LABEL:
551 panic("invalid namespace found");
555 * pushs an environment_entry on the environment stack and links the
556 * corresponding symbol to the new entry
558 static declaration_t *stack_push(stack_entry_t **stack_ptr,
559 declaration_t *declaration,
560 context_t *parent_context)
562 symbol_t *symbol = declaration->symbol;
563 namespace_t namespc = (namespace_t)declaration->namespc;
565 /* a declaration should be only pushed once */
566 declaration->parent_context = parent_context;
568 declaration_t *previous_declaration = get_declaration(symbol, namespc);
569 assert(declaration != previous_declaration);
570 if(previous_declaration != NULL
571 && previous_declaration->parent_context == context) {
572 if(!is_compatible_declaration(declaration, previous_declaration)) {
573 parser_print_error_prefix_pos(declaration->source_position);
574 fprintf(stderr, "definition of symbol '%s%s' with type ",
575 get_namespace_prefix(namespc), symbol->string);
576 print_type_quoted(declaration->type);
578 parser_print_error_prefix_pos(
579 previous_declaration->source_position);
580 fprintf(stderr, "is incompatible with previous declaration "
582 print_type_quoted(previous_declaration->type);
585 unsigned old_storage_class = previous_declaration->storage_class;
586 unsigned new_storage_class = declaration->storage_class;
587 type_t *type = previous_declaration->type;
588 type = skip_typeref(type);
590 if (current_function == NULL) {
591 if (old_storage_class != STORAGE_CLASS_STATIC &&
592 new_storage_class == STORAGE_CLASS_STATIC) {
593 parser_print_error_prefix_pos(declaration->source_position);
595 "static declaration of '%s' follows non-static declaration\n",
597 parser_print_error_prefix_pos(previous_declaration->source_position);
598 fprintf(stderr, "previous declaration of '%s' was here\n",
601 if (old_storage_class == STORAGE_CLASS_EXTERN) {
602 if (new_storage_class == STORAGE_CLASS_NONE) {
603 previous_declaration->storage_class = STORAGE_CLASS_NONE;
605 } else if(!is_type_function(type)) {
606 parser_print_warning_prefix_pos(declaration->source_position);
607 fprintf(stderr, "redundant declaration for '%s'\n",
609 parser_print_warning_prefix_pos(previous_declaration->source_position);
610 fprintf(stderr, "previous declaration of '%s' was here\n",
615 if (old_storage_class == STORAGE_CLASS_EXTERN &&
616 new_storage_class == STORAGE_CLASS_EXTERN) {
617 parser_print_warning_prefix_pos(declaration->source_position);
618 fprintf(stderr, "redundant extern declaration for '%s'\n",
620 parser_print_warning_prefix_pos(previous_declaration->source_position);
621 fprintf(stderr, "previous declaration of '%s' was here\n",
624 parser_print_error_prefix_pos(declaration->source_position);
625 if (old_storage_class == new_storage_class) {
626 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
628 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
630 parser_print_error_prefix_pos(previous_declaration->source_position);
631 fprintf(stderr, "previous declaration of '%s' was here\n",
636 return previous_declaration;
639 /* remember old declaration */
641 entry.symbol = symbol;
642 entry.old_declaration = symbol->declaration;
643 entry.namespc = (unsigned short) namespc;
644 ARR_APP1(stack_entry_t, *stack_ptr, entry);
646 /* replace/add declaration into declaration list of the symbol */
647 if(symbol->declaration == NULL) {
648 symbol->declaration = declaration;
650 declaration_t *iter_last = NULL;
651 declaration_t *iter = symbol->declaration;
652 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
653 /* replace an entry? */
654 if(iter->namespc == namespc) {
655 if(iter_last == NULL) {
656 symbol->declaration = declaration;
658 iter_last->symbol_next = declaration;
660 declaration->symbol_next = iter->symbol_next;
665 assert(iter_last->symbol_next == NULL);
666 iter_last->symbol_next = declaration;
673 static declaration_t *environment_push(declaration_t *declaration)
675 assert(declaration->source_position.input_name != NULL);
676 return stack_push(&environment_stack, declaration, context);
679 static declaration_t *label_push(declaration_t *declaration)
681 return stack_push(&label_stack, declaration, ¤t_function->context);
685 * pops symbols from the environment stack until @p new_top is the top element
687 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
689 stack_entry_t *stack = *stack_ptr;
690 size_t top = ARR_LEN(stack);
693 assert(new_top <= top);
697 for(i = top; i > new_top; --i) {
698 stack_entry_t *entry = &stack[i - 1];
700 declaration_t *old_declaration = entry->old_declaration;
701 symbol_t *symbol = entry->symbol;
702 namespace_t namespc = (namespace_t)entry->namespc;
704 /* replace/remove declaration */
705 declaration_t *declaration = symbol->declaration;
706 assert(declaration != NULL);
707 if(declaration->namespc == namespc) {
708 if(old_declaration == NULL) {
709 symbol->declaration = declaration->symbol_next;
711 symbol->declaration = old_declaration;
714 declaration_t *iter_last = declaration;
715 declaration_t *iter = declaration->symbol_next;
716 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
717 /* replace an entry? */
718 if(iter->namespc == namespc) {
719 assert(iter_last != NULL);
720 iter_last->symbol_next = old_declaration;
721 old_declaration->symbol_next = iter->symbol_next;
725 assert(iter != NULL);
729 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
732 static void environment_pop_to(size_t new_top)
734 stack_pop_to(&environment_stack, new_top);
737 static void label_pop_to(size_t new_top)
739 stack_pop_to(&label_stack, new_top);
743 static int get_rank(const type_t *type)
745 assert(!is_typeref(type));
746 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
747 * and esp. footnote 108). However we can't fold constants (yet), so we
748 * can't decide wether unsigned int is possible, while int always works.
749 * (unsigned int would be preferable when possible... for stuff like
750 * struct { enum { ... } bla : 4; } ) */
751 if(type->type == TYPE_ENUM)
752 return ATOMIC_TYPE_INT;
754 assert(type->type == TYPE_ATOMIC);
755 const atomic_type_t *atomic_type = &type->atomic;
756 atomic_type_type_t atype = atomic_type->atype;
760 static type_t *promote_integer(type_t *type)
762 if(get_rank(type) < ATOMIC_TYPE_INT)
768 static expression_t *create_cast_expression(expression_t *expression,
771 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
773 cast->unary.value = expression;
774 cast->base.datatype = dest_type;
779 static bool is_null_pointer_constant(const expression_t *expression)
781 /* skip void* cast */
782 if(expression->type == EXPR_UNARY_CAST
783 || expression->type == EXPR_UNARY_CAST_IMPLICIT) {
784 expression = expression->unary.value;
787 /* TODO: not correct yet, should be any constant integer expression
788 * which evaluates to 0 */
789 if (expression->type != EXPR_CONST)
792 type_t *const type = skip_typeref(expression->base.datatype);
793 if (!is_type_integer(type))
796 return expression->conste.v.int_value == 0;
799 static expression_t *create_implicit_cast(expression_t *expression,
802 type_t *source_type = expression->base.datatype;
804 if(source_type == NULL)
807 source_type = skip_typeref(source_type);
808 dest_type = skip_typeref(dest_type);
810 if(source_type == dest_type)
813 switch (dest_type->type) {
815 /* TODO warning for implicitly converting to enum */
817 if (source_type->type != TYPE_ATOMIC &&
818 source_type->type != TYPE_ENUM) {
819 panic("casting of non-atomic types not implemented yet");
822 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
823 type_error_incompatible("can't cast types",
824 expression->base.source_position, source_type,
829 return create_cast_expression(expression, dest_type);
832 switch (source_type->type) {
834 if (is_null_pointer_constant(expression)) {
835 return create_cast_expression(expression, dest_type);
840 if (pointers_compatible(source_type, dest_type)) {
841 return create_cast_expression(expression, dest_type);
846 array_type_t *array_type = &source_type->array;
847 pointer_type_t *pointer_type = &dest_type->pointer;
848 if (types_compatible(array_type->element_type,
849 pointer_type->points_to)) {
850 return create_cast_expression(expression, dest_type);
856 panic("casting of non-atomic types not implemented yet");
859 type_error_incompatible("can't implicitly cast types",
860 expression->base.source_position, source_type, dest_type);
864 panic("casting of non-atomic types not implemented yet");
868 /** Implements the rules from § 6.5.16.1 */
869 static void semantic_assign(type_t *orig_type_left, expression_t **right,
872 type_t *orig_type_right = (*right)->base.datatype;
874 if(orig_type_right == NULL)
877 type_t *const type_left = skip_typeref(orig_type_left);
878 type_t *const type_right = skip_typeref(orig_type_right);
880 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
881 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
882 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
883 && is_type_pointer(type_right))) {
884 *right = create_implicit_cast(*right, type_left);
888 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
889 pointer_type_t *pointer_type_left = &type_left->pointer;
890 pointer_type_t *pointer_type_right = &type_right->pointer;
891 type_t *points_to_left = pointer_type_left->points_to;
892 type_t *points_to_right = pointer_type_right->points_to;
894 points_to_left = skip_typeref(points_to_left);
895 points_to_right = skip_typeref(points_to_right);
897 /* the left type has all qualifiers from the right type */
898 unsigned missing_qualifiers
899 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
900 if(missing_qualifiers != 0) {
901 parser_print_error_prefix();
902 fprintf(stderr, "destination type ");
903 print_type_quoted(type_left);
904 fprintf(stderr, " in %s from type ", context);
905 print_type_quoted(type_right);
906 fprintf(stderr, " lacks qualifiers '");
907 print_type_qualifiers(missing_qualifiers);
908 fprintf(stderr, "' in pointed-to type\n");
912 points_to_left = get_unqualified_type(points_to_left);
913 points_to_right = get_unqualified_type(points_to_right);
915 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
916 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
917 && !types_compatible(points_to_left, points_to_right)) {
918 goto incompatible_assign_types;
921 *right = create_implicit_cast(*right, type_left);
925 if (is_type_compound(type_left)
926 && types_compatible(type_left, type_right)) {
927 *right = create_implicit_cast(*right, type_left);
931 incompatible_assign_types:
932 /* TODO: improve error message */
933 parser_print_error_prefix();
934 fprintf(stderr, "incompatible types in %s\n", context);
935 parser_print_error_prefix();
936 print_type_quoted(orig_type_left);
937 fputs(" <- ", stderr);
938 print_type_quoted(orig_type_right);
942 static expression_t *parse_constant_expression(void)
944 /* start parsing at precedence 7 (conditional expression) */
945 expression_t *result = parse_sub_expression(7);
947 if(!is_constant_expression(result)) {
948 parser_print_error_prefix_pos(result->base.source_position);
949 fprintf(stderr, "expression '");
950 print_expression(result);
951 fprintf(stderr, "' is not constant\n");
957 static expression_t *parse_assignment_expression(void)
959 /* start parsing at precedence 2 (assignment expression) */
960 return parse_sub_expression(2);
963 static type_t *make_global_typedef(const char *name, type_t *type)
965 symbol_t *symbol = symbol_table_insert(name);
967 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
968 declaration->namespc = NAMESPACE_NORMAL;
969 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
970 declaration->type = type;
971 declaration->symbol = symbol;
972 declaration->source_position = builtin_source_position;
974 record_declaration(declaration);
976 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
977 typedef_type->typedeft.declaration = declaration;
982 static const char *parse_string_literals(void)
984 assert(token.type == T_STRING_LITERAL);
985 const char *result = token.v.string;
989 while(token.type == T_STRING_LITERAL) {
990 result = concat_strings(result, token.v.string);
997 static void parse_attributes(void)
1000 switch(token.type) {
1001 case T___attribute__: {
1007 switch(token.type) {
1009 parse_error("EOF while parsing attribute");
1028 if(token.type != T_STRING_LITERAL) {
1029 parse_error_expected("while parsing assembler attribute",
1034 parse_string_literals();
1039 goto attributes_finished;
1043 attributes_finished:
1048 static designator_t *parse_designation(void)
1050 if(token.type != '[' && token.type != '.')
1053 designator_t *result = NULL;
1054 designator_t *last = NULL;
1057 designator_t *designator;
1058 switch(token.type) {
1060 designator = allocate_ast_zero(sizeof(designator[0]));
1062 designator->array_access = parse_constant_expression();
1066 designator = allocate_ast_zero(sizeof(designator[0]));
1068 if(token.type != T_IDENTIFIER) {
1069 parse_error_expected("while parsing designator",
1073 designator->symbol = token.v.symbol;
1081 assert(designator != NULL);
1083 last->next = designator;
1085 result = designator;
1092 static initializer_t *initializer_from_string(array_type_t *type,
1095 /* TODO: check len vs. size of array type */
1098 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1099 initializer->string.string = string;
1104 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1105 wide_string_t *const string)
1107 /* TODO: check len vs. size of array type */
1110 initializer_t *const initializer =
1111 allocate_initializer(INITIALIZER_WIDE_STRING);
1112 initializer->wide_string.string = *string;
1117 static initializer_t *initializer_from_expression(type_t *type,
1118 expression_t *expression)
1120 /* TODO check that expression is a constant expression */
1122 /* § 6.7.8.14/15 char array may be initialized by string literals */
1123 type_t *const expr_type = expression->base.datatype;
1124 if (is_type_array(type) && expr_type->type == TYPE_POINTER) {
1125 array_type_t *const array_type = &type->array;
1126 type_t *const element_type = skip_typeref(array_type->element_type);
1128 if (element_type->type == TYPE_ATOMIC) {
1129 switch (expression->type) {
1130 case EXPR_STRING_LITERAL:
1131 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1132 return initializer_from_string(array_type,
1133 expression->string.value);
1136 case EXPR_WIDE_STRING_LITERAL: {
1137 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1138 if (get_unqualified_type(element_type) == bare_wchar_type) {
1139 return initializer_from_wide_string(array_type,
1140 &expression->wide_string.value);
1149 type_t *expression_type = skip_typeref(expression->base.datatype);
1150 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1151 semantic_assign(type, &expression, "initializer");
1153 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1154 result->value.value = expression;
1162 static initializer_t *parse_sub_initializer(type_t *type,
1163 expression_t *expression,
1164 type_t *expression_type);
1166 static initializer_t *parse_sub_initializer_elem(type_t *type)
1168 if(token.type == '{') {
1169 return parse_sub_initializer(type, NULL, NULL);
1172 expression_t *expression = parse_assignment_expression();
1173 type_t *expression_type = skip_typeref(expression->base.datatype);
1175 return parse_sub_initializer(type, expression, expression_type);
1178 static bool had_initializer_brace_warning;
1180 static initializer_t *parse_sub_initializer(type_t *type,
1181 expression_t *expression,
1182 type_t *expression_type)
1184 if(is_type_scalar(type)) {
1185 /* there might be extra {} hierarchies */
1186 if(token.type == '{') {
1188 if(!had_initializer_brace_warning) {
1189 parse_warning("braces around scalar initializer");
1190 had_initializer_brace_warning = true;
1192 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1193 if(token.type == ',') {
1195 /* TODO: warn about excessive elements */
1201 if(expression == NULL) {
1202 expression = parse_assignment_expression();
1204 return initializer_from_expression(type, expression);
1207 /* does the expression match the currently looked at object to initalize */
1208 if(expression != NULL) {
1209 initializer_t *result = initializer_from_expression(type, expression);
1214 bool read_paren = false;
1215 if(token.type == '{') {
1220 /* descend into subtype */
1221 initializer_t *result = NULL;
1222 initializer_t **elems;
1223 if(is_type_array(type)) {
1224 array_type_t *array_type = &type->array;
1225 type_t *element_type = array_type->element_type;
1226 element_type = skip_typeref(element_type);
1229 had_initializer_brace_warning = false;
1230 if(expression == NULL) {
1231 sub = parse_sub_initializer_elem(element_type);
1233 sub = parse_sub_initializer(element_type, expression,
1237 /* didn't match the subtypes -> try the parent type */
1239 assert(!read_paren);
1243 elems = NEW_ARR_F(initializer_t*, 0);
1244 ARR_APP1(initializer_t*, elems, sub);
1247 if(token.type == '}')
1250 if(token.type == '}')
1253 sub = parse_sub_initializer_elem(element_type);
1255 /* TODO error, do nicer cleanup */
1256 parse_error("member initializer didn't match");
1260 ARR_APP1(initializer_t*, elems, sub);
1263 assert(is_type_compound(type));
1264 compound_type_t *compound_type = &type->compound;
1265 context_t *context = &compound_type->declaration->context;
1267 declaration_t *first = context->declarations;
1270 type_t *first_type = first->type;
1271 first_type = skip_typeref(first_type);
1274 had_initializer_brace_warning = false;
1275 if(expression == NULL) {
1276 sub = parse_sub_initializer_elem(first_type);
1278 sub = parse_sub_initializer(first_type, expression,expression_type);
1281 /* didn't match the subtypes -> try our parent type */
1283 assert(!read_paren);
1287 elems = NEW_ARR_F(initializer_t*, 0);
1288 ARR_APP1(initializer_t*, elems, sub);
1290 declaration_t *iter = first->next;
1291 for( ; iter != NULL; iter = iter->next) {
1292 if(iter->symbol == NULL)
1294 if(iter->namespc != NAMESPACE_NORMAL)
1297 if(token.type == '}')
1300 if(token.type == '}')
1303 type_t *iter_type = iter->type;
1304 iter_type = skip_typeref(iter_type);
1306 sub = parse_sub_initializer_elem(iter_type);
1308 /* TODO error, do nicer cleanup*/
1309 parse_error("member initializer didn't match");
1313 ARR_APP1(initializer_t*, elems, sub);
1317 int len = ARR_LEN(elems);
1318 size_t elems_size = sizeof(initializer_t*) * len;
1320 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1322 init->initializer.type = INITIALIZER_LIST;
1324 memcpy(init->initializers, elems, elems_size);
1327 result = (initializer_t*) init;
1330 if(token.type == ',')
1337 static initializer_t *parse_initializer(type_t *type)
1339 initializer_t *result;
1341 type = skip_typeref(type);
1343 if(token.type != '{') {
1344 expression_t *expression = parse_assignment_expression();
1345 initializer_t *initializer = initializer_from_expression(type, expression);
1346 if(initializer == NULL) {
1347 parser_print_error_prefix();
1348 fprintf(stderr, "initializer expression '");
1349 print_expression(expression);
1350 fprintf(stderr, "', type ");
1351 print_type_quoted(expression->base.datatype);
1352 fprintf(stderr, " is incompatible with type ");
1353 print_type_quoted(type);
1354 fprintf(stderr, "\n");
1359 if(is_type_scalar(type)) {
1363 expression_t *expression = parse_assignment_expression();
1364 result = initializer_from_expression(type, expression);
1366 if(token.type == ',')
1372 result = parse_sub_initializer(type, NULL, NULL);
1380 static declaration_t *parse_compound_type_specifier(bool is_struct)
1388 symbol_t *symbol = NULL;
1389 declaration_t *declaration = NULL;
1391 if (token.type == T___attribute__) {
1396 if(token.type == T_IDENTIFIER) {
1397 symbol = token.v.symbol;
1401 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1403 declaration = get_declaration(symbol, NAMESPACE_UNION);
1405 } else if(token.type != '{') {
1407 parse_error_expected("while parsing struct type specifier",
1408 T_IDENTIFIER, '{', 0);
1410 parse_error_expected("while parsing union type specifier",
1411 T_IDENTIFIER, '{', 0);
1417 if(declaration == NULL) {
1418 declaration = allocate_ast_zero(sizeof(declaration[0]));
1421 declaration->namespc = NAMESPACE_STRUCT;
1423 declaration->namespc = NAMESPACE_UNION;
1425 declaration->source_position = token.source_position;
1426 declaration->symbol = symbol;
1427 record_declaration(declaration);
1430 if(token.type == '{') {
1431 if(declaration->init.is_defined) {
1432 assert(symbol != NULL);
1433 parser_print_error_prefix();
1434 fprintf(stderr, "multiple definition of %s %s\n",
1435 is_struct ? "struct" : "union", symbol->string);
1436 declaration->context.declarations = NULL;
1438 declaration->init.is_defined = true;
1440 int top = environment_top();
1441 context_t *last_context = context;
1442 set_context(&declaration->context);
1444 parse_compound_type_entries();
1447 assert(context == &declaration->context);
1448 set_context(last_context);
1449 environment_pop_to(top);
1455 static void parse_enum_entries(enum_type_t *const enum_type)
1459 if(token.type == '}') {
1461 parse_error("empty enum not allowed");
1466 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1468 if(token.type != T_IDENTIFIER) {
1469 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1473 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1474 entry->type = (type_t*) enum_type;
1475 entry->symbol = token.v.symbol;
1476 entry->source_position = token.source_position;
1479 if(token.type == '=') {
1481 entry->init.enum_value = parse_constant_expression();
1486 record_declaration(entry);
1488 if(token.type != ',')
1491 } while(token.type != '}');
1496 static type_t *parse_enum_specifier(void)
1500 declaration_t *declaration;
1503 if(token.type == T_IDENTIFIER) {
1504 symbol = token.v.symbol;
1507 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1508 } else if(token.type != '{') {
1509 parse_error_expected("while parsing enum type specifier",
1510 T_IDENTIFIER, '{', 0);
1517 if(declaration == NULL) {
1518 declaration = allocate_ast_zero(sizeof(declaration[0]));
1520 declaration->namespc = NAMESPACE_ENUM;
1521 declaration->source_position = token.source_position;
1522 declaration->symbol = symbol;
1525 type_t *const type = allocate_type_zero(TYPE_ENUM);
1526 type->enumt.declaration = declaration;
1528 if(token.type == '{') {
1529 if(declaration->init.is_defined) {
1530 parser_print_error_prefix();
1531 fprintf(stderr, "multiple definitions of enum %s\n",
1534 record_declaration(declaration);
1535 declaration->init.is_defined = 1;
1537 parse_enum_entries(&type->enumt);
1545 * if a symbol is a typedef to another type, return true
1547 static bool is_typedef_symbol(symbol_t *symbol)
1549 const declaration_t *const declaration =
1550 get_declaration(symbol, NAMESPACE_NORMAL);
1552 declaration != NULL &&
1553 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1556 static type_t *parse_typeof(void)
1564 expression_t *expression = NULL;
1567 switch(token.type) {
1568 case T___extension__:
1569 /* this can be a prefix to a typename or an expression */
1570 /* we simply eat it now. */
1573 } while(token.type == T___extension__);
1577 if(is_typedef_symbol(token.v.symbol)) {
1578 type = parse_typename();
1580 expression = parse_expression();
1581 type = expression->base.datatype;
1586 type = parse_typename();
1590 expression = parse_expression();
1591 type = expression->base.datatype;
1597 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1598 typeof_type->typeoft.expression = expression;
1599 typeof_type->typeoft.typeof_type = type;
1605 SPECIFIER_SIGNED = 1 << 0,
1606 SPECIFIER_UNSIGNED = 1 << 1,
1607 SPECIFIER_LONG = 1 << 2,
1608 SPECIFIER_INT = 1 << 3,
1609 SPECIFIER_DOUBLE = 1 << 4,
1610 SPECIFIER_CHAR = 1 << 5,
1611 SPECIFIER_SHORT = 1 << 6,
1612 SPECIFIER_LONG_LONG = 1 << 7,
1613 SPECIFIER_FLOAT = 1 << 8,
1614 SPECIFIER_BOOL = 1 << 9,
1615 SPECIFIER_VOID = 1 << 10,
1616 #ifdef PROVIDE_COMPLEX
1617 SPECIFIER_COMPLEX = 1 << 11,
1618 SPECIFIER_IMAGINARY = 1 << 12,
1622 static type_t *create_builtin_type(symbol_t *const symbol,
1623 type_t *const real_type)
1625 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1626 type->builtin.symbol = symbol;
1627 type->builtin.real_type = real_type;
1629 type_t *result = typehash_insert(type);
1630 if (type != result) {
1637 static type_t *get_typedef_type(symbol_t *symbol)
1639 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1640 if(declaration == NULL
1641 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1644 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1645 type->typedeft.declaration = declaration;
1650 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1652 type_t *type = NULL;
1653 unsigned type_qualifiers = 0;
1654 unsigned type_specifiers = 0;
1657 specifiers->source_position = token.source_position;
1660 switch(token.type) {
1663 #define MATCH_STORAGE_CLASS(token, class) \
1665 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1666 parse_error("multiple storage classes in declaration " \
1669 specifiers->storage_class = class; \
1673 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1674 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1675 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1676 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1677 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1680 switch (specifiers->storage_class) {
1681 case STORAGE_CLASS_NONE:
1682 specifiers->storage_class = STORAGE_CLASS_THREAD;
1685 case STORAGE_CLASS_EXTERN:
1686 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1689 case STORAGE_CLASS_STATIC:
1690 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1694 parse_error("multiple storage classes in declaration specifiers");
1700 /* type qualifiers */
1701 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1703 type_qualifiers |= qualifier; \
1707 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1708 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1709 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1711 case T___extension__:
1716 /* type specifiers */
1717 #define MATCH_SPECIFIER(token, specifier, name) \
1720 if(type_specifiers & specifier) { \
1721 parse_error("multiple " name " type specifiers given"); \
1723 type_specifiers |= specifier; \
1727 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1728 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1729 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1730 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1731 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1732 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1733 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1734 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1735 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1736 #ifdef PROVIDE_COMPLEX
1737 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1738 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1741 /* only in microsoft mode */
1742 specifiers->decl_modifiers |= DM_FORCEINLINE;
1746 specifiers->is_inline = true;
1751 if(type_specifiers & SPECIFIER_LONG_LONG) {
1752 parse_error("multiple type specifiers given");
1753 } else if(type_specifiers & SPECIFIER_LONG) {
1754 type_specifiers |= SPECIFIER_LONG_LONG;
1756 type_specifiers |= SPECIFIER_LONG;
1760 /* TODO: if type != NULL for the following rules should issue
1763 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1765 type->compound.declaration = parse_compound_type_specifier(true);
1769 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1771 type->compound.declaration = parse_compound_type_specifier(false);
1775 type = parse_enum_specifier();
1778 type = parse_typeof();
1780 case T___builtin_va_list:
1781 type = duplicate_type(type_valist);
1785 case T___attribute__:
1790 case T_IDENTIFIER: {
1791 type_t *typedef_type = get_typedef_type(token.v.symbol);
1793 if(typedef_type == NULL)
1794 goto finish_specifiers;
1797 type = typedef_type;
1801 /* function specifier */
1803 goto finish_specifiers;
1810 atomic_type_type_t atomic_type;
1812 /* match valid basic types */
1813 switch(type_specifiers) {
1814 case SPECIFIER_VOID:
1815 atomic_type = ATOMIC_TYPE_VOID;
1817 case SPECIFIER_CHAR:
1818 atomic_type = ATOMIC_TYPE_CHAR;
1820 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1821 atomic_type = ATOMIC_TYPE_SCHAR;
1823 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1824 atomic_type = ATOMIC_TYPE_UCHAR;
1826 case SPECIFIER_SHORT:
1827 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1828 case SPECIFIER_SHORT | SPECIFIER_INT:
1829 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1830 atomic_type = ATOMIC_TYPE_SHORT;
1832 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1833 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1834 atomic_type = ATOMIC_TYPE_USHORT;
1837 case SPECIFIER_SIGNED:
1838 case SPECIFIER_SIGNED | SPECIFIER_INT:
1839 atomic_type = ATOMIC_TYPE_INT;
1841 case SPECIFIER_UNSIGNED:
1842 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1843 atomic_type = ATOMIC_TYPE_UINT;
1845 case SPECIFIER_LONG:
1846 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1847 case SPECIFIER_LONG | SPECIFIER_INT:
1848 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1849 atomic_type = ATOMIC_TYPE_LONG;
1851 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1852 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1853 atomic_type = ATOMIC_TYPE_ULONG;
1855 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1856 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1857 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1858 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1860 atomic_type = ATOMIC_TYPE_LONGLONG;
1862 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1863 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1865 atomic_type = ATOMIC_TYPE_ULONGLONG;
1867 case SPECIFIER_FLOAT:
1868 atomic_type = ATOMIC_TYPE_FLOAT;
1870 case SPECIFIER_DOUBLE:
1871 atomic_type = ATOMIC_TYPE_DOUBLE;
1873 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1874 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1876 case SPECIFIER_BOOL:
1877 atomic_type = ATOMIC_TYPE_BOOL;
1879 #ifdef PROVIDE_COMPLEX
1880 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1881 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1883 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1884 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1886 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1887 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1889 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1890 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1892 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1893 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1895 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1896 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1900 /* invalid specifier combination, give an error message */
1901 if(type_specifiers == 0) {
1903 parse_warning("no type specifiers in declaration, using int");
1904 atomic_type = ATOMIC_TYPE_INT;
1907 parse_error("no type specifiers given in declaration");
1909 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1910 (type_specifiers & SPECIFIER_UNSIGNED)) {
1911 parse_error("signed and unsigned specifiers gives");
1912 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1913 parse_error("only integer types can be signed or unsigned");
1915 parse_error("multiple datatypes in declaration");
1917 atomic_type = ATOMIC_TYPE_INVALID;
1920 type = allocate_type_zero(TYPE_ATOMIC);
1921 type->atomic.atype = atomic_type;
1924 if(type_specifiers != 0) {
1925 parse_error("multiple datatypes in declaration");
1929 type->base.qualifiers = type_qualifiers;
1931 type_t *result = typehash_insert(type);
1932 if(newtype && result != type) {
1936 specifiers->type = result;
1939 static type_qualifiers_t parse_type_qualifiers(void)
1941 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1944 switch(token.type) {
1945 /* type qualifiers */
1946 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1947 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1948 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1951 return type_qualifiers;
1956 static declaration_t *parse_identifier_list(void)
1958 declaration_t *declarations = NULL;
1959 declaration_t *last_declaration = NULL;
1961 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1963 declaration->source_position = token.source_position;
1964 declaration->symbol = token.v.symbol;
1967 if(last_declaration != NULL) {
1968 last_declaration->next = declaration;
1970 declarations = declaration;
1972 last_declaration = declaration;
1974 if(token.type != ',')
1977 } while(token.type == T_IDENTIFIER);
1979 return declarations;
1982 static void semantic_parameter(declaration_t *declaration)
1984 /* TODO: improve error messages */
1986 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1987 parse_error("typedef not allowed in parameter list");
1988 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1989 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1990 parse_error("parameter may only have none or register storage class");
1993 type_t *orig_type = declaration->type;
1994 if(orig_type == NULL)
1996 type_t *type = skip_typeref(orig_type);
1998 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1999 * into a pointer. § 6.7.5.3 (7) */
2000 if (is_type_array(type)) {
2001 const array_type_t *arr_type = &type->array;
2002 type_t *element_type = arr_type->element_type;
2004 type = make_pointer_type(element_type, type->base.qualifiers);
2006 declaration->type = type;
2009 if(is_type_incomplete(type)) {
2010 parser_print_error_prefix();
2011 fprintf(stderr, "incomplete type (");
2012 print_type_quoted(orig_type);
2013 fprintf(stderr, ") not allowed for parameter '%s'\n",
2014 declaration->symbol->string);
2018 static declaration_t *parse_parameter(void)
2020 declaration_specifiers_t specifiers;
2021 memset(&specifiers, 0, sizeof(specifiers));
2023 parse_declaration_specifiers(&specifiers);
2025 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2027 semantic_parameter(declaration);
2032 static declaration_t *parse_parameters(function_type_t *type)
2034 if(token.type == T_IDENTIFIER) {
2035 symbol_t *symbol = token.v.symbol;
2036 if(!is_typedef_symbol(symbol)) {
2037 type->kr_style_parameters = true;
2038 return parse_identifier_list();
2042 if(token.type == ')') {
2043 type->unspecified_parameters = 1;
2046 if(token.type == T_void && look_ahead(1)->type == ')') {
2051 declaration_t *declarations = NULL;
2052 declaration_t *declaration;
2053 declaration_t *last_declaration = NULL;
2054 function_parameter_t *parameter;
2055 function_parameter_t *last_parameter = NULL;
2058 switch(token.type) {
2062 return declarations;
2065 case T___extension__:
2067 declaration = parse_parameter();
2069 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2070 memset(parameter, 0, sizeof(parameter[0]));
2071 parameter->type = declaration->type;
2073 if(last_parameter != NULL) {
2074 last_declaration->next = declaration;
2075 last_parameter->next = parameter;
2077 type->parameters = parameter;
2078 declarations = declaration;
2080 last_parameter = parameter;
2081 last_declaration = declaration;
2085 return declarations;
2087 if(token.type != ',')
2088 return declarations;
2098 } construct_type_type_t;
2100 typedef struct construct_type_t construct_type_t;
2101 struct construct_type_t {
2102 construct_type_type_t type;
2103 construct_type_t *next;
2106 typedef struct parsed_pointer_t parsed_pointer_t;
2107 struct parsed_pointer_t {
2108 construct_type_t construct_type;
2109 type_qualifiers_t type_qualifiers;
2112 typedef struct construct_function_type_t construct_function_type_t;
2113 struct construct_function_type_t {
2114 construct_type_t construct_type;
2115 type_t *function_type;
2118 typedef struct parsed_array_t parsed_array_t;
2119 struct parsed_array_t {
2120 construct_type_t construct_type;
2121 type_qualifiers_t type_qualifiers;
2127 typedef struct construct_base_type_t construct_base_type_t;
2128 struct construct_base_type_t {
2129 construct_type_t construct_type;
2133 static construct_type_t *parse_pointer_declarator(void)
2137 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2138 memset(pointer, 0, sizeof(pointer[0]));
2139 pointer->construct_type.type = CONSTRUCT_POINTER;
2140 pointer->type_qualifiers = parse_type_qualifiers();
2142 return (construct_type_t*) pointer;
2145 static construct_type_t *parse_array_declarator(void)
2149 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2150 memset(array, 0, sizeof(array[0]));
2151 array->construct_type.type = CONSTRUCT_ARRAY;
2153 if(token.type == T_static) {
2154 array->is_static = true;
2158 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2159 if(type_qualifiers != 0) {
2160 if(token.type == T_static) {
2161 array->is_static = true;
2165 array->type_qualifiers = type_qualifiers;
2167 if(token.type == '*' && look_ahead(1)->type == ']') {
2168 array->is_variable = true;
2170 } else if(token.type != ']') {
2171 array->size = parse_assignment_expression();
2176 return (construct_type_t*) array;
2179 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2183 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2185 declaration_t *parameters = parse_parameters(&type->function);
2186 if(declaration != NULL) {
2187 declaration->context.declarations = parameters;
2190 construct_function_type_t *construct_function_type =
2191 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2192 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2193 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2194 construct_function_type->function_type = type;
2198 return (construct_type_t*) construct_function_type;
2201 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2202 bool may_be_abstract)
2204 /* construct a single linked list of construct_type_t's which describe
2205 * how to construct the final declarator type */
2206 construct_type_t *first = NULL;
2207 construct_type_t *last = NULL;
2210 while(token.type == '*') {
2211 construct_type_t *type = parse_pointer_declarator();
2222 /* TODO: find out if this is correct */
2225 construct_type_t *inner_types = NULL;
2227 switch(token.type) {
2229 if(declaration == NULL) {
2230 parse_error("no identifier expected in typename");
2232 declaration->symbol = token.v.symbol;
2233 declaration->source_position = token.source_position;
2239 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2245 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2246 /* avoid a loop in the outermost scope, because eat_statement doesn't
2248 if(token.type == '}' && current_function == NULL) {
2256 construct_type_t *p = last;
2259 construct_type_t *type;
2260 switch(token.type) {
2262 type = parse_function_declarator(declaration);
2265 type = parse_array_declarator();
2268 goto declarator_finished;
2271 /* insert in the middle of the list (behind p) */
2273 type->next = p->next;
2284 declarator_finished:
2287 /* append inner_types at the end of the list, we don't to set last anymore
2288 * as it's not needed anymore */
2290 assert(first == NULL);
2291 first = inner_types;
2293 last->next = inner_types;
2299 static type_t *construct_declarator_type(construct_type_t *construct_list,
2302 construct_type_t *iter = construct_list;
2303 for( ; iter != NULL; iter = iter->next) {
2304 switch(iter->type) {
2305 case CONSTRUCT_INVALID:
2306 panic("invalid type construction found");
2307 case CONSTRUCT_FUNCTION: {
2308 construct_function_type_t *construct_function_type
2309 = (construct_function_type_t*) iter;
2311 type_t *function_type = construct_function_type->function_type;
2313 function_type->function.return_type = type;
2315 type = function_type;
2319 case CONSTRUCT_POINTER: {
2320 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2321 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2322 pointer_type->pointer.points_to = type;
2323 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2325 type = pointer_type;
2329 case CONSTRUCT_ARRAY: {
2330 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2331 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2333 array_type->base.qualifiers = parsed_array->type_qualifiers;
2334 array_type->array.element_type = type;
2335 array_type->array.is_static = parsed_array->is_static;
2336 array_type->array.is_variable = parsed_array->is_variable;
2337 array_type->array.size = parsed_array->size;
2344 type_t *hashed_type = typehash_insert(type);
2345 if(hashed_type != type) {
2346 /* the function type was constructed earlier freeing it here will
2347 * destroy other types... */
2348 if(iter->type != CONSTRUCT_FUNCTION) {
2358 static declaration_t *parse_declarator(
2359 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2361 type_t *type = specifiers->type;
2362 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2363 declaration->storage_class = specifiers->storage_class;
2364 declaration->decl_modifiers = specifiers->decl_modifiers;
2365 declaration->is_inline = specifiers->is_inline;
2367 construct_type_t *construct_type
2368 = parse_inner_declarator(declaration, may_be_abstract);
2369 declaration->type = construct_declarator_type(construct_type, type);
2371 if(construct_type != NULL) {
2372 obstack_free(&temp_obst, construct_type);
2378 static type_t *parse_abstract_declarator(type_t *base_type)
2380 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2382 type_t *result = construct_declarator_type(construct_type, base_type);
2383 if(construct_type != NULL) {
2384 obstack_free(&temp_obst, construct_type);
2390 static declaration_t *record_declaration(declaration_t *declaration)
2392 assert(declaration->parent_context == NULL);
2393 assert(context != NULL);
2395 symbol_t *symbol = declaration->symbol;
2396 if(symbol != NULL) {
2397 declaration_t *alias = environment_push(declaration);
2398 if(alias != declaration)
2401 declaration->parent_context = context;
2404 if(last_declaration != NULL) {
2405 last_declaration->next = declaration;
2407 context->declarations = declaration;
2409 last_declaration = declaration;
2414 static void parser_error_multiple_definition(declaration_t *declaration,
2415 const source_position_t source_position)
2417 parser_print_error_prefix_pos(source_position);
2418 fprintf(stderr, "multiple definition of symbol '%s'\n",
2419 declaration->symbol->string);
2420 parser_print_error_prefix_pos(declaration->source_position);
2421 fprintf(stderr, "this is the location of the previous definition.\n");
2424 static bool is_declaration_specifier(const token_t *token,
2425 bool only_type_specifiers)
2427 switch(token->type) {
2431 return is_typedef_symbol(token->v.symbol);
2433 case T___extension__:
2436 return !only_type_specifiers;
2443 static void parse_init_declarator_rest(declaration_t *declaration)
2447 type_t *orig_type = declaration->type;
2448 type_t *type = NULL;
2449 if(orig_type != NULL)
2450 type = skip_typeref(orig_type);
2452 if(declaration->init.initializer != NULL) {
2453 parser_error_multiple_definition(declaration, token.source_position);
2456 initializer_t *initializer = parse_initializer(type);
2458 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2459 * the array type size */
2460 if(type != NULL && is_type_array(type) && initializer != NULL) {
2461 array_type_t *array_type = &type->array;
2463 if(array_type->size == NULL) {
2464 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2466 cnst->base.datatype = type_size_t;
2468 switch (initializer->type) {
2469 case INITIALIZER_LIST: {
2470 initializer_list_t *const list = &initializer->list;
2471 cnst->conste.v.int_value = list->len;
2475 case INITIALIZER_STRING: {
2476 initializer_string_t *const string = &initializer->string;
2477 cnst->conste.v.int_value = strlen(string->string) + 1;
2481 case INITIALIZER_WIDE_STRING: {
2482 initializer_wide_string_t *const string = &initializer->wide_string;
2483 cnst->conste.v.int_value = string->string.size;
2488 panic("invalid initializer type");
2491 array_type->size = cnst;
2495 if(type != NULL && is_type_function(type)) {
2496 parser_print_error_prefix_pos(declaration->source_position);
2497 fprintf(stderr, "initializers not allowed for function types at "
2498 "declator '%s' (type ", declaration->symbol->string);
2499 print_type_quoted(orig_type);
2500 fprintf(stderr, ")\n");
2502 declaration->init.initializer = initializer;
2506 /* parse rest of a declaration without any declarator */
2507 static void parse_anonymous_declaration_rest(
2508 const declaration_specifiers_t *specifiers,
2509 parsed_declaration_func finished_declaration)
2513 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2515 declaration->type = specifiers->type;
2516 declaration->storage_class = specifiers->storage_class;
2517 declaration->source_position = specifiers->source_position;
2519 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2520 parse_warning_pos(declaration->source_position,
2521 "useless storage class in empty declaration");
2524 type_t *type = declaration->type;
2525 switch (type->type) {
2526 case TYPE_COMPOUND_STRUCT:
2527 case TYPE_COMPOUND_UNION: {
2528 const compound_type_t *compound_type = &type->compound;
2529 if (compound_type->declaration->symbol == NULL) {
2530 parse_warning_pos(declaration->source_position,
2531 "unnamed struct/union that defines no instances");
2540 parse_warning_pos(declaration->source_position,
2541 "empty declaration");
2545 finished_declaration(declaration);
2548 static void parse_declaration_rest(declaration_t *ndeclaration,
2549 const declaration_specifiers_t *specifiers,
2550 parsed_declaration_func finished_declaration)
2553 declaration_t *declaration = finished_declaration(ndeclaration);
2555 type_t *orig_type = declaration->type;
2556 type_t *type = skip_typeref(orig_type);
2558 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2559 parser_print_warning_prefix_pos(declaration->source_position);
2560 fprintf(stderr, "variable '%s' declared 'inline'\n",
2561 declaration->symbol->string);
2564 if(token.type == '=') {
2565 parse_init_declarator_rest(declaration);
2568 if(token.type != ',')
2572 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2577 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2579 /* TODO: check that it was actually a parameter that gets a type */
2581 /* we should have a declaration for the parameter in the current
2583 return record_declaration(declaration);
2586 static void parse_declaration(parsed_declaration_func finished_declaration)
2588 declaration_specifiers_t specifiers;
2589 memset(&specifiers, 0, sizeof(specifiers));
2590 parse_declaration_specifiers(&specifiers);
2592 if(token.type == ';') {
2593 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2595 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2596 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2600 static void parse_kr_declaration_list(declaration_t *declaration)
2602 type_t *type = skip_typeref(declaration->type);
2603 if(!is_type_function(type))
2606 if(!type->function.kr_style_parameters)
2609 /* push function parameters */
2610 int top = environment_top();
2611 context_t *last_context = context;
2612 set_context(&declaration->context);
2614 declaration_t *parameter = declaration->context.declarations;
2615 for( ; parameter != NULL; parameter = parameter->next) {
2616 environment_push(parameter);
2619 /* parse declaration list */
2620 while(is_declaration_specifier(&token, false)) {
2621 parse_declaration(finished_kr_declaration);
2624 /* pop function parameters */
2625 assert(context == &declaration->context);
2626 set_context(last_context);
2627 environment_pop_to(top);
2629 /* update function type */
2630 type_t *new_type = duplicate_type(type);
2631 new_type->function.kr_style_parameters = false;
2633 function_parameter_t *parameters = NULL;
2634 function_parameter_t *last_parameter = NULL;
2636 declaration_t *parameter_declaration = declaration->context.declarations;
2637 for( ; parameter_declaration != NULL;
2638 parameter_declaration = parameter_declaration->next) {
2639 type_t *parameter_type = parameter_declaration->type;
2640 if(parameter_type == NULL) {
2642 parser_print_error_prefix();
2643 fprintf(stderr, "no type specified for function parameter '%s'\n",
2644 parameter_declaration->symbol->string);
2646 parser_print_warning_prefix();
2647 fprintf(stderr, "no type specified for function parameter '%s', "
2648 "using int\n", parameter_declaration->symbol->string);
2649 parameter_type = type_int;
2650 parameter_declaration->type = parameter_type;
2654 semantic_parameter(parameter_declaration);
2655 parameter_type = parameter_declaration->type;
2657 function_parameter_t *function_parameter
2658 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2659 memset(function_parameter, 0, sizeof(function_parameter[0]));
2661 function_parameter->type = parameter_type;
2662 if(last_parameter != NULL) {
2663 last_parameter->next = function_parameter;
2665 parameters = function_parameter;
2667 last_parameter = function_parameter;
2669 new_type->function.parameters = parameters;
2671 type = typehash_insert(new_type);
2672 if(type != new_type) {
2673 obstack_free(type_obst, new_type);
2676 declaration->type = type;
2679 static void parse_external_declaration(void)
2681 /* function-definitions and declarations both start with declaration
2683 declaration_specifiers_t specifiers;
2684 memset(&specifiers, 0, sizeof(specifiers));
2685 parse_declaration_specifiers(&specifiers);
2687 /* must be a declaration */
2688 if(token.type == ';') {
2689 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2693 /* declarator is common to both function-definitions and declarations */
2694 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2696 /* must be a declaration */
2697 if(token.type == ',' || token.type == '=' || token.type == ';') {
2698 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2702 /* must be a function definition */
2703 parse_kr_declaration_list(ndeclaration);
2705 if(token.type != '{') {
2706 parse_error_expected("while parsing function definition", '{', 0);
2711 type_t *type = ndeclaration->type;
2717 /* note that we don't skip typerefs: the standard doesn't allow them here
2718 * (so we can't use is_type_function here) */
2719 if(type->type != TYPE_FUNCTION) {
2720 parser_print_error_prefix();
2721 fprintf(stderr, "declarator '");
2722 print_type_ext(type, ndeclaration->symbol, NULL);
2723 fprintf(stderr, "' has a body but is not a function type.\n");
2728 /* § 6.7.5.3 (14) a function definition with () means no
2729 * parameters (and not unspecified parameters) */
2730 if(type->function.unspecified_parameters) {
2731 type_t *duplicate = duplicate_type(type);
2732 duplicate->function.unspecified_parameters = false;
2734 type = typehash_insert(duplicate);
2735 if(type != duplicate) {
2736 obstack_free(type_obst, duplicate);
2738 ndeclaration->type = type;
2741 declaration_t *declaration = record_declaration(ndeclaration);
2742 if(ndeclaration != declaration) {
2743 memcpy(&declaration->context, &ndeclaration->context,
2744 sizeof(declaration->context));
2746 type = skip_typeref(declaration->type);
2748 /* push function parameters and switch context */
2749 int top = environment_top();
2750 context_t *last_context = context;
2751 set_context(&declaration->context);
2753 declaration_t *parameter = declaration->context.declarations;
2754 for( ; parameter != NULL; parameter = parameter->next) {
2755 environment_push(parameter);
2758 if(declaration->init.statement != NULL) {
2759 parser_error_multiple_definition(declaration, token.source_position);
2761 goto end_of_parse_external_declaration;
2763 /* parse function body */
2764 int label_stack_top = label_top();
2765 declaration_t *old_current_function = current_function;
2766 current_function = declaration;
2768 declaration->init.statement = parse_compound_statement();
2770 assert(current_function == declaration);
2771 current_function = old_current_function;
2772 label_pop_to(label_stack_top);
2775 end_of_parse_external_declaration:
2776 assert(context == &declaration->context);
2777 set_context(last_context);
2778 environment_pop_to(top);
2781 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2784 if(token.type == ':') {
2786 parse_constant_expression();
2787 /* TODO (bitfields) */
2789 declaration_t *declaration = parse_declarator(specifiers, /*may_be_abstract=*/true);
2791 /* TODO: check constraints for struct declarations */
2792 /* TODO: check for doubled fields */
2793 record_declaration(declaration);
2795 if(token.type == ':') {
2797 parse_constant_expression();
2798 /* TODO (bitfields) */
2802 if(token.type != ',')
2809 static void parse_compound_type_entries(void)
2813 while(token.type != '}' && token.type != T_EOF) {
2814 declaration_specifiers_t specifiers;
2815 memset(&specifiers, 0, sizeof(specifiers));
2816 parse_declaration_specifiers(&specifiers);
2818 parse_struct_declarators(&specifiers);
2820 if(token.type == T_EOF) {
2821 parse_error("EOF while parsing struct");
2826 static type_t *parse_typename(void)
2828 declaration_specifiers_t specifiers;
2829 memset(&specifiers, 0, sizeof(specifiers));
2830 parse_declaration_specifiers(&specifiers);
2831 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2832 /* TODO: improve error message, user does probably not know what a
2833 * storage class is...
2835 parse_error("typename may not have a storage class");
2838 type_t *result = parse_abstract_declarator(specifiers.type);
2846 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2847 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2848 expression_t *left);
2850 typedef struct expression_parser_function_t expression_parser_function_t;
2851 struct expression_parser_function_t {
2852 unsigned precedence;
2853 parse_expression_function parser;
2854 unsigned infix_precedence;
2855 parse_expression_infix_function infix_parser;
2858 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2860 static expression_t *create_invalid_expression(void)
2862 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2863 expression->base.source_position = token.source_position;
2867 static expression_t *expected_expression_error(void)
2869 parser_print_error_prefix();
2870 fprintf(stderr, "expected expression, got token ");
2871 print_token(stderr, &token);
2872 fprintf(stderr, "\n");
2876 return create_invalid_expression();
2879 static expression_t *parse_string_const(void)
2881 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2882 cnst->base.datatype = type_string;
2883 cnst->string.value = parse_string_literals();
2888 static expression_t *parse_wide_string_const(void)
2890 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2891 cnst->base.datatype = type_wchar_t_ptr;
2892 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2897 static expression_t *parse_int_const(void)
2899 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2900 cnst->base.datatype = token.datatype;
2901 cnst->conste.v.int_value = token.v.intvalue;
2908 static expression_t *parse_float_const(void)
2910 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2911 cnst->base.datatype = token.datatype;
2912 cnst->conste.v.float_value = token.v.floatvalue;
2919 static declaration_t *create_implicit_function(symbol_t *symbol,
2920 const source_position_t source_position)
2922 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2923 ntype->function.return_type = type_int;
2924 ntype->function.unspecified_parameters = true;
2926 type_t *type = typehash_insert(ntype);
2931 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2933 declaration->storage_class = STORAGE_CLASS_EXTERN;
2934 declaration->type = type;
2935 declaration->symbol = symbol;
2936 declaration->source_position = source_position;
2938 /* prepend the implicit definition to the global context
2939 * this is safe since the symbol wasn't declared as anything else yet
2941 assert(symbol->declaration == NULL);
2943 context_t *last_context = context;
2944 context = global_context;
2946 environment_push(declaration);
2947 declaration->next = context->declarations;
2948 context->declarations = declaration;
2950 context = last_context;
2955 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2957 function_parameter_t *parameter
2958 = obstack_alloc(type_obst, sizeof(parameter[0]));
2959 memset(parameter, 0, sizeof(parameter[0]));
2960 parameter->type = argument_type;
2962 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2963 type->function.return_type = return_type;
2964 type->function.parameters = parameter;
2966 type_t *result = typehash_insert(type);
2967 if(result != type) {
2974 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2976 switch(symbol->ID) {
2977 case T___builtin_alloca:
2978 return make_function_1_type(type_void_ptr, type_size_t);
2979 case T___builtin_nan:
2980 return make_function_1_type(type_double, type_string);
2981 case T___builtin_nanf:
2982 return make_function_1_type(type_float, type_string);
2983 case T___builtin_nand:
2984 return make_function_1_type(type_long_double, type_string);
2985 case T___builtin_va_end:
2986 return make_function_1_type(type_void, type_valist);
2988 panic("not implemented builtin symbol found");
2993 * performs automatic type cast as described in § 6.3.2.1
2995 static type_t *automatic_type_conversion(type_t *orig_type)
2997 if(orig_type == NULL)
3000 type_t *type = skip_typeref(orig_type);
3001 if(is_type_array(type)) {
3002 array_type_t *array_type = &type->array;
3003 type_t *element_type = array_type->element_type;
3004 unsigned qualifiers = array_type->type.qualifiers;
3006 return make_pointer_type(element_type, qualifiers);
3009 if(is_type_function(type)) {
3010 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3017 * reverts the automatic casts of array to pointer types and function
3018 * to function-pointer types as defined § 6.3.2.1
3020 type_t *revert_automatic_type_conversion(const expression_t *expression)
3022 if(expression->base.datatype == NULL)
3025 switch(expression->type) {
3026 case EXPR_REFERENCE: {
3027 const reference_expression_t *ref = &expression->reference;
3028 return ref->declaration->type;
3031 const select_expression_t *select = &expression->select;
3032 return select->compound_entry->type;
3034 case EXPR_UNARY_DEREFERENCE: {
3035 expression_t *value = expression->unary.value;
3036 type_t *type = skip_typeref(value->base.datatype);
3037 pointer_type_t *pointer_type = &type->pointer;
3039 return pointer_type->points_to;
3041 case EXPR_BUILTIN_SYMBOL: {
3042 const builtin_symbol_expression_t *builtin
3043 = &expression->builtin_symbol;
3044 return get_builtin_symbol_type(builtin->symbol);
3046 case EXPR_ARRAY_ACCESS: {
3047 const array_access_expression_t *array_access
3048 = &expression->array_access;
3049 const expression_t *array_ref = array_access->array_ref;
3050 type_t *type_left = skip_typeref(array_ref->base.datatype);
3051 assert(is_type_pointer(type_left));
3052 pointer_type_t *pointer_type = &type_left->pointer;
3053 return pointer_type->points_to;
3060 return expression->base.datatype;
3063 static expression_t *parse_reference(void)
3065 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3067 reference_expression_t *ref = &expression->reference;
3068 ref->symbol = token.v.symbol;
3070 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3072 source_position_t source_position = token.source_position;
3075 if(declaration == NULL) {
3077 /* an implicitly defined function */
3078 if(token.type == '(') {
3079 parser_print_prefix_pos(token.source_position);
3080 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3081 ref->symbol->string);
3083 declaration = create_implicit_function(ref->symbol,
3088 parser_print_error_prefix();
3089 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3094 type_t *type = declaration->type;
3095 /* we always do the auto-type conversions; the & and sizeof parser contains
3096 * code to revert this! */
3097 type = automatic_type_conversion(type);
3099 ref->declaration = declaration;
3100 ref->expression.datatype = type;
3105 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3109 /* TODO check if explicit cast is allowed and issue warnings/errors */
3112 static expression_t *parse_cast(void)
3114 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3116 cast->base.source_position = token.source_position;
3118 type_t *type = parse_typename();
3121 expression_t *value = parse_sub_expression(20);
3123 check_cast_allowed(value, type);
3125 cast->base.datatype = type;
3126 cast->unary.value = value;
3131 static expression_t *parse_statement_expression(void)
3133 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3135 statement_t *statement = parse_compound_statement();
3136 expression->statement.statement = statement;
3137 if(statement == NULL) {
3142 assert(statement->type == STATEMENT_COMPOUND);
3143 compound_statement_t *compound_statement = &statement->compound;
3145 /* find last statement and use it's type */
3146 const statement_t *last_statement = NULL;
3147 const statement_t *iter = compound_statement->statements;
3148 for( ; iter != NULL; iter = iter->base.next) {
3149 last_statement = iter;
3152 if(last_statement->type == STATEMENT_EXPRESSION) {
3153 const expression_statement_t *expression_statement
3154 = &last_statement->expression;
3155 expression->base.datatype
3156 = expression_statement->expression->base.datatype;
3158 expression->base.datatype = type_void;
3166 static expression_t *parse_brace_expression(void)
3170 switch(token.type) {
3172 /* gcc extension: a stement expression */
3173 return parse_statement_expression();
3177 return parse_cast();
3179 if(is_typedef_symbol(token.v.symbol)) {
3180 return parse_cast();
3184 expression_t *result = parse_expression();
3190 static expression_t *parse_function_keyword(void)
3195 if (current_function == NULL) {
3196 parse_error("'__func__' used outside of a function");
3199 string_literal_expression_t *expression
3200 = allocate_ast_zero(sizeof(expression[0]));
3202 expression->expression.type = EXPR_FUNCTION;
3203 expression->expression.datatype = type_string;
3204 expression->value = current_function->symbol->string;
3206 return (expression_t*) expression;
3209 static expression_t *parse_pretty_function_keyword(void)
3211 eat(T___PRETTY_FUNCTION__);
3214 if (current_function == NULL) {
3215 parse_error("'__PRETTY_FUNCTION__' used outside of a function");
3218 string_literal_expression_t *expression
3219 = allocate_ast_zero(sizeof(expression[0]));
3221 expression->expression.type = EXPR_PRETTY_FUNCTION;
3222 expression->expression.datatype = type_string;
3223 expression->value = current_function->symbol->string;
3225 return (expression_t*) expression;
3228 static designator_t *parse_designator(void)
3230 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3232 if(token.type != T_IDENTIFIER) {
3233 parse_error_expected("while parsing member designator",
3238 result->symbol = token.v.symbol;
3241 designator_t *last_designator = result;
3243 if(token.type == '.') {
3245 if(token.type != T_IDENTIFIER) {
3246 parse_error_expected("while parsing member designator",
3251 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3252 designator->symbol = token.v.symbol;
3255 last_designator->next = designator;
3256 last_designator = designator;
3259 if(token.type == '[') {
3261 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3262 designator->array_access = parse_expression();
3263 if(designator->array_access == NULL) {
3269 last_designator->next = designator;
3270 last_designator = designator;
3279 static expression_t *parse_offsetof(void)
3281 eat(T___builtin_offsetof);
3283 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3284 expression->base.datatype = type_size_t;
3287 expression->offsetofe.type = parse_typename();
3289 expression->offsetofe.designator = parse_designator();
3295 static expression_t *parse_va_start(void)
3297 eat(T___builtin_va_start);
3299 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3302 expression->va_starte.ap = parse_assignment_expression();
3304 expression_t *const expr = parse_assignment_expression();
3305 if (expr->type == EXPR_REFERENCE) {
3306 declaration_t *const decl = expr->reference.declaration;
3307 if (decl->parent_context == ¤t_function->context &&
3308 decl->next == NULL) {
3309 expression->va_starte.parameter = decl;
3314 parser_print_error_prefix_pos(expr->base.source_position);
3315 fprintf(stderr, "second argument of 'va_start' must be last parameter "
3316 "of the current function\n");
3318 return create_invalid_expression();
3321 static expression_t *parse_va_arg(void)
3323 eat(T___builtin_va_arg);
3325 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3328 expression->va_arge.ap = parse_assignment_expression();
3330 expression->base.datatype = parse_typename();
3336 static expression_t *parse_builtin_symbol(void)
3338 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3340 symbol_t *symbol = token.v.symbol;
3342 expression->builtin_symbol.symbol = symbol;
3345 type_t *type = get_builtin_symbol_type(symbol);
3346 type = automatic_type_conversion(type);
3348 expression->base.datatype = type;
3352 static expression_t *parse_compare_builtin(void)
3354 expression_t *expression;
3356 switch(token.type) {
3357 case T___builtin_isgreater:
3358 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3360 case T___builtin_isgreaterequal:
3361 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3363 case T___builtin_isless:
3364 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3366 case T___builtin_islessequal:
3367 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3369 case T___builtin_islessgreater:
3370 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3372 case T___builtin_isunordered:
3373 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3376 panic("invalid compare builtin found");
3382 expression->binary.left = parse_assignment_expression();
3384 expression->binary.right = parse_assignment_expression();
3387 type_t *orig_type_left = expression->binary.left->base.datatype;
3388 type_t *orig_type_right = expression->binary.right->base.datatype;
3389 if(orig_type_left == NULL || orig_type_right == NULL)
3392 type_t *type_left = skip_typeref(orig_type_left);
3393 type_t *type_right = skip_typeref(orig_type_right);
3394 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3395 type_error_incompatible("invalid operands in comparison",
3396 token.source_position, type_left, type_right);
3398 semantic_comparison(&expression->binary);
3404 static expression_t *parse_builtin_expect(void)
3406 eat(T___builtin_expect);
3408 expression_t *expression
3409 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3412 expression->binary.left = parse_assignment_expression();
3414 expression->binary.right = parse_constant_expression();
3417 expression->base.datatype = expression->binary.left->base.datatype;
3422 static expression_t *parse_primary_expression(void)
3424 switch(token.type) {
3426 return parse_int_const();
3427 case T_FLOATINGPOINT:
3428 return parse_float_const();
3429 case T_STRING_LITERAL: /* TODO merge */
3430 return parse_string_const();
3431 case T_WIDE_STRING_LITERAL:
3432 return parse_wide_string_const();
3434 return parse_reference();
3435 case T___FUNCTION__:
3437 return parse_function_keyword();
3438 case T___PRETTY_FUNCTION__:
3439 return parse_pretty_function_keyword();
3440 case T___builtin_offsetof:
3441 return parse_offsetof();
3442 case T___builtin_va_start:
3443 return parse_va_start();
3444 case T___builtin_va_arg:
3445 return parse_va_arg();
3446 case T___builtin_expect:
3447 return parse_builtin_expect();
3448 case T___builtin_nanf:
3449 case T___builtin_alloca:
3450 case T___builtin_va_end:
3451 return parse_builtin_symbol();
3452 case T___builtin_isgreater:
3453 case T___builtin_isgreaterequal:
3454 case T___builtin_isless:
3455 case T___builtin_islessequal:
3456 case T___builtin_islessgreater:
3457 case T___builtin_isunordered:
3458 return parse_compare_builtin();
3461 return parse_brace_expression();
3464 parser_print_error_prefix();
3465 fprintf(stderr, "unexpected token ");
3466 print_token(stderr, &token);
3467 fprintf(stderr, "\n");
3470 return create_invalid_expression();
3473 static expression_t *parse_array_expression(unsigned precedence,
3480 expression_t *inside = parse_expression();
3482 array_access_expression_t *array_access
3483 = allocate_ast_zero(sizeof(array_access[0]));
3485 array_access->expression.type = EXPR_ARRAY_ACCESS;
3487 type_t *type_left = left->base.datatype;
3488 type_t *type_inside = inside->base.datatype;
3489 type_t *return_type = NULL;
3491 if(type_left != NULL && type_inside != NULL) {
3492 type_left = skip_typeref(type_left);
3493 type_inside = skip_typeref(type_inside);
3495 if(is_type_pointer(type_left)) {
3496 pointer_type_t *pointer = &type_left->pointer;
3497 return_type = pointer->points_to;
3498 array_access->array_ref = left;
3499 array_access->index = inside;
3500 } else if(is_type_pointer(type_inside)) {
3501 pointer_type_t *pointer = &type_inside->pointer;
3502 return_type = pointer->points_to;
3503 array_access->array_ref = inside;
3504 array_access->index = left;
3505 array_access->flipped = true;
3507 parser_print_error_prefix();
3508 fprintf(stderr, "array access on object with non-pointer types ");
3509 print_type_quoted(type_left);
3510 fprintf(stderr, ", ");
3511 print_type_quoted(type_inside);
3512 fprintf(stderr, "\n");
3515 array_access->array_ref = left;
3516 array_access->index = inside;
3519 if(token.type != ']') {
3520 parse_error_expected("Problem while parsing array access", ']', 0);
3521 return (expression_t*) array_access;
3525 return_type = automatic_type_conversion(return_type);
3526 array_access->expression.datatype = return_type;
3528 return (expression_t*) array_access;
3531 static expression_t *parse_sizeof(unsigned precedence)
3535 sizeof_expression_t *sizeof_expression
3536 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3537 sizeof_expression->expression.type = EXPR_SIZEOF;
3538 sizeof_expression->expression.datatype = type_size_t;
3540 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3542 sizeof_expression->type = parse_typename();
3545 expression_t *expression = parse_sub_expression(precedence);
3546 expression->base.datatype = revert_automatic_type_conversion(expression);
3548 sizeof_expression->type = expression->base.datatype;
3549 sizeof_expression->size_expression = expression;
3552 return (expression_t*) sizeof_expression;
3555 static expression_t *parse_select_expression(unsigned precedence,
3556 expression_t *compound)
3559 assert(token.type == '.' || token.type == T_MINUSGREATER);
3561 bool is_pointer = (token.type == T_MINUSGREATER);
3564 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3565 select->select.compound = compound;
3567 if(token.type != T_IDENTIFIER) {
3568 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3571 symbol_t *symbol = token.v.symbol;
3572 select->select.symbol = symbol;
3575 type_t *orig_type = compound->base.datatype;
3576 if(orig_type == NULL)
3577 return create_invalid_expression();
3579 type_t *type = skip_typeref(orig_type);
3581 type_t *type_left = type;
3583 if(type->type != TYPE_POINTER) {
3584 parser_print_error_prefix();
3585 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3586 print_type_quoted(orig_type);
3587 fputc('\n', stderr);
3588 return create_invalid_expression();
3590 pointer_type_t *pointer_type = &type->pointer;
3591 type_left = pointer_type->points_to;
3593 type_left = skip_typeref(type_left);
3595 if(type_left->type != TYPE_COMPOUND_STRUCT
3596 && type_left->type != TYPE_COMPOUND_UNION) {
3597 parser_print_error_prefix();
3598 fprintf(stderr, "request for member '%s' in something not a struct or "
3599 "union, but ", symbol->string);
3600 print_type_quoted(type_left);
3601 fputc('\n', stderr);
3602 return create_invalid_expression();
3605 compound_type_t *compound_type = &type_left->compound;
3606 declaration_t *declaration = compound_type->declaration;
3608 if(!declaration->init.is_defined) {
3609 parser_print_error_prefix();
3610 fprintf(stderr, "request for member '%s' of incomplete type ",
3612 print_type_quoted(type_left);
3613 fputc('\n', stderr);
3614 return create_invalid_expression();
3617 declaration_t *iter = declaration->context.declarations;
3618 for( ; iter != NULL; iter = iter->next) {
3619 if(iter->symbol == symbol) {
3624 parser_print_error_prefix();
3625 print_type_quoted(type_left);
3626 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3627 return create_invalid_expression();
3630 /* we always do the auto-type conversions; the & and sizeof parser contains
3631 * code to revert this! */
3632 type_t *expression_type = automatic_type_conversion(iter->type);
3634 select->select.compound_entry = iter;
3635 select->base.datatype = expression_type;
3639 static expression_t *parse_call_expression(unsigned precedence,
3640 expression_t *expression)
3643 expression_t *result = allocate_expression_zero(EXPR_CALL);
3645 call_expression_t *call = &result->call;
3646 call->function = expression;
3648 function_type_t *function_type = NULL;
3649 type_t *orig_type = expression->base.datatype;
3650 if(orig_type != NULL) {
3651 type_t *type = skip_typeref(orig_type);
3653 if(is_type_pointer(type)) {
3654 pointer_type_t *pointer_type = &type->pointer;
3656 type = skip_typeref(pointer_type->points_to);
3658 if (is_type_function(type)) {
3659 function_type = &type->function;
3660 call->expression.datatype = function_type->return_type;
3663 if(function_type == NULL) {
3664 parser_print_error_prefix();
3665 fputs("called object '", stderr);
3666 print_expression(expression);
3667 fputs("' (type ", stderr);
3668 print_type_quoted(orig_type);
3669 fputs(") is not a pointer to a function\n", stderr);
3671 function_type = NULL;
3672 call->expression.datatype = NULL;
3676 /* parse arguments */
3679 if(token.type != ')') {
3680 call_argument_t *last_argument = NULL;
3683 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3685 argument->expression = parse_assignment_expression();
3686 if(last_argument == NULL) {
3687 call->arguments = argument;
3689 last_argument->next = argument;
3691 last_argument = argument;
3693 if(token.type != ',')
3700 if(function_type != NULL) {
3701 function_parameter_t *parameter = function_type->parameters;
3702 call_argument_t *argument = call->arguments;
3703 for( ; parameter != NULL && argument != NULL;
3704 parameter = parameter->next, argument = argument->next) {
3705 type_t *expected_type = parameter->type;
3706 /* TODO report context in error messages */
3707 argument->expression = create_implicit_cast(argument->expression,
3710 /* too few parameters */
3711 if(parameter != NULL) {
3712 parser_print_error_prefix();
3713 fprintf(stderr, "too few arguments to function '");
3714 print_expression(expression);
3715 fprintf(stderr, "'\n");
3716 } else if(argument != NULL) {
3717 /* too many parameters */
3718 if(!function_type->variadic
3719 && !function_type->unspecified_parameters) {
3720 parser_print_error_prefix();
3721 fprintf(stderr, "too many arguments to function '");
3722 print_expression(expression);
3723 fprintf(stderr, "'\n");
3725 /* do default promotion */
3726 for( ; argument != NULL; argument = argument->next) {
3727 type_t *type = argument->expression->base.datatype;
3732 type = skip_typeref(type);
3733 if(is_type_integer(type)) {
3734 type = promote_integer(type);
3735 } else if(type == type_float) {
3739 argument->expression
3740 = create_implicit_cast(argument->expression, type);
3749 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3751 static bool same_compound_type(const type_t *type1, const type_t *type2)
3753 if(!is_type_compound(type1))
3755 if(type1->type != type2->type)
3758 const compound_type_t *compound1 = &type1->compound;
3759 const compound_type_t *compound2 = &type2->compound;
3761 return compound1->declaration == compound2->declaration;
3764 static expression_t *parse_conditional_expression(unsigned precedence,
3765 expression_t *expression)
3769 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3771 conditional_expression_t *conditional = &result->conditional;
3772 conditional->condition = expression;
3775 type_t *condition_type_orig = expression->base.datatype;
3776 if(condition_type_orig != NULL) {
3777 type_t *condition_type = skip_typeref(condition_type_orig);
3778 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3779 type_error("expected a scalar type in conditional condition",
3780 expression->base.source_position, condition_type_orig);
3784 expression_t *true_expression = parse_expression();
3786 expression_t *false_expression = parse_sub_expression(precedence);
3788 conditional->true_expression = true_expression;
3789 conditional->false_expression = false_expression;
3791 type_t *orig_true_type = true_expression->base.datatype;
3792 type_t *orig_false_type = false_expression->base.datatype;
3793 if(orig_true_type == NULL || orig_false_type == NULL)
3796 type_t *true_type = skip_typeref(orig_true_type);
3797 type_t *false_type = skip_typeref(orig_false_type);
3800 type_t *result_type = NULL;
3801 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3802 result_type = semantic_arithmetic(true_type, false_type);
3804 true_expression = create_implicit_cast(true_expression, result_type);
3805 false_expression = create_implicit_cast(false_expression, result_type);
3807 conditional->true_expression = true_expression;
3808 conditional->false_expression = false_expression;
3809 conditional->expression.datatype = result_type;
3810 } else if (same_compound_type(true_type, false_type)
3811 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3812 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3813 /* just take 1 of the 2 types */
3814 result_type = true_type;
3815 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3816 && pointers_compatible(true_type, false_type)) {
3818 result_type = true_type;
3821 type_error_incompatible("while parsing conditional",
3822 expression->base.source_position, true_type,
3826 conditional->expression.datatype = result_type;
3830 static expression_t *parse_extension(unsigned precedence)
3832 eat(T___extension__);
3834 /* TODO enable extensions */
3836 return parse_sub_expression(precedence);
3839 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3841 eat(T___builtin_classify_type);
3843 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3844 result->base.datatype = type_int;
3847 expression_t *expression = parse_sub_expression(precedence);
3849 result->classify_type.type_expression = expression;
3854 static void semantic_incdec(unary_expression_t *expression)
3856 type_t *orig_type = expression->value->base.datatype;
3857 if(orig_type == NULL)
3860 type_t *type = skip_typeref(orig_type);
3861 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3862 /* TODO: improve error message */
3863 parser_print_error_prefix();
3864 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3868 expression->expression.datatype = orig_type;
3871 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3873 type_t *orig_type = expression->value->base.datatype;
3874 if(orig_type == NULL)
3877 type_t *type = skip_typeref(orig_type);
3878 if(!is_type_arithmetic(type)) {
3879 /* TODO: improve error message */
3880 parser_print_error_prefix();
3881 fprintf(stderr, "operation needs an arithmetic type\n");
3885 expression->expression.datatype = orig_type;
3888 static void semantic_unexpr_scalar(unary_expression_t *expression)
3890 type_t *orig_type = expression->value->base.datatype;
3891 if(orig_type == NULL)
3894 type_t *type = skip_typeref(orig_type);
3895 if (!is_type_scalar(type)) {
3896 parse_error("operand of ! must be of scalar type\n");
3900 expression->expression.datatype = orig_type;
3903 static void semantic_unexpr_integer(unary_expression_t *expression)
3905 type_t *orig_type = expression->value->base.datatype;
3906 if(orig_type == NULL)
3909 type_t *type = skip_typeref(orig_type);
3910 if (!is_type_integer(type)) {
3911 parse_error("operand of ~ must be of integer type\n");
3915 expression->expression.datatype = orig_type;
3918 static void semantic_dereference(unary_expression_t *expression)
3920 type_t *orig_type = expression->value->base.datatype;
3921 if(orig_type == NULL)
3924 type_t *type = skip_typeref(orig_type);
3925 if(!is_type_pointer(type)) {
3926 parser_print_error_prefix();
3927 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3928 print_type_quoted(orig_type);
3929 fputs(" given.\n", stderr);
3933 pointer_type_t *pointer_type = &type->pointer;
3934 type_t *result_type = pointer_type->points_to;
3936 result_type = automatic_type_conversion(result_type);
3937 expression->expression.datatype = result_type;
3940 static void semantic_take_addr(unary_expression_t *expression)
3942 expression_t *value = expression->value;
3943 value->base.datatype = revert_automatic_type_conversion(value);
3945 type_t *orig_type = value->base.datatype;
3946 if(orig_type == NULL)
3949 if(value->type == EXPR_REFERENCE) {
3950 reference_expression_t *reference = (reference_expression_t*) value;
3951 declaration_t *declaration = reference->declaration;
3952 if(declaration != NULL) {
3953 declaration->address_taken = 1;
3957 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3960 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3961 static expression_t *parse_##unexpression_type(unsigned precedence) \
3965 expression_t *unary_expression \
3966 = allocate_expression_zero(unexpression_type); \
3967 unary_expression->unary.value = parse_sub_expression(precedence); \
3969 sfunc(&unary_expression->unary); \
3971 return unary_expression; \
3974 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
3975 semantic_unexpr_arithmetic)
3976 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
3977 semantic_unexpr_arithmetic)
3978 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
3979 semantic_unexpr_scalar)
3980 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
3981 semantic_dereference)
3982 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
3984 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
3985 semantic_unexpr_integer)
3986 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
3988 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
3991 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3993 static expression_t *parse_##unexpression_type(unsigned precedence, \
3994 expression_t *left) \
3996 (void) precedence; \
3999 expression_t *unary_expression \
4000 = allocate_expression_zero(unexpression_type); \
4001 unary_expression->unary.value = left; \
4003 sfunc(&unary_expression->unary); \
4005 return unary_expression; \
4008 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4009 EXPR_UNARY_POSTFIX_INCREMENT,
4011 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4012 EXPR_UNARY_POSTFIX_DECREMENT,
4015 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4017 /* TODO: handle complex + imaginary types */
4019 /* § 6.3.1.8 Usual arithmetic conversions */
4020 if(type_left == type_long_double || type_right == type_long_double) {
4021 return type_long_double;
4022 } else if(type_left == type_double || type_right == type_double) {
4024 } else if(type_left == type_float || type_right == type_float) {
4028 type_right = promote_integer(type_right);
4029 type_left = promote_integer(type_left);
4031 if(type_left == type_right)
4034 bool signed_left = is_type_signed(type_left);
4035 bool signed_right = is_type_signed(type_right);
4036 int rank_left = get_rank(type_left);
4037 int rank_right = get_rank(type_right);
4038 if(rank_left < rank_right) {
4039 if(signed_left == signed_right || !signed_right) {
4045 if(signed_left == signed_right || !signed_left) {
4053 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4055 expression_t *left = expression->left;
4056 expression_t *right = expression->right;
4057 type_t *orig_type_left = left->base.datatype;
4058 type_t *orig_type_right = right->base.datatype;
4060 if(orig_type_left == NULL || orig_type_right == NULL)
4063 type_t *type_left = skip_typeref(orig_type_left);
4064 type_t *type_right = skip_typeref(orig_type_right);
4066 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4067 /* TODO: improve error message */
4068 parser_print_error_prefix();
4069 fprintf(stderr, "operation needs arithmetic types\n");
4073 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4074 expression->left = create_implicit_cast(left, arithmetic_type);
4075 expression->right = create_implicit_cast(right, arithmetic_type);
4076 expression->expression.datatype = arithmetic_type;
4079 static void semantic_shift_op(binary_expression_t *expression)
4081 expression_t *left = expression->left;
4082 expression_t *right = expression->right;
4083 type_t *orig_type_left = left->base.datatype;
4084 type_t *orig_type_right = right->base.datatype;
4086 if(orig_type_left == NULL || orig_type_right == NULL)
4089 type_t *type_left = skip_typeref(orig_type_left);
4090 type_t *type_right = skip_typeref(orig_type_right);
4092 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4093 /* TODO: improve error message */
4094 parser_print_error_prefix();
4095 fprintf(stderr, "operation needs integer types\n");
4099 type_left = promote_integer(type_left);
4100 type_right = promote_integer(type_right);
4102 expression->left = create_implicit_cast(left, type_left);
4103 expression->right = create_implicit_cast(right, type_right);
4104 expression->expression.datatype = type_left;
4107 static void semantic_add(binary_expression_t *expression)
4109 expression_t *left = expression->left;
4110 expression_t *right = expression->right;
4111 type_t *orig_type_left = left->base.datatype;
4112 type_t *orig_type_right = right->base.datatype;
4114 if(orig_type_left == NULL || orig_type_right == NULL)
4117 type_t *type_left = skip_typeref(orig_type_left);
4118 type_t *type_right = skip_typeref(orig_type_right);
4121 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4122 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4123 expression->left = create_implicit_cast(left, arithmetic_type);
4124 expression->right = create_implicit_cast(right, arithmetic_type);
4125 expression->expression.datatype = arithmetic_type;
4127 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4128 expression->expression.datatype = type_left;
4129 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4130 expression->expression.datatype = type_right;
4132 parser_print_error_prefix();
4133 fprintf(stderr, "invalid operands to binary + (");
4134 print_type_quoted(orig_type_left);
4135 fprintf(stderr, ", ");
4136 print_type_quoted(orig_type_right);
4137 fprintf(stderr, ")\n");
4141 static void semantic_sub(binary_expression_t *expression)
4143 expression_t *left = expression->left;
4144 expression_t *right = expression->right;
4145 type_t *orig_type_left = left->base.datatype;
4146 type_t *orig_type_right = right->base.datatype;
4148 if(orig_type_left == NULL || orig_type_right == NULL)
4151 type_t *type_left = skip_typeref(orig_type_left);
4152 type_t *type_right = skip_typeref(orig_type_right);
4155 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4156 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4157 expression->left = create_implicit_cast(left, arithmetic_type);
4158 expression->right = create_implicit_cast(right, arithmetic_type);
4159 expression->expression.datatype = arithmetic_type;
4161 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4162 expression->expression.datatype = type_left;
4163 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4164 if(!pointers_compatible(type_left, type_right)) {
4165 parser_print_error_prefix();
4166 fprintf(stderr, "pointers to incompatible objects to binary - (");
4167 print_type_quoted(orig_type_left);
4168 fprintf(stderr, ", ");
4169 print_type_quoted(orig_type_right);
4170 fprintf(stderr, ")\n");
4172 expression->expression.datatype = type_ptrdiff_t;
4175 parser_print_error_prefix();
4176 fprintf(stderr, "invalid operands to binary - (");
4177 print_type_quoted(orig_type_left);
4178 fprintf(stderr, ", ");
4179 print_type_quoted(orig_type_right);
4180 fprintf(stderr, ")\n");
4184 static void semantic_comparison(binary_expression_t *expression)
4186 expression_t *left = expression->left;
4187 expression_t *right = expression->right;
4188 type_t *orig_type_left = left->base.datatype;
4189 type_t *orig_type_right = right->base.datatype;
4191 if(orig_type_left == NULL || orig_type_right == NULL)
4194 type_t *type_left = skip_typeref(orig_type_left);
4195 type_t *type_right = skip_typeref(orig_type_right);
4197 /* TODO non-arithmetic types */
4198 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4199 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4200 expression->left = create_implicit_cast(left, arithmetic_type);
4201 expression->right = create_implicit_cast(right, arithmetic_type);
4202 expression->expression.datatype = arithmetic_type;
4203 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4204 /* TODO check compatibility */
4205 } else if (is_type_pointer(type_left)) {
4206 expression->right = create_implicit_cast(right, type_left);
4207 } else if (is_type_pointer(type_right)) {
4208 expression->left = create_implicit_cast(left, type_right);
4210 type_error_incompatible("invalid operands in comparison",
4211 token.source_position, type_left, type_right);
4213 expression->expression.datatype = type_int;
4216 static void semantic_arithmetic_assign(binary_expression_t *expression)
4218 expression_t *left = expression->left;
4219 expression_t *right = expression->right;
4220 type_t *orig_type_left = left->base.datatype;
4221 type_t *orig_type_right = right->base.datatype;
4223 if(orig_type_left == NULL || orig_type_right == NULL)
4226 type_t *type_left = skip_typeref(orig_type_left);
4227 type_t *type_right = skip_typeref(orig_type_right);
4229 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4230 /* TODO: improve error message */
4231 parser_print_error_prefix();
4232 fprintf(stderr, "operation needs arithmetic types\n");
4236 /* combined instructions are tricky. We can't create an implicit cast on
4237 * the left side, because we need the uncasted form for the store.
4238 * The ast2firm pass has to know that left_type must be right_type
4239 * for the arithmeitc operation and create a cast by itself */
4240 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4241 expression->right = create_implicit_cast(right, arithmetic_type);
4242 expression->expression.datatype = type_left;
4245 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4247 expression_t *left = expression->left;
4248 expression_t *right = expression->right;
4249 type_t *orig_type_left = left->base.datatype;
4250 type_t *orig_type_right = right->base.datatype;
4252 if(orig_type_left == NULL || orig_type_right == NULL)
4255 type_t *type_left = skip_typeref(orig_type_left);
4256 type_t *type_right = skip_typeref(orig_type_right);
4258 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4259 /* combined instructions are tricky. We can't create an implicit cast on
4260 * the left side, because we need the uncasted form for the store.
4261 * The ast2firm pass has to know that left_type must be right_type
4262 * for the arithmeitc operation and create a cast by itself */
4263 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4264 expression->right = create_implicit_cast(right, arithmetic_type);
4265 expression->expression.datatype = type_left;
4266 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4267 expression->expression.datatype = type_left;
4269 parser_print_error_prefix();
4270 fputs("Incompatible types ", stderr);
4271 print_type_quoted(orig_type_left);
4272 fputs(" and ", stderr);
4273 print_type_quoted(orig_type_right);
4274 fputs(" in assignment\n", stderr);
4279 static void semantic_logical_op(binary_expression_t *expression)
4281 expression_t *left = expression->left;
4282 expression_t *right = expression->right;
4283 type_t *orig_type_left = left->base.datatype;
4284 type_t *orig_type_right = right->base.datatype;
4286 if(orig_type_left == NULL || orig_type_right == NULL)
4289 type_t *type_left = skip_typeref(orig_type_left);
4290 type_t *type_right = skip_typeref(orig_type_right);
4292 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4293 /* TODO: improve error message */
4294 parser_print_error_prefix();
4295 fprintf(stderr, "operation needs scalar types\n");
4299 expression->expression.datatype = type_int;
4302 static bool has_const_fields(type_t *type)
4309 static void semantic_binexpr_assign(binary_expression_t *expression)
4311 expression_t *left = expression->left;
4312 type_t *orig_type_left = left->base.datatype;
4314 if(orig_type_left == NULL)
4317 type_t *type_left = revert_automatic_type_conversion(left);
4318 type_left = skip_typeref(orig_type_left);
4320 /* must be a modifiable lvalue */
4321 if (is_type_array(type_left)) {
4322 parser_print_error_prefix();
4323 fprintf(stderr, "Cannot assign to arrays ('");
4324 print_expression(left);
4325 fprintf(stderr, "')\n");
4328 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4329 parser_print_error_prefix();
4330 fprintf(stderr, "assignment to readonly location '");
4331 print_expression(left);
4332 fprintf(stderr, "' (type ");
4333 print_type_quoted(orig_type_left);
4334 fprintf(stderr, ")\n");
4337 if(is_type_incomplete(type_left)) {
4338 parser_print_error_prefix();
4339 fprintf(stderr, "left-hand side of assignment '");
4340 print_expression(left);
4341 fprintf(stderr, "' has incomplete type ");
4342 print_type_quoted(orig_type_left);
4343 fprintf(stderr, "\n");
4346 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4347 parser_print_error_prefix();
4348 fprintf(stderr, "can't assign to '");
4349 print_expression(left);
4350 fprintf(stderr, "' because compound type ");
4351 print_type_quoted(orig_type_left);
4352 fprintf(stderr, " has readonly fields\n");
4356 semantic_assign(orig_type_left, &expression->right, "assignment");
4358 expression->expression.datatype = orig_type_left;
4361 static void semantic_comma(binary_expression_t *expression)
4363 expression->expression.datatype = expression->right->base.datatype;
4366 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4367 static expression_t *parse_##binexpression_type(unsigned precedence, \
4368 expression_t *left) \
4372 expression_t *right = parse_sub_expression(precedence + lr); \
4374 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4375 binexpr->binary.left = left; \
4376 binexpr->binary.right = right; \
4377 sfunc(&binexpr->binary); \
4382 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4383 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4384 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4385 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4386 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4387 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4388 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4389 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4390 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4392 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4393 semantic_comparison, 1)
4394 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4395 semantic_comparison, 1)
4396 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4397 semantic_comparison, 1)
4398 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4399 semantic_comparison, 1)
4401 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4402 semantic_binexpr_arithmetic, 1)
4403 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4404 semantic_binexpr_arithmetic, 1)
4405 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4406 semantic_binexpr_arithmetic, 1)
4407 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4408 semantic_logical_op, 1)
4409 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4410 semantic_logical_op, 1)
4411 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4412 semantic_shift_op, 1)
4413 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4414 semantic_shift_op, 1)
4415 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4416 semantic_arithmetic_addsubb_assign, 0)
4417 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4418 semantic_arithmetic_addsubb_assign, 0)
4419 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4420 semantic_arithmetic_assign, 0)
4421 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4422 semantic_arithmetic_assign, 0)
4423 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4424 semantic_arithmetic_assign, 0)
4425 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4426 semantic_arithmetic_assign, 0)
4427 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4428 semantic_arithmetic_assign, 0)
4429 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4430 semantic_arithmetic_assign, 0)
4431 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4432 semantic_arithmetic_assign, 0)
4433 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4434 semantic_arithmetic_assign, 0)
4436 static expression_t *parse_sub_expression(unsigned precedence)
4438 if(token.type < 0) {
4439 return expected_expression_error();
4442 expression_parser_function_t *parser
4443 = &expression_parsers[token.type];
4444 source_position_t source_position = token.source_position;
4447 if(parser->parser != NULL) {
4448 left = parser->parser(parser->precedence);
4450 left = parse_primary_expression();
4452 assert(left != NULL);
4453 left->base.source_position = source_position;
4456 if(token.type < 0) {
4457 return expected_expression_error();
4460 parser = &expression_parsers[token.type];
4461 if(parser->infix_parser == NULL)
4463 if(parser->infix_precedence < precedence)
4466 left = parser->infix_parser(parser->infix_precedence, left);
4468 assert(left != NULL);
4469 assert(left->type != EXPR_UNKNOWN);
4470 left->base.source_position = source_position;
4476 static expression_t *parse_expression(void)
4478 return parse_sub_expression(1);
4483 static void register_expression_parser(parse_expression_function parser,
4484 int token_type, unsigned precedence)
4486 expression_parser_function_t *entry = &expression_parsers[token_type];
4488 if(entry->parser != NULL) {
4489 fprintf(stderr, "for token ");
4490 print_token_type(stderr, (token_type_t) token_type);
4491 fprintf(stderr, "\n");
4492 panic("trying to register multiple expression parsers for a token");
4494 entry->parser = parser;
4495 entry->precedence = precedence;
4498 static void register_infix_parser(parse_expression_infix_function parser,
4499 int token_type, unsigned precedence)
4501 expression_parser_function_t *entry = &expression_parsers[token_type];
4503 if(entry->infix_parser != NULL) {
4504 fprintf(stderr, "for token ");
4505 print_token_type(stderr, (token_type_t) token_type);
4506 fprintf(stderr, "\n");
4507 panic("trying to register multiple infix expression parsers for a "
4510 entry->infix_parser = parser;
4511 entry->infix_precedence = precedence;
4514 static void init_expression_parsers(void)
4516 memset(&expression_parsers, 0, sizeof(expression_parsers));
4518 register_infix_parser(parse_array_expression, '[', 30);
4519 register_infix_parser(parse_call_expression, '(', 30);
4520 register_infix_parser(parse_select_expression, '.', 30);
4521 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4522 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4524 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4527 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4528 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4529 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4530 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4531 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4532 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4533 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4534 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4535 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4536 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4537 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4538 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4539 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4540 T_EXCLAMATIONMARKEQUAL, 13);
4541 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4542 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4543 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4544 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4545 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4546 register_infix_parser(parse_conditional_expression, '?', 7);
4547 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4548 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4549 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4550 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4551 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4552 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4553 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4554 T_LESSLESSEQUAL, 2);
4555 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4556 T_GREATERGREATEREQUAL, 2);
4557 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4559 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4561 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4564 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4566 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4567 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4568 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4569 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4570 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4571 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4572 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4574 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4576 register_expression_parser(parse_sizeof, T_sizeof, 25);
4577 register_expression_parser(parse_extension, T___extension__, 25);
4578 register_expression_parser(parse_builtin_classify_type,
4579 T___builtin_classify_type, 25);
4582 static asm_constraint_t *parse_asm_constraints(void)
4584 asm_constraint_t *result = NULL;
4585 asm_constraint_t *last = NULL;
4587 while(token.type == T_STRING_LITERAL || token.type == '[') {
4588 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4589 memset(constraint, 0, sizeof(constraint[0]));
4591 if(token.type == '[') {
4593 if(token.type != T_IDENTIFIER) {
4594 parse_error_expected("while parsing asm constraint",
4598 constraint->symbol = token.v.symbol;
4603 constraint->constraints = parse_string_literals();
4605 constraint->expression = parse_expression();
4609 last->next = constraint;
4611 result = constraint;
4615 if(token.type != ',')
4623 static asm_clobber_t *parse_asm_clobbers(void)
4625 asm_clobber_t *result = NULL;
4626 asm_clobber_t *last = NULL;
4628 while(token.type == T_STRING_LITERAL) {
4629 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4630 clobber->clobber = parse_string_literals();
4633 last->next = clobber;
4639 if(token.type != ',')
4647 static statement_t *parse_asm_statement(void)
4651 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4652 statement->base.source_position = token.source_position;
4654 asm_statement_t *asm_statement = &statement->asms;
4656 if(token.type == T_volatile) {
4658 asm_statement->is_volatile = true;
4662 asm_statement->asm_text = parse_string_literals();
4664 if(token.type != ':')
4668 asm_statement->inputs = parse_asm_constraints();
4669 if(token.type != ':')
4673 asm_statement->outputs = parse_asm_constraints();
4674 if(token.type != ':')
4678 asm_statement->clobbers = parse_asm_clobbers();
4686 static statement_t *parse_case_statement(void)
4690 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4692 statement->base.source_position = token.source_position;
4693 statement->case_label.expression = parse_expression();
4696 statement->case_label.label_statement = parse_statement();
4701 static statement_t *parse_default_statement(void)
4705 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4707 statement->base.source_position = token.source_position;
4710 statement->label.label_statement = parse_statement();
4715 static declaration_t *get_label(symbol_t *symbol)
4717 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4718 assert(current_function != NULL);
4719 /* if we found a label in the same function, then we already created the
4721 if(candidate != NULL
4722 && candidate->parent_context == ¤t_function->context) {
4726 /* otherwise we need to create a new one */
4727 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4728 declaration->namespc = NAMESPACE_LABEL;
4729 declaration->symbol = symbol;
4731 label_push(declaration);
4736 static statement_t *parse_label_statement(void)
4738 assert(token.type == T_IDENTIFIER);
4739 symbol_t *symbol = token.v.symbol;
4742 declaration_t *label = get_label(symbol);
4744 /* if source position is already set then the label is defined twice,
4745 * otherwise it was just mentioned in a goto so far */
4746 if(label->source_position.input_name != NULL) {
4747 parser_print_error_prefix();
4748 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4749 parser_print_error_prefix_pos(label->source_position);
4750 fprintf(stderr, "previous definition of '%s' was here\n",
4753 label->source_position = token.source_position;
4756 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4758 label_statement->statement.type = STATEMENT_LABEL;
4759 label_statement->statement.source_position = token.source_position;
4760 label_statement->label = label;
4764 if(token.type == '}') {
4765 parse_error("label at end of compound statement");
4766 return (statement_t*) label_statement;
4768 label_statement->label_statement = parse_statement();
4771 return (statement_t*) label_statement;
4774 static statement_t *parse_if(void)
4778 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4779 statement->statement.type = STATEMENT_IF;
4780 statement->statement.source_position = token.source_position;
4783 statement->condition = parse_expression();
4786 statement->true_statement = parse_statement();
4787 if(token.type == T_else) {
4789 statement->false_statement = parse_statement();
4792 return (statement_t*) statement;
4795 static statement_t *parse_switch(void)
4799 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4800 statement->statement.type = STATEMENT_SWITCH;
4801 statement->statement.source_position = token.source_position;
4804 statement->expression = parse_expression();
4806 statement->body = parse_statement();
4808 return (statement_t*) statement;
4811 static statement_t *parse_while(void)
4815 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4816 statement->statement.type = STATEMENT_WHILE;
4817 statement->statement.source_position = token.source_position;
4820 statement->condition = parse_expression();
4822 statement->body = parse_statement();
4824 return (statement_t*) statement;
4827 static statement_t *parse_do(void)
4831 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4832 statement->statement.type = STATEMENT_DO_WHILE;
4833 statement->statement.source_position = token.source_position;
4835 statement->body = parse_statement();
4838 statement->condition = parse_expression();
4842 return (statement_t*) statement;
4845 static statement_t *parse_for(void)
4849 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4850 statement->statement.type = STATEMENT_FOR;
4851 statement->statement.source_position = token.source_position;
4855 int top = environment_top();
4856 context_t *last_context = context;
4857 set_context(&statement->context);
4859 if(token.type != ';') {
4860 if(is_declaration_specifier(&token, false)) {
4861 parse_declaration(record_declaration);
4863 statement->initialisation = parse_expression();
4870 if(token.type != ';') {
4871 statement->condition = parse_expression();
4874 if(token.type != ')') {
4875 statement->step = parse_expression();
4878 statement->body = parse_statement();
4880 assert(context == &statement->context);
4881 set_context(last_context);
4882 environment_pop_to(top);
4884 return (statement_t*) statement;
4887 static statement_t *parse_goto(void)
4891 if(token.type != T_IDENTIFIER) {
4892 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4896 symbol_t *symbol = token.v.symbol;
4899 declaration_t *label = get_label(symbol);
4901 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4903 statement->statement.type = STATEMENT_GOTO;
4904 statement->statement.source_position = token.source_position;
4906 statement->label = label;
4910 return (statement_t*) statement;
4913 static statement_t *parse_continue(void)
4918 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4919 statement->type = STATEMENT_CONTINUE;
4920 statement->base.source_position = token.source_position;
4925 static statement_t *parse_break(void)
4930 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4931 statement->type = STATEMENT_BREAK;
4932 statement->base.source_position = token.source_position;
4937 static statement_t *parse_return(void)
4941 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4943 statement->statement.type = STATEMENT_RETURN;
4944 statement->statement.source_position = token.source_position;
4946 assert(is_type_function(current_function->type));
4947 function_type_t *function_type = ¤t_function->type->function;
4948 type_t *return_type = function_type->return_type;
4950 expression_t *return_value = NULL;
4951 if(token.type != ';') {
4952 return_value = parse_expression();
4956 if(return_type == NULL)
4957 return (statement_t*) statement;
4958 if(return_value != NULL && return_value->base.datatype == NULL)
4959 return (statement_t*) statement;
4961 return_type = skip_typeref(return_type);
4963 if(return_value != NULL) {
4964 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4966 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4967 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4968 parse_warning("'return' with a value, in function returning void");
4969 return_value = NULL;
4971 if(return_type != NULL) {
4972 semantic_assign(return_type, &return_value, "'return'");
4976 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4977 parse_warning("'return' without value, in function returning "
4981 statement->return_value = return_value;
4983 return (statement_t*) statement;
4986 static statement_t *parse_declaration_statement(void)
4988 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4990 statement->base.source_position = token.source_position;
4992 declaration_t *before = last_declaration;
4993 parse_declaration(record_declaration);
4995 if(before == NULL) {
4996 statement->declaration.declarations_begin = context->declarations;
4998 statement->declaration.declarations_begin = before->next;
5000 statement->declaration.declarations_end = last_declaration;
5005 static statement_t *parse_expression_statement(void)
5007 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5009 statement->base.source_position = token.source_position;
5010 statement->expression.expression = parse_expression();
5017 static statement_t *parse_statement(void)
5019 statement_t *statement = NULL;
5021 /* declaration or statement */
5022 switch(token.type) {
5024 statement = parse_asm_statement();
5028 statement = parse_case_statement();
5032 statement = parse_default_statement();
5036 statement = parse_compound_statement();
5040 statement = parse_if();
5044 statement = parse_switch();
5048 statement = parse_while();
5052 statement = parse_do();
5056 statement = parse_for();
5060 statement = parse_goto();
5064 statement = parse_continue();
5068 statement = parse_break();
5072 statement = parse_return();
5081 if(look_ahead(1)->type == ':') {
5082 statement = parse_label_statement();
5086 if(is_typedef_symbol(token.v.symbol)) {
5087 statement = parse_declaration_statement();
5091 statement = parse_expression_statement();
5094 case T___extension__:
5095 /* this can be a prefix to a declaration or an expression statement */
5096 /* we simply eat it now and parse the rest with tail recursion */
5099 } while(token.type == T___extension__);
5100 statement = parse_statement();
5104 statement = parse_declaration_statement();
5108 statement = parse_expression_statement();
5112 assert(statement == NULL
5113 || statement->base.source_position.input_name != NULL);
5118 static statement_t *parse_compound_statement(void)
5120 compound_statement_t *compound_statement
5121 = allocate_ast_zero(sizeof(compound_statement[0]));
5122 compound_statement->statement.type = STATEMENT_COMPOUND;
5123 compound_statement->statement.source_position = token.source_position;
5127 int top = environment_top();
5128 context_t *last_context = context;
5129 set_context(&compound_statement->context);
5131 statement_t *last_statement = NULL;
5133 while(token.type != '}' && token.type != T_EOF) {
5134 statement_t *statement = parse_statement();
5135 if(statement == NULL)
5138 if(last_statement != NULL) {
5139 last_statement->base.next = statement;
5141 compound_statement->statements = statement;
5144 while(statement->base.next != NULL)
5145 statement = statement->base.next;
5147 last_statement = statement;
5150 if(token.type != '}') {
5151 parser_print_error_prefix_pos(
5152 compound_statement->statement.source_position);
5153 fprintf(stderr, "end of file while looking for closing '}'\n");
5157 assert(context == &compound_statement->context);
5158 set_context(last_context);
5159 environment_pop_to(top);
5161 return (statement_t*) compound_statement;
5164 static void initialize_builtins(void)
5166 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5167 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5168 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5169 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5170 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5171 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5172 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5173 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5175 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5176 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5177 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5178 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5181 static translation_unit_t *parse_translation_unit(void)
5183 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5185 assert(global_context == NULL);
5186 global_context = &unit->context;
5188 assert(context == NULL);
5189 set_context(&unit->context);
5191 initialize_builtins();
5193 while(token.type != T_EOF) {
5194 parse_external_declaration();
5197 assert(context == &unit->context);
5199 last_declaration = NULL;
5201 assert(global_context == &unit->context);
5202 global_context = NULL;
5207 translation_unit_t *parse(void)
5209 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5210 label_stack = NEW_ARR_F(stack_entry_t, 0);
5211 found_error = false;
5213 type_set_output(stderr);
5214 ast_set_output(stderr);
5216 lookahead_bufpos = 0;
5217 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5220 translation_unit_t *unit = parse_translation_unit();
5222 DEL_ARR_F(environment_stack);
5223 DEL_ARR_F(label_stack);
5231 void init_parser(void)
5233 init_expression_parsers();
5234 obstack_init(&temp_obst);
5236 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5237 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5240 void exit_parser(void)
5242 obstack_free(&temp_obst, NULL);