7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "adt/bitfiddle.h"
17 #include "adt/error.h"
18 #include "adt/array.h"
20 //#define PRINT_TOKENS
21 //#define ABORT_ON_ERROR
22 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static context_t *global_context = NULL;
48 static context_t *context = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static struct obstack temp_obst;
53 #define HERE token.source_position
55 static type_t *type_valist;
57 static statement_t *parse_compound_statement(void);
58 static statement_t *parse_statement(void);
60 static expression_t *parse_sub_expression(unsigned precedence);
61 static expression_t *parse_expression(void);
62 static type_t *parse_typename(void);
64 static void parse_compound_type_entries(void);
65 static declaration_t *parse_declarator(
66 const declaration_specifiers_t *specifiers, bool may_be_abstract);
67 static declaration_t *record_declaration(declaration_t *declaration);
69 static void semantic_comparison(binary_expression_t *expression);
71 #define STORAGE_CLASSES \
78 #define TYPE_QUALIFIERS \
85 #ifdef PROVIDE_COMPLEX
86 #define COMPLEX_SPECIFIERS \
88 #define IMAGINARY_SPECIFIERS \
91 #define COMPLEX_SPECIFIERS
92 #define IMAGINARY_SPECIFIERS
95 #define TYPE_SPECIFIERS \
110 case T___builtin_va_list: \
114 #define DECLARATION_START \
119 #define TYPENAME_START \
123 static void *allocate_ast_zero(size_t size)
125 void *res = allocate_ast(size);
126 memset(res, 0, size);
130 static size_t get_statement_struct_size(statement_kind_t kind)
132 static const size_t sizes[] = {
133 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
134 [STATEMENT_RETURN] = sizeof(return_statement_t),
135 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
136 [STATEMENT_IF] = sizeof(if_statement_t),
137 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
138 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
139 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
140 [STATEMENT_BREAK] = sizeof(statement_base_t),
141 [STATEMENT_GOTO] = sizeof(goto_statement_t),
142 [STATEMENT_LABEL] = sizeof(label_statement_t),
143 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
144 [STATEMENT_WHILE] = sizeof(while_statement_t),
145 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
146 [STATEMENT_FOR] = sizeof(for_statement_t),
147 [STATEMENT_ASM] = sizeof(asm_statement_t)
149 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
150 assert(sizes[kind] != 0);
154 static statement_t *allocate_statement_zero(statement_kind_t kind)
156 size_t size = get_statement_struct_size(kind);
157 statement_t *res = allocate_ast_zero(size);
159 res->base.kind = kind;
164 static size_t get_expression_struct_size(expression_kind_t type)
166 static const size_t sizes[] = {
167 [EXPR_INVALID] = sizeof(expression_base_t),
168 [EXPR_REFERENCE] = sizeof(reference_expression_t),
169 [EXPR_CONST] = sizeof(const_expression_t),
170 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
171 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
172 [EXPR_CALL] = sizeof(call_expression_t),
173 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
174 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
175 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
176 [EXPR_SELECT] = sizeof(select_expression_t),
177 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
178 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
179 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
180 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
181 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
182 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
183 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
184 [EXPR_VA_START] = sizeof(va_start_expression_t),
185 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
186 [EXPR_STATEMENT] = sizeof(statement_expression_t),
188 if(type >= EXPR_UNARY_FIRST && type <= EXPR_UNARY_LAST) {
189 return sizes[EXPR_UNARY_FIRST];
191 if(type >= EXPR_BINARY_FIRST && type <= EXPR_BINARY_LAST) {
192 return sizes[EXPR_BINARY_FIRST];
194 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
195 assert(sizes[type] != 0);
199 static expression_t *allocate_expression_zero(expression_kind_t kind)
201 size_t size = get_expression_struct_size(kind);
202 expression_t *res = allocate_ast_zero(size);
204 res->base.kind = kind;
208 static size_t get_type_struct_size(type_kind_t kind)
210 static const size_t sizes[] = {
211 [TYPE_ATOMIC] = sizeof(atomic_type_t),
212 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
213 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
214 [TYPE_ENUM] = sizeof(enum_type_t),
215 [TYPE_FUNCTION] = sizeof(function_type_t),
216 [TYPE_POINTER] = sizeof(pointer_type_t),
217 [TYPE_ARRAY] = sizeof(array_type_t),
218 [TYPE_BUILTIN] = sizeof(builtin_type_t),
219 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
220 [TYPE_TYPEOF] = sizeof(typeof_type_t),
222 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
223 assert(kind <= TYPE_TYPEOF);
224 assert(sizes[kind] != 0);
228 static type_t *allocate_type_zero(type_kind_t kind)
230 size_t size = get_type_struct_size(kind);
231 type_t *res = obstack_alloc(type_obst, size);
232 memset(res, 0, size);
234 res->base.kind = kind;
238 static size_t get_initializer_size(initializer_kind_t kind)
240 static const size_t sizes[] = {
241 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
242 [INITIALIZER_STRING] = sizeof(initializer_string_t),
243 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
244 [INITIALIZER_LIST] = sizeof(initializer_list_t)
246 assert(kind < sizeof(sizes) / sizeof(*sizes));
247 assert(sizes[kind] != 0);
251 static initializer_t *allocate_initializer(initializer_kind_t kind)
253 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
259 static void free_type(void *type)
261 obstack_free(type_obst, type);
265 * returns the top element of the environment stack
267 static size_t environment_top(void)
269 return ARR_LEN(environment_stack);
272 static size_t label_top(void)
274 return ARR_LEN(label_stack);
279 static inline void next_token(void)
281 token = lookahead_buffer[lookahead_bufpos];
282 lookahead_buffer[lookahead_bufpos] = lexer_token;
285 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
288 print_token(stderr, &token);
289 fprintf(stderr, "\n");
293 static inline const token_t *look_ahead(int num)
295 assert(num > 0 && num <= MAX_LOOKAHEAD);
296 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
297 return &lookahead_buffer[pos];
300 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
302 static void parse_error_expected(const char *message, ...)
304 if(message != NULL) {
305 errorf(HERE, "%s", message);
308 va_start(ap, message);
309 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
313 static void type_error(const char *msg, const source_position_t source_position,
316 errorf(source_position, "%s, but found type '%T'", msg, type);
319 static void type_error_incompatible(const char *msg,
320 const source_position_t source_position, type_t *type1, type_t *type2)
322 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
325 static void eat_block(void)
327 if(token.type == '{')
330 while(token.type != '}') {
331 if(token.type == T_EOF)
333 if(token.type == '{') {
342 static void eat_statement(void)
344 while(token.type != ';') {
345 if(token.type == T_EOF)
347 if(token.type == '}')
349 if(token.type == '{') {
358 static void eat_paren(void)
360 if(token.type == '(')
363 while(token.type != ')') {
364 if(token.type == T_EOF)
366 if(token.type == ')' || token.type == ';' || token.type == '}') {
369 if(token.type == '(') {
373 if(token.type == '{') {
382 #define expect(expected) \
383 if(UNLIKELY(token.type != (expected))) { \
384 parse_error_expected(NULL, (expected), 0); \
390 #define expect_block(expected) \
391 if(UNLIKELY(token.type != (expected))) { \
392 parse_error_expected(NULL, (expected), 0); \
398 #define expect_void(expected) \
399 if(UNLIKELY(token.type != (expected))) { \
400 parse_error_expected(NULL, (expected), 0); \
406 static void set_context(context_t *new_context)
408 context = new_context;
410 last_declaration = new_context->declarations;
411 if(last_declaration != NULL) {
412 while(last_declaration->next != NULL) {
413 last_declaration = last_declaration->next;
419 * called when we find a 2nd declarator for an identifier we already have a
422 static bool is_compatible_declaration(declaration_t *declaration,
423 declaration_t *previous)
425 /* happens for K&R style function parameters */
426 if(previous->type == NULL) {
427 previous->type = declaration->type;
431 type_t *type1 = skip_typeref(declaration->type);
432 type_t *type2 = skip_typeref(previous->type);
434 return types_compatible(type1, type2);
437 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
439 declaration_t *declaration = symbol->declaration;
440 for( ; declaration != NULL; declaration = declaration->symbol_next) {
441 if(declaration->namespc == namespc)
448 static const char *get_namespace_prefix(namespace_t namespc)
451 case NAMESPACE_NORMAL:
453 case NAMESPACE_UNION:
455 case NAMESPACE_STRUCT:
459 case NAMESPACE_LABEL:
462 panic("invalid namespace found");
466 * pushs an environment_entry on the environment stack and links the
467 * corresponding symbol to the new entry
469 static declaration_t *stack_push(stack_entry_t **stack_ptr,
470 declaration_t *declaration,
471 context_t *parent_context)
473 symbol_t *symbol = declaration->symbol;
474 namespace_t namespc = (namespace_t)declaration->namespc;
476 /* a declaration should be only pushed once */
477 declaration->parent_context = parent_context;
479 declaration_t *previous_declaration = get_declaration(symbol, namespc);
480 assert(declaration != previous_declaration);
481 if(previous_declaration != NULL
482 && previous_declaration->parent_context == context) {
483 if(!is_compatible_declaration(declaration, previous_declaration)) {
484 errorf(declaration->source_position, "definition of symbol '%s%s' with type '%T'", get_namespace_prefix(namespc), symbol->string, declaration->type);
485 errorf(previous_declaration->source_position, "is incompatible with previous declaration of type '%T'", previous_declaration->type);
487 unsigned old_storage_class = previous_declaration->storage_class;
488 unsigned new_storage_class = declaration->storage_class;
489 type_t *type = previous_declaration->type;
490 type = skip_typeref(type);
492 if (current_function == NULL) {
493 if (old_storage_class != STORAGE_CLASS_STATIC &&
494 new_storage_class == STORAGE_CLASS_STATIC) {
495 errorf(declaration->source_position, "static declaration of '%s' follows non-static declaration", symbol->string);
496 errorf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
498 if (old_storage_class == STORAGE_CLASS_EXTERN) {
499 if (new_storage_class == STORAGE_CLASS_NONE) {
500 previous_declaration->storage_class = STORAGE_CLASS_NONE;
502 } else if(!is_type_function(type)) {
503 warningf(declaration->source_position, "redundant declaration for '%s'\n", symbol->string);
504 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
508 if (old_storage_class == STORAGE_CLASS_EXTERN &&
509 new_storage_class == STORAGE_CLASS_EXTERN) {
510 warningf(declaration->source_position, "redundant extern declaration for '%s'\n", symbol->string);
511 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
513 if (old_storage_class == new_storage_class) {
514 errorf(declaration->source_position, "redeclaration of '%s'\n", symbol->string);
516 errorf(declaration->source_position, "redeclaration of '%s' with different linkage\n", symbol->string);
518 errorf(previous_declaration->source_position, "previous declaration of '%s' was here", symbol->string);
522 return previous_declaration;
525 /* remember old declaration */
527 entry.symbol = symbol;
528 entry.old_declaration = symbol->declaration;
529 entry.namespc = (unsigned short) namespc;
530 ARR_APP1(stack_entry_t, *stack_ptr, entry);
532 /* replace/add declaration into declaration list of the symbol */
533 if(symbol->declaration == NULL) {
534 symbol->declaration = declaration;
536 declaration_t *iter_last = NULL;
537 declaration_t *iter = symbol->declaration;
538 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
539 /* replace an entry? */
540 if(iter->namespc == namespc) {
541 if(iter_last == NULL) {
542 symbol->declaration = declaration;
544 iter_last->symbol_next = declaration;
546 declaration->symbol_next = iter->symbol_next;
551 assert(iter_last->symbol_next == NULL);
552 iter_last->symbol_next = declaration;
559 static declaration_t *environment_push(declaration_t *declaration)
561 assert(declaration->source_position.input_name != NULL);
562 return stack_push(&environment_stack, declaration, context);
565 static declaration_t *label_push(declaration_t *declaration)
567 return stack_push(&label_stack, declaration, ¤t_function->context);
571 * pops symbols from the environment stack until @p new_top is the top element
573 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
575 stack_entry_t *stack = *stack_ptr;
576 size_t top = ARR_LEN(stack);
579 assert(new_top <= top);
583 for(i = top; i > new_top; --i) {
584 stack_entry_t *entry = &stack[i - 1];
586 declaration_t *old_declaration = entry->old_declaration;
587 symbol_t *symbol = entry->symbol;
588 namespace_t namespc = (namespace_t)entry->namespc;
590 /* replace/remove declaration */
591 declaration_t *declaration = symbol->declaration;
592 assert(declaration != NULL);
593 if(declaration->namespc == namespc) {
594 if(old_declaration == NULL) {
595 symbol->declaration = declaration->symbol_next;
597 symbol->declaration = old_declaration;
600 declaration_t *iter_last = declaration;
601 declaration_t *iter = declaration->symbol_next;
602 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
603 /* replace an entry? */
604 if(iter->namespc == namespc) {
605 assert(iter_last != NULL);
606 iter_last->symbol_next = old_declaration;
607 old_declaration->symbol_next = iter->symbol_next;
611 assert(iter != NULL);
615 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
618 static void environment_pop_to(size_t new_top)
620 stack_pop_to(&environment_stack, new_top);
623 static void label_pop_to(size_t new_top)
625 stack_pop_to(&label_stack, new_top);
629 static int get_rank(const type_t *type)
631 assert(!is_typeref(type));
632 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
633 * and esp. footnote 108). However we can't fold constants (yet), so we
634 * can't decide whether unsigned int is possible, while int always works.
635 * (unsigned int would be preferable when possible... for stuff like
636 * struct { enum { ... } bla : 4; } ) */
637 if(type->kind == TYPE_ENUM)
638 return ATOMIC_TYPE_INT;
640 assert(type->kind == TYPE_ATOMIC);
641 const atomic_type_t *atomic_type = &type->atomic;
642 atomic_type_type_t atype = atomic_type->atype;
646 static type_t *promote_integer(type_t *type)
648 if(get_rank(type) < ATOMIC_TYPE_INT)
654 static expression_t *create_cast_expression(expression_t *expression,
657 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
659 cast->unary.value = expression;
660 cast->base.datatype = dest_type;
665 static bool is_null_pointer_constant(const expression_t *expression)
667 /* skip void* cast */
668 if(expression->kind == EXPR_UNARY_CAST
669 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
670 expression = expression->unary.value;
673 /* TODO: not correct yet, should be any constant integer expression
674 * which evaluates to 0 */
675 if (expression->kind != EXPR_CONST)
678 type_t *const type = skip_typeref(expression->base.datatype);
679 if (!is_type_integer(type))
682 return expression->conste.v.int_value == 0;
685 static expression_t *create_implicit_cast(expression_t *expression,
688 type_t *source_type = expression->base.datatype;
690 if(source_type == NULL)
693 source_type = skip_typeref(source_type);
694 dest_type = skip_typeref(dest_type);
696 if(source_type == dest_type)
699 switch (dest_type->kind) {
701 /* TODO warning for implicitly converting to enum */
703 if (source_type->kind != TYPE_ATOMIC &&
704 source_type->kind != TYPE_ENUM) {
705 panic("casting of non-atomic types not implemented yet");
708 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
709 type_error_incompatible("can't cast types",
710 expression->base.source_position, source_type,
715 return create_cast_expression(expression, dest_type);
718 switch (source_type->kind) {
720 if (is_null_pointer_constant(expression)) {
721 return create_cast_expression(expression, dest_type);
726 if (pointers_compatible(source_type, dest_type)) {
727 return create_cast_expression(expression, dest_type);
732 array_type_t *array_type = &source_type->array;
733 pointer_type_t *pointer_type = &dest_type->pointer;
734 if (types_compatible(array_type->element_type,
735 pointer_type->points_to)) {
736 return create_cast_expression(expression, dest_type);
742 panic("casting of non-atomic types not implemented yet");
745 type_error_incompatible("can't implicitly cast types",
746 expression->base.source_position, source_type, dest_type);
750 panic("casting of non-atomic types not implemented yet");
754 /** Implements the rules from § 6.5.16.1 */
755 static void semantic_assign(type_t *orig_type_left, expression_t **right,
758 type_t *orig_type_right = (*right)->base.datatype;
760 if(orig_type_right == NULL)
763 type_t *const type_left = skip_typeref(orig_type_left);
764 type_t *const type_right = skip_typeref(orig_type_right);
766 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
767 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
768 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
769 && is_type_pointer(type_right))) {
770 *right = create_implicit_cast(*right, type_left);
774 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
775 pointer_type_t *pointer_type_left = &type_left->pointer;
776 pointer_type_t *pointer_type_right = &type_right->pointer;
777 type_t *points_to_left = pointer_type_left->points_to;
778 type_t *points_to_right = pointer_type_right->points_to;
780 points_to_left = skip_typeref(points_to_left);
781 points_to_right = skip_typeref(points_to_right);
783 /* the left type has all qualifiers from the right type */
784 unsigned missing_qualifiers
785 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
786 if(missing_qualifiers != 0) {
787 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
791 points_to_left = get_unqualified_type(points_to_left);
792 points_to_right = get_unqualified_type(points_to_right);
794 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
795 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
796 && !types_compatible(points_to_left, points_to_right)) {
797 goto incompatible_assign_types;
800 *right = create_implicit_cast(*right, type_left);
804 if (is_type_compound(type_left)
805 && types_compatible(type_left, type_right)) {
806 *right = create_implicit_cast(*right, type_left);
810 incompatible_assign_types:
811 /* TODO: improve error message */
812 errorf(HERE, "incompatible types in %s", context);
813 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
816 static expression_t *parse_constant_expression(void)
818 /* start parsing at precedence 7 (conditional expression) */
819 expression_t *result = parse_sub_expression(7);
821 if(!is_constant_expression(result)) {
822 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
828 static expression_t *parse_assignment_expression(void)
830 /* start parsing at precedence 2 (assignment expression) */
831 return parse_sub_expression(2);
834 static type_t *make_global_typedef(const char *name, type_t *type)
836 symbol_t *const symbol = symbol_table_insert(name);
838 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
839 declaration->namespc = NAMESPACE_NORMAL;
840 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
841 declaration->type = type;
842 declaration->symbol = symbol;
843 declaration->source_position = builtin_source_position;
845 record_declaration(declaration);
847 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
848 typedef_type->typedeft.declaration = declaration;
853 static const char *parse_string_literals(void)
855 assert(token.type == T_STRING_LITERAL);
856 const char *result = token.v.string;
860 while(token.type == T_STRING_LITERAL) {
861 result = concat_strings(result, token.v.string);
868 static void parse_attributes(void)
872 case T___attribute__: {
880 errorf(HERE, "EOF while parsing attribute");
899 if(token.type != T_STRING_LITERAL) {
900 parse_error_expected("while parsing assembler attribute",
905 parse_string_literals();
910 goto attributes_finished;
919 static designator_t *parse_designation(void)
921 if(token.type != '[' && token.type != '.')
924 designator_t *result = NULL;
925 designator_t *last = NULL;
928 designator_t *designator;
931 designator = allocate_ast_zero(sizeof(designator[0]));
933 designator->array_access = parse_constant_expression();
937 designator = allocate_ast_zero(sizeof(designator[0]));
939 if(token.type != T_IDENTIFIER) {
940 parse_error_expected("while parsing designator",
944 designator->symbol = token.v.symbol;
952 assert(designator != NULL);
954 last->next = designator;
963 static initializer_t *initializer_from_string(array_type_t *type,
966 /* TODO: check len vs. size of array type */
969 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
970 initializer->string.string = string;
975 static initializer_t *initializer_from_wide_string(array_type_t *const type,
976 wide_string_t *const string)
978 /* TODO: check len vs. size of array type */
981 initializer_t *const initializer =
982 allocate_initializer(INITIALIZER_WIDE_STRING);
983 initializer->wide_string.string = *string;
988 static initializer_t *initializer_from_expression(type_t *type,
989 expression_t *expression)
991 /* TODO check that expression is a constant expression */
993 /* § 6.7.8.14/15 char array may be initialized by string literals */
994 type_t *const expr_type = expression->base.datatype;
995 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
996 array_type_t *const array_type = &type->array;
997 type_t *const element_type = skip_typeref(array_type->element_type);
999 if (element_type->kind == TYPE_ATOMIC) {
1000 switch (expression->kind) {
1001 case EXPR_STRING_LITERAL:
1002 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1003 return initializer_from_string(array_type,
1004 expression->string.value);
1007 case EXPR_WIDE_STRING_LITERAL: {
1008 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1009 if (get_unqualified_type(element_type) == bare_wchar_type) {
1010 return initializer_from_wide_string(array_type,
1011 &expression->wide_string.value);
1020 type_t *expression_type = skip_typeref(expression->base.datatype);
1021 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1022 semantic_assign(type, &expression, "initializer");
1024 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1025 result->value.value = expression;
1033 static initializer_t *parse_sub_initializer(type_t *type,
1034 expression_t *expression,
1035 type_t *expression_type);
1037 static initializer_t *parse_sub_initializer_elem(type_t *type)
1039 if(token.type == '{') {
1040 return parse_sub_initializer(type, NULL, NULL);
1043 expression_t *expression = parse_assignment_expression();
1044 type_t *expression_type = skip_typeref(expression->base.datatype);
1046 return parse_sub_initializer(type, expression, expression_type);
1049 static bool had_initializer_brace_warning;
1051 static initializer_t *parse_sub_initializer(type_t *type,
1052 expression_t *expression,
1053 type_t *expression_type)
1055 if(is_type_scalar(type)) {
1056 /* there might be extra {} hierarchies */
1057 if(token.type == '{') {
1059 if(!had_initializer_brace_warning) {
1060 warningf(HERE, "braces around scalar initializer");
1061 had_initializer_brace_warning = true;
1063 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1064 if(token.type == ',') {
1066 /* TODO: warn about excessive elements */
1072 if(expression == NULL) {
1073 expression = parse_assignment_expression();
1075 return initializer_from_expression(type, expression);
1078 /* does the expression match the currently looked at object to initialize */
1079 if(expression != NULL) {
1080 initializer_t *result = initializer_from_expression(type, expression);
1085 bool read_paren = false;
1086 if(token.type == '{') {
1091 /* descend into subtype */
1092 initializer_t *result = NULL;
1093 initializer_t **elems;
1094 if(is_type_array(type)) {
1095 array_type_t *array_type = &type->array;
1096 type_t *element_type = array_type->element_type;
1097 element_type = skip_typeref(element_type);
1100 had_initializer_brace_warning = false;
1101 if(expression == NULL) {
1102 sub = parse_sub_initializer_elem(element_type);
1104 sub = parse_sub_initializer(element_type, expression,
1108 /* didn't match the subtypes -> try the parent type */
1110 assert(!read_paren);
1114 elems = NEW_ARR_F(initializer_t*, 0);
1115 ARR_APP1(initializer_t*, elems, sub);
1118 if(token.type == '}')
1121 if(token.type == '}')
1124 sub = parse_sub_initializer_elem(element_type);
1126 /* TODO error, do nicer cleanup */
1127 errorf(HERE, "member initializer didn't match");
1131 ARR_APP1(initializer_t*, elems, sub);
1134 assert(is_type_compound(type));
1135 compound_type_t *compound_type = &type->compound;
1136 context_t *context = &compound_type->declaration->context;
1138 declaration_t *first = context->declarations;
1141 type_t *first_type = first->type;
1142 first_type = skip_typeref(first_type);
1145 had_initializer_brace_warning = false;
1146 if(expression == NULL) {
1147 sub = parse_sub_initializer_elem(first_type);
1149 sub = parse_sub_initializer(first_type, expression,expression_type);
1152 /* didn't match the subtypes -> try our parent type */
1154 assert(!read_paren);
1158 elems = NEW_ARR_F(initializer_t*, 0);
1159 ARR_APP1(initializer_t*, elems, sub);
1161 declaration_t *iter = first->next;
1162 for( ; iter != NULL; iter = iter->next) {
1163 if(iter->symbol == NULL)
1165 if(iter->namespc != NAMESPACE_NORMAL)
1168 if(token.type == '}')
1171 if(token.type == '}')
1174 type_t *iter_type = iter->type;
1175 iter_type = skip_typeref(iter_type);
1177 sub = parse_sub_initializer_elem(iter_type);
1179 /* TODO error, do nicer cleanup */
1180 errorf(HERE, "member initializer didn't match");
1184 ARR_APP1(initializer_t*, elems, sub);
1188 int len = ARR_LEN(elems);
1189 size_t elems_size = sizeof(initializer_t*) * len;
1191 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1193 init->initializer.kind = INITIALIZER_LIST;
1195 memcpy(init->initializers, elems, elems_size);
1198 result = (initializer_t*) init;
1201 if(token.type == ',')
1208 static initializer_t *parse_initializer(type_t *type)
1210 initializer_t *result;
1212 type = skip_typeref(type);
1214 if(token.type != '{') {
1215 expression_t *expression = parse_assignment_expression();
1216 initializer_t *initializer = initializer_from_expression(type, expression);
1217 if(initializer == NULL) {
1218 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1223 if(is_type_scalar(type)) {
1227 expression_t *expression = parse_assignment_expression();
1228 result = initializer_from_expression(type, expression);
1230 if(token.type == ',')
1236 result = parse_sub_initializer(type, NULL, NULL);
1244 static declaration_t *parse_compound_type_specifier(bool is_struct)
1252 symbol_t *symbol = NULL;
1253 declaration_t *declaration = NULL;
1255 if (token.type == T___attribute__) {
1260 if(token.type == T_IDENTIFIER) {
1261 symbol = token.v.symbol;
1265 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1267 declaration = get_declaration(symbol, NAMESPACE_UNION);
1269 } else if(token.type != '{') {
1271 parse_error_expected("while parsing struct type specifier",
1272 T_IDENTIFIER, '{', 0);
1274 parse_error_expected("while parsing union type specifier",
1275 T_IDENTIFIER, '{', 0);
1281 if(declaration == NULL) {
1282 declaration = allocate_ast_zero(sizeof(declaration[0]));
1285 declaration->namespc = NAMESPACE_STRUCT;
1287 declaration->namespc = NAMESPACE_UNION;
1289 declaration->source_position = token.source_position;
1290 declaration->symbol = symbol;
1291 record_declaration(declaration);
1294 if(token.type == '{') {
1295 if(declaration->init.is_defined) {
1296 assert(symbol != NULL);
1297 errorf(HERE, "multiple definition of %s %s", is_struct ? "struct" : "union", symbol->string);
1298 declaration->context.declarations = NULL;
1300 declaration->init.is_defined = true;
1302 int top = environment_top();
1303 context_t *last_context = context;
1304 set_context(&declaration->context);
1306 parse_compound_type_entries();
1309 assert(context == &declaration->context);
1310 set_context(last_context);
1311 environment_pop_to(top);
1317 static void parse_enum_entries(enum_type_t *const enum_type)
1321 if(token.type == '}') {
1323 errorf(HERE, "empty enum not allowed");
1328 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1330 if(token.type != T_IDENTIFIER) {
1331 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1335 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1336 entry->type = (type_t*) enum_type;
1337 entry->symbol = token.v.symbol;
1338 entry->source_position = token.source_position;
1341 if(token.type == '=') {
1343 entry->init.enum_value = parse_constant_expression();
1348 record_declaration(entry);
1350 if(token.type != ',')
1353 } while(token.type != '}');
1358 static type_t *parse_enum_specifier(void)
1362 declaration_t *declaration;
1365 if(token.type == T_IDENTIFIER) {
1366 symbol = token.v.symbol;
1369 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1370 } else if(token.type != '{') {
1371 parse_error_expected("while parsing enum type specifier",
1372 T_IDENTIFIER, '{', 0);
1379 if(declaration == NULL) {
1380 declaration = allocate_ast_zero(sizeof(declaration[0]));
1382 declaration->namespc = NAMESPACE_ENUM;
1383 declaration->source_position = token.source_position;
1384 declaration->symbol = symbol;
1387 type_t *const type = allocate_type_zero(TYPE_ENUM);
1388 type->enumt.declaration = declaration;
1390 if(token.type == '{') {
1391 if(declaration->init.is_defined) {
1392 errorf(HERE, "multiple definitions of enum %s", symbol->string);
1394 record_declaration(declaration);
1395 declaration->init.is_defined = 1;
1397 parse_enum_entries(&type->enumt);
1405 * if a symbol is a typedef to another type, return true
1407 static bool is_typedef_symbol(symbol_t *symbol)
1409 const declaration_t *const declaration =
1410 get_declaration(symbol, NAMESPACE_NORMAL);
1412 declaration != NULL &&
1413 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1416 static type_t *parse_typeof(void)
1424 expression_t *expression = NULL;
1427 switch(token.type) {
1428 case T___extension__:
1429 /* this can be a prefix to a typename or an expression */
1430 /* we simply eat it now. */
1433 } while(token.type == T___extension__);
1437 if(is_typedef_symbol(token.v.symbol)) {
1438 type = parse_typename();
1440 expression = parse_expression();
1441 type = expression->base.datatype;
1446 type = parse_typename();
1450 expression = parse_expression();
1451 type = expression->base.datatype;
1457 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1458 typeof_type->typeoft.expression = expression;
1459 typeof_type->typeoft.typeof_type = type;
1465 SPECIFIER_SIGNED = 1 << 0,
1466 SPECIFIER_UNSIGNED = 1 << 1,
1467 SPECIFIER_LONG = 1 << 2,
1468 SPECIFIER_INT = 1 << 3,
1469 SPECIFIER_DOUBLE = 1 << 4,
1470 SPECIFIER_CHAR = 1 << 5,
1471 SPECIFIER_SHORT = 1 << 6,
1472 SPECIFIER_LONG_LONG = 1 << 7,
1473 SPECIFIER_FLOAT = 1 << 8,
1474 SPECIFIER_BOOL = 1 << 9,
1475 SPECIFIER_VOID = 1 << 10,
1476 #ifdef PROVIDE_COMPLEX
1477 SPECIFIER_COMPLEX = 1 << 11,
1478 SPECIFIER_IMAGINARY = 1 << 12,
1482 static type_t *create_builtin_type(symbol_t *const symbol,
1483 type_t *const real_type)
1485 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1486 type->builtin.symbol = symbol;
1487 type->builtin.real_type = real_type;
1489 type_t *result = typehash_insert(type);
1490 if (type != result) {
1497 static type_t *get_typedef_type(symbol_t *symbol)
1499 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1500 if(declaration == NULL
1501 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1504 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1505 type->typedeft.declaration = declaration;
1510 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1512 type_t *type = NULL;
1513 unsigned type_qualifiers = 0;
1514 unsigned type_specifiers = 0;
1517 specifiers->source_position = token.source_position;
1520 switch(token.type) {
1523 #define MATCH_STORAGE_CLASS(token, class) \
1525 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1526 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1528 specifiers->storage_class = class; \
1532 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1533 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1534 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1535 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1536 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1539 switch (specifiers->storage_class) {
1540 case STORAGE_CLASS_NONE:
1541 specifiers->storage_class = STORAGE_CLASS_THREAD;
1544 case STORAGE_CLASS_EXTERN:
1545 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1548 case STORAGE_CLASS_STATIC:
1549 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1553 errorf(HERE, "multiple storage classes in declaration specifiers");
1559 /* type qualifiers */
1560 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1562 type_qualifiers |= qualifier; \
1566 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1567 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1568 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1570 case T___extension__:
1575 /* type specifiers */
1576 #define MATCH_SPECIFIER(token, specifier, name) \
1579 if(type_specifiers & specifier) { \
1580 errorf(HERE, "multiple " name " type specifiers given"); \
1582 type_specifiers |= specifier; \
1586 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1587 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1588 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1589 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1590 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1591 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1592 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1593 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1594 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1595 #ifdef PROVIDE_COMPLEX
1596 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1597 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1600 /* only in microsoft mode */
1601 specifiers->decl_modifiers |= DM_FORCEINLINE;
1605 specifiers->is_inline = true;
1610 if(type_specifiers & SPECIFIER_LONG_LONG) {
1611 errorf(HERE, "multiple type specifiers given");
1612 } else if(type_specifiers & SPECIFIER_LONG) {
1613 type_specifiers |= SPECIFIER_LONG_LONG;
1615 type_specifiers |= SPECIFIER_LONG;
1619 /* TODO: if type != NULL for the following rules should issue
1622 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1624 type->compound.declaration = parse_compound_type_specifier(true);
1628 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1630 type->compound.declaration = parse_compound_type_specifier(false);
1634 type = parse_enum_specifier();
1637 type = parse_typeof();
1639 case T___builtin_va_list:
1640 type = duplicate_type(type_valist);
1644 case T___attribute__:
1649 case T_IDENTIFIER: {
1650 type_t *typedef_type = get_typedef_type(token.v.symbol);
1652 if(typedef_type == NULL)
1653 goto finish_specifiers;
1656 type = typedef_type;
1660 /* function specifier */
1662 goto finish_specifiers;
1669 atomic_type_type_t atomic_type;
1671 /* match valid basic types */
1672 switch(type_specifiers) {
1673 case SPECIFIER_VOID:
1674 atomic_type = ATOMIC_TYPE_VOID;
1676 case SPECIFIER_CHAR:
1677 atomic_type = ATOMIC_TYPE_CHAR;
1679 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1680 atomic_type = ATOMIC_TYPE_SCHAR;
1682 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1683 atomic_type = ATOMIC_TYPE_UCHAR;
1685 case SPECIFIER_SHORT:
1686 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1687 case SPECIFIER_SHORT | SPECIFIER_INT:
1688 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1689 atomic_type = ATOMIC_TYPE_SHORT;
1691 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1692 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1693 atomic_type = ATOMIC_TYPE_USHORT;
1696 case SPECIFIER_SIGNED:
1697 case SPECIFIER_SIGNED | SPECIFIER_INT:
1698 atomic_type = ATOMIC_TYPE_INT;
1700 case SPECIFIER_UNSIGNED:
1701 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1702 atomic_type = ATOMIC_TYPE_UINT;
1704 case SPECIFIER_LONG:
1705 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1706 case SPECIFIER_LONG | SPECIFIER_INT:
1707 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1708 atomic_type = ATOMIC_TYPE_LONG;
1710 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1711 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1712 atomic_type = ATOMIC_TYPE_ULONG;
1714 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1715 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1716 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1717 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1719 atomic_type = ATOMIC_TYPE_LONGLONG;
1721 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1722 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1724 atomic_type = ATOMIC_TYPE_ULONGLONG;
1726 case SPECIFIER_FLOAT:
1727 atomic_type = ATOMIC_TYPE_FLOAT;
1729 case SPECIFIER_DOUBLE:
1730 atomic_type = ATOMIC_TYPE_DOUBLE;
1732 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1733 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1735 case SPECIFIER_BOOL:
1736 atomic_type = ATOMIC_TYPE_BOOL;
1738 #ifdef PROVIDE_COMPLEX
1739 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1740 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1742 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1743 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1745 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1746 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1748 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1749 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1751 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1752 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1754 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1755 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1759 /* invalid specifier combination, give an error message */
1760 if(type_specifiers == 0) {
1762 warningf(HERE, "no type specifiers in declaration, using int");
1763 atomic_type = ATOMIC_TYPE_INT;
1766 errorf(HERE, "no type specifiers given in declaration");
1768 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1769 (type_specifiers & SPECIFIER_UNSIGNED)) {
1770 errorf(HERE, "signed and unsigned specifiers gives");
1771 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1772 errorf(HERE, "only integer types can be signed or unsigned");
1774 errorf(HERE, "multiple datatypes in declaration");
1776 atomic_type = ATOMIC_TYPE_INVALID;
1779 type = allocate_type_zero(TYPE_ATOMIC);
1780 type->atomic.atype = atomic_type;
1783 if(type_specifiers != 0) {
1784 errorf(HERE, "multiple datatypes in declaration");
1788 type->base.qualifiers = type_qualifiers;
1790 type_t *result = typehash_insert(type);
1791 if(newtype && result != type) {
1795 specifiers->type = result;
1798 static type_qualifiers_t parse_type_qualifiers(void)
1800 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1803 switch(token.type) {
1804 /* type qualifiers */
1805 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1806 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1807 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1810 return type_qualifiers;
1815 static declaration_t *parse_identifier_list(void)
1817 declaration_t *declarations = NULL;
1818 declaration_t *last_declaration = NULL;
1820 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1822 declaration->source_position = token.source_position;
1823 declaration->symbol = token.v.symbol;
1826 if(last_declaration != NULL) {
1827 last_declaration->next = declaration;
1829 declarations = declaration;
1831 last_declaration = declaration;
1833 if(token.type != ',')
1836 } while(token.type == T_IDENTIFIER);
1838 return declarations;
1841 static void semantic_parameter(declaration_t *declaration)
1843 /* TODO: improve error messages */
1845 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1846 errorf(HERE, "typedef not allowed in parameter list");
1847 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1848 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1849 errorf(HERE, "parameter may only have none or register storage class");
1852 type_t *orig_type = declaration->type;
1853 if(orig_type == NULL)
1855 type_t *type = skip_typeref(orig_type);
1857 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1858 * into a pointer. § 6.7.5.3 (7) */
1859 if (is_type_array(type)) {
1860 const array_type_t *arr_type = &type->array;
1861 type_t *element_type = arr_type->element_type;
1863 type = make_pointer_type(element_type, type->base.qualifiers);
1865 declaration->type = type;
1868 if(is_type_incomplete(type)) {
1869 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%s'", orig_type, declaration->symbol->string);
1873 static declaration_t *parse_parameter(void)
1875 declaration_specifiers_t specifiers;
1876 memset(&specifiers, 0, sizeof(specifiers));
1878 parse_declaration_specifiers(&specifiers);
1880 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1882 semantic_parameter(declaration);
1887 static declaration_t *parse_parameters(function_type_t *type)
1889 if(token.type == T_IDENTIFIER) {
1890 symbol_t *symbol = token.v.symbol;
1891 if(!is_typedef_symbol(symbol)) {
1892 type->kr_style_parameters = true;
1893 return parse_identifier_list();
1897 if(token.type == ')') {
1898 type->unspecified_parameters = 1;
1901 if(token.type == T_void && look_ahead(1)->type == ')') {
1906 declaration_t *declarations = NULL;
1907 declaration_t *declaration;
1908 declaration_t *last_declaration = NULL;
1909 function_parameter_t *parameter;
1910 function_parameter_t *last_parameter = NULL;
1913 switch(token.type) {
1917 return declarations;
1920 case T___extension__:
1922 declaration = parse_parameter();
1924 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1925 memset(parameter, 0, sizeof(parameter[0]));
1926 parameter->type = declaration->type;
1928 if(last_parameter != NULL) {
1929 last_declaration->next = declaration;
1930 last_parameter->next = parameter;
1932 type->parameters = parameter;
1933 declarations = declaration;
1935 last_parameter = parameter;
1936 last_declaration = declaration;
1940 return declarations;
1942 if(token.type != ',')
1943 return declarations;
1953 } construct_type_type_t;
1955 typedef struct construct_type_t construct_type_t;
1956 struct construct_type_t {
1957 construct_type_type_t type;
1958 construct_type_t *next;
1961 typedef struct parsed_pointer_t parsed_pointer_t;
1962 struct parsed_pointer_t {
1963 construct_type_t construct_type;
1964 type_qualifiers_t type_qualifiers;
1967 typedef struct construct_function_type_t construct_function_type_t;
1968 struct construct_function_type_t {
1969 construct_type_t construct_type;
1970 type_t *function_type;
1973 typedef struct parsed_array_t parsed_array_t;
1974 struct parsed_array_t {
1975 construct_type_t construct_type;
1976 type_qualifiers_t type_qualifiers;
1982 typedef struct construct_base_type_t construct_base_type_t;
1983 struct construct_base_type_t {
1984 construct_type_t construct_type;
1988 static construct_type_t *parse_pointer_declarator(void)
1992 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1993 memset(pointer, 0, sizeof(pointer[0]));
1994 pointer->construct_type.type = CONSTRUCT_POINTER;
1995 pointer->type_qualifiers = parse_type_qualifiers();
1997 return (construct_type_t*) pointer;
2000 static construct_type_t *parse_array_declarator(void)
2004 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2005 memset(array, 0, sizeof(array[0]));
2006 array->construct_type.type = CONSTRUCT_ARRAY;
2008 if(token.type == T_static) {
2009 array->is_static = true;
2013 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2014 if(type_qualifiers != 0) {
2015 if(token.type == T_static) {
2016 array->is_static = true;
2020 array->type_qualifiers = type_qualifiers;
2022 if(token.type == '*' && look_ahead(1)->type == ']') {
2023 array->is_variable = true;
2025 } else if(token.type != ']') {
2026 array->size = parse_assignment_expression();
2031 return (construct_type_t*) array;
2034 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2038 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2040 declaration_t *parameters = parse_parameters(&type->function);
2041 if(declaration != NULL) {
2042 declaration->context.declarations = parameters;
2045 construct_function_type_t *construct_function_type =
2046 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2047 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2048 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2049 construct_function_type->function_type = type;
2053 return (construct_type_t*) construct_function_type;
2056 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2057 bool may_be_abstract)
2059 /* construct a single linked list of construct_type_t's which describe
2060 * how to construct the final declarator type */
2061 construct_type_t *first = NULL;
2062 construct_type_t *last = NULL;
2065 while(token.type == '*') {
2066 construct_type_t *type = parse_pointer_declarator();
2077 /* TODO: find out if this is correct */
2080 construct_type_t *inner_types = NULL;
2082 switch(token.type) {
2084 if(declaration == NULL) {
2085 errorf(HERE, "no identifier expected in typename");
2087 declaration->symbol = token.v.symbol;
2088 declaration->source_position = token.source_position;
2094 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2100 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2101 /* avoid a loop in the outermost scope, because eat_statement doesn't
2103 if(token.type == '}' && current_function == NULL) {
2111 construct_type_t *p = last;
2114 construct_type_t *type;
2115 switch(token.type) {
2117 type = parse_function_declarator(declaration);
2120 type = parse_array_declarator();
2123 goto declarator_finished;
2126 /* insert in the middle of the list (behind p) */
2128 type->next = p->next;
2139 declarator_finished:
2142 /* append inner_types at the end of the list, we don't to set last anymore
2143 * as it's not needed anymore */
2145 assert(first == NULL);
2146 first = inner_types;
2148 last->next = inner_types;
2154 static type_t *construct_declarator_type(construct_type_t *construct_list,
2157 construct_type_t *iter = construct_list;
2158 for( ; iter != NULL; iter = iter->next) {
2159 switch(iter->type) {
2160 case CONSTRUCT_INVALID:
2161 panic("invalid type construction found");
2162 case CONSTRUCT_FUNCTION: {
2163 construct_function_type_t *construct_function_type
2164 = (construct_function_type_t*) iter;
2166 type_t *function_type = construct_function_type->function_type;
2168 function_type->function.return_type = type;
2170 type = function_type;
2174 case CONSTRUCT_POINTER: {
2175 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2176 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2177 pointer_type->pointer.points_to = type;
2178 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2180 type = pointer_type;
2184 case CONSTRUCT_ARRAY: {
2185 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2186 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2188 array_type->base.qualifiers = parsed_array->type_qualifiers;
2189 array_type->array.element_type = type;
2190 array_type->array.is_static = parsed_array->is_static;
2191 array_type->array.is_variable = parsed_array->is_variable;
2192 array_type->array.size = parsed_array->size;
2199 type_t *hashed_type = typehash_insert(type);
2200 if(hashed_type != type) {
2201 /* the function type was constructed earlier freeing it here will
2202 * destroy other types... */
2203 if(iter->type != CONSTRUCT_FUNCTION) {
2213 static declaration_t *parse_declarator(
2214 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2216 type_t *type = specifiers->type;
2217 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2218 declaration->storage_class = specifiers->storage_class;
2219 declaration->decl_modifiers = specifiers->decl_modifiers;
2220 declaration->is_inline = specifiers->is_inline;
2222 construct_type_t *construct_type
2223 = parse_inner_declarator(declaration, may_be_abstract);
2224 declaration->type = construct_declarator_type(construct_type, type);
2226 if(construct_type != NULL) {
2227 obstack_free(&temp_obst, construct_type);
2233 static type_t *parse_abstract_declarator(type_t *base_type)
2235 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2237 type_t *result = construct_declarator_type(construct_type, base_type);
2238 if(construct_type != NULL) {
2239 obstack_free(&temp_obst, construct_type);
2245 static declaration_t *record_declaration(declaration_t *declaration)
2247 assert(declaration->parent_context == NULL);
2248 assert(context != NULL);
2250 symbol_t *symbol = declaration->symbol;
2251 if(symbol != NULL) {
2252 declaration_t *alias = environment_push(declaration);
2253 if(alias != declaration)
2256 declaration->parent_context = context;
2259 if(last_declaration != NULL) {
2260 last_declaration->next = declaration;
2262 context->declarations = declaration;
2264 last_declaration = declaration;
2269 static void parser_error_multiple_definition(declaration_t *declaration,
2270 const source_position_t source_position)
2272 errorf(source_position, "multiple definition of symbol '%s'", declaration->symbol->string);
2273 errorf(declaration->source_position, "this is the location of the previous definition.");
2276 static bool is_declaration_specifier(const token_t *token,
2277 bool only_type_specifiers)
2279 switch(token->type) {
2283 return is_typedef_symbol(token->v.symbol);
2285 case T___extension__:
2288 return !only_type_specifiers;
2295 static void parse_init_declarator_rest(declaration_t *declaration)
2299 type_t *orig_type = declaration->type;
2300 type_t *type = NULL;
2301 if(orig_type != NULL)
2302 type = skip_typeref(orig_type);
2304 if(declaration->init.initializer != NULL) {
2305 parser_error_multiple_definition(declaration, token.source_position);
2308 initializer_t *initializer = parse_initializer(type);
2310 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2311 * the array type size */
2312 if(type != NULL && is_type_array(type) && initializer != NULL) {
2313 array_type_t *array_type = &type->array;
2315 if(array_type->size == NULL) {
2316 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2318 cnst->base.datatype = type_size_t;
2320 switch (initializer->kind) {
2321 case INITIALIZER_LIST: {
2322 initializer_list_t *const list = &initializer->list;
2323 cnst->conste.v.int_value = list->len;
2327 case INITIALIZER_STRING: {
2328 initializer_string_t *const string = &initializer->string;
2329 cnst->conste.v.int_value = strlen(string->string) + 1;
2333 case INITIALIZER_WIDE_STRING: {
2334 initializer_wide_string_t *const string = &initializer->wide_string;
2335 cnst->conste.v.int_value = string->string.size;
2340 panic("invalid initializer type");
2343 array_type->size = cnst;
2347 if(type != NULL && is_type_function(type)) {
2348 errorf(declaration->source_position, "initializers not allowed for function types at declator '%s' (type '%T')", declaration->symbol->string, orig_type);
2350 declaration->init.initializer = initializer;
2354 /* parse rest of a declaration without any declarator */
2355 static void parse_anonymous_declaration_rest(
2356 const declaration_specifiers_t *specifiers,
2357 parsed_declaration_func finished_declaration)
2361 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2363 declaration->type = specifiers->type;
2364 declaration->storage_class = specifiers->storage_class;
2365 declaration->source_position = specifiers->source_position;
2367 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2368 warningf(declaration->source_position, "useless storage class in empty declaration");
2371 type_t *type = declaration->type;
2372 switch (type->kind) {
2373 case TYPE_COMPOUND_STRUCT:
2374 case TYPE_COMPOUND_UNION: {
2375 const compound_type_t *compound_type = &type->compound;
2376 if (compound_type->declaration->symbol == NULL) {
2377 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2386 warningf(declaration->source_position, "empty declaration");
2390 finished_declaration(declaration);
2393 static void parse_declaration_rest(declaration_t *ndeclaration,
2394 const declaration_specifiers_t *specifiers,
2395 parsed_declaration_func finished_declaration)
2398 declaration_t *declaration = finished_declaration(ndeclaration);
2400 type_t *orig_type = declaration->type;
2401 type_t *type = skip_typeref(orig_type);
2403 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2404 warningf(declaration->source_position, "variable '%s' declared 'inline'\n", declaration->symbol->string);
2407 if(token.type == '=') {
2408 parse_init_declarator_rest(declaration);
2411 if(token.type != ',')
2415 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2420 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2422 /* TODO: check that it was actually a parameter that gets a type */
2424 /* we should have a declaration for the parameter in the current
2426 return record_declaration(declaration);
2429 static void parse_declaration(parsed_declaration_func finished_declaration)
2431 declaration_specifiers_t specifiers;
2432 memset(&specifiers, 0, sizeof(specifiers));
2433 parse_declaration_specifiers(&specifiers);
2435 if(token.type == ';') {
2436 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2438 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2439 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2443 static void parse_kr_declaration_list(declaration_t *declaration)
2445 type_t *type = skip_typeref(declaration->type);
2446 if(!is_type_function(type))
2449 if(!type->function.kr_style_parameters)
2452 /* push function parameters */
2453 int top = environment_top();
2454 context_t *last_context = context;
2455 set_context(&declaration->context);
2457 declaration_t *parameter = declaration->context.declarations;
2458 for( ; parameter != NULL; parameter = parameter->next) {
2459 environment_push(parameter);
2462 /* parse declaration list */
2463 while(is_declaration_specifier(&token, false)) {
2464 parse_declaration(finished_kr_declaration);
2467 /* pop function parameters */
2468 assert(context == &declaration->context);
2469 set_context(last_context);
2470 environment_pop_to(top);
2472 /* update function type */
2473 type_t *new_type = duplicate_type(type);
2474 new_type->function.kr_style_parameters = false;
2476 function_parameter_t *parameters = NULL;
2477 function_parameter_t *last_parameter = NULL;
2479 declaration_t *parameter_declaration = declaration->context.declarations;
2480 for( ; parameter_declaration != NULL;
2481 parameter_declaration = parameter_declaration->next) {
2482 type_t *parameter_type = parameter_declaration->type;
2483 if(parameter_type == NULL) {
2485 errorf(HERE, "no type specified for function parameter '%s'", parameter_declaration->symbol->string);
2487 warningf(HERE, "no type specified for function parameter '%s', using int", parameter_declaration->symbol->string);
2488 parameter_type = type_int;
2489 parameter_declaration->type = parameter_type;
2493 semantic_parameter(parameter_declaration);
2494 parameter_type = parameter_declaration->type;
2496 function_parameter_t *function_parameter
2497 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2498 memset(function_parameter, 0, sizeof(function_parameter[0]));
2500 function_parameter->type = parameter_type;
2501 if(last_parameter != NULL) {
2502 last_parameter->next = function_parameter;
2504 parameters = function_parameter;
2506 last_parameter = function_parameter;
2508 new_type->function.parameters = parameters;
2510 type = typehash_insert(new_type);
2511 if(type != new_type) {
2512 obstack_free(type_obst, new_type);
2515 declaration->type = type;
2518 static void parse_external_declaration(void)
2520 /* function-definitions and declarations both start with declaration
2522 declaration_specifiers_t specifiers;
2523 memset(&specifiers, 0, sizeof(specifiers));
2524 parse_declaration_specifiers(&specifiers);
2526 /* must be a declaration */
2527 if(token.type == ';') {
2528 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2532 /* declarator is common to both function-definitions and declarations */
2533 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2535 /* must be a declaration */
2536 if(token.type == ',' || token.type == '=' || token.type == ';') {
2537 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2541 /* must be a function definition */
2542 parse_kr_declaration_list(ndeclaration);
2544 if(token.type != '{') {
2545 parse_error_expected("while parsing function definition", '{', 0);
2550 type_t *type = ndeclaration->type;
2556 /* note that we don't skip typerefs: the standard doesn't allow them here
2557 * (so we can't use is_type_function here) */
2558 if(type->kind != TYPE_FUNCTION) {
2559 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2564 /* § 6.7.5.3 (14) a function definition with () means no
2565 * parameters (and not unspecified parameters) */
2566 if(type->function.unspecified_parameters) {
2567 type_t *duplicate = duplicate_type(type);
2568 duplicate->function.unspecified_parameters = false;
2570 type = typehash_insert(duplicate);
2571 if(type != duplicate) {
2572 obstack_free(type_obst, duplicate);
2574 ndeclaration->type = type;
2577 declaration_t *declaration = record_declaration(ndeclaration);
2578 if(ndeclaration != declaration) {
2579 memcpy(&declaration->context, &ndeclaration->context,
2580 sizeof(declaration->context));
2582 type = skip_typeref(declaration->type);
2584 /* push function parameters and switch context */
2585 int top = environment_top();
2586 context_t *last_context = context;
2587 set_context(&declaration->context);
2589 declaration_t *parameter = declaration->context.declarations;
2590 for( ; parameter != NULL; parameter = parameter->next) {
2591 environment_push(parameter);
2594 if(declaration->init.statement != NULL) {
2595 parser_error_multiple_definition(declaration, token.source_position);
2597 goto end_of_parse_external_declaration;
2599 /* parse function body */
2600 int label_stack_top = label_top();
2601 declaration_t *old_current_function = current_function;
2602 current_function = declaration;
2604 declaration->init.statement = parse_compound_statement();
2606 assert(current_function == declaration);
2607 current_function = old_current_function;
2608 label_pop_to(label_stack_top);
2611 end_of_parse_external_declaration:
2612 assert(context == &declaration->context);
2613 set_context(last_context);
2614 environment_pop_to(top);
2617 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2620 if(token.type == ':') {
2622 parse_constant_expression();
2623 /* TODO (bitfields) */
2625 declaration_t *declaration = parse_declarator(specifiers, /*may_be_abstract=*/true);
2627 /* TODO: check constraints for struct declarations */
2628 /* TODO: check for doubled fields */
2629 record_declaration(declaration);
2631 if(token.type == ':') {
2633 parse_constant_expression();
2634 /* TODO (bitfields) */
2638 if(token.type != ',')
2645 static void parse_compound_type_entries(void)
2649 while(token.type != '}' && token.type != T_EOF) {
2650 declaration_specifiers_t specifiers;
2651 memset(&specifiers, 0, sizeof(specifiers));
2652 parse_declaration_specifiers(&specifiers);
2654 parse_struct_declarators(&specifiers);
2656 if(token.type == T_EOF) {
2657 errorf(HERE, "EOF while parsing struct");
2662 static type_t *parse_typename(void)
2664 declaration_specifiers_t specifiers;
2665 memset(&specifiers, 0, sizeof(specifiers));
2666 parse_declaration_specifiers(&specifiers);
2667 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2668 /* TODO: improve error message, user does probably not know what a
2669 * storage class is...
2671 errorf(HERE, "typename may not have a storage class");
2674 type_t *result = parse_abstract_declarator(specifiers.type);
2682 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2683 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2684 expression_t *left);
2686 typedef struct expression_parser_function_t expression_parser_function_t;
2687 struct expression_parser_function_t {
2688 unsigned precedence;
2689 parse_expression_function parser;
2690 unsigned infix_precedence;
2691 parse_expression_infix_function infix_parser;
2694 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2696 static expression_t *create_invalid_expression(void)
2698 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2699 expression->base.source_position = token.source_position;
2703 static expression_t *expected_expression_error(void)
2705 errorf(HERE, "expected expression, got token '%K'", &token);
2709 return create_invalid_expression();
2712 static expression_t *parse_string_const(void)
2714 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2715 cnst->base.datatype = type_string;
2716 cnst->string.value = parse_string_literals();
2721 static expression_t *parse_wide_string_const(void)
2723 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2724 cnst->base.datatype = type_wchar_t_ptr;
2725 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2730 static expression_t *parse_int_const(void)
2732 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2733 cnst->base.datatype = token.datatype;
2734 cnst->conste.v.int_value = token.v.intvalue;
2741 static expression_t *parse_float_const(void)
2743 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2744 cnst->base.datatype = token.datatype;
2745 cnst->conste.v.float_value = token.v.floatvalue;
2752 static declaration_t *create_implicit_function(symbol_t *symbol,
2753 const source_position_t source_position)
2755 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2756 ntype->function.return_type = type_int;
2757 ntype->function.unspecified_parameters = true;
2759 type_t *type = typehash_insert(ntype);
2764 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2766 declaration->storage_class = STORAGE_CLASS_EXTERN;
2767 declaration->type = type;
2768 declaration->symbol = symbol;
2769 declaration->source_position = source_position;
2771 /* prepend the implicit definition to the global context
2772 * this is safe since the symbol wasn't declared as anything else yet
2774 assert(symbol->declaration == NULL);
2776 context_t *last_context = context;
2777 context = global_context;
2779 environment_push(declaration);
2780 declaration->next = context->declarations;
2781 context->declarations = declaration;
2783 context = last_context;
2788 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2790 function_parameter_t *parameter
2791 = obstack_alloc(type_obst, sizeof(parameter[0]));
2792 memset(parameter, 0, sizeof(parameter[0]));
2793 parameter->type = argument_type;
2795 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2796 type->function.return_type = return_type;
2797 type->function.parameters = parameter;
2799 type_t *result = typehash_insert(type);
2800 if(result != type) {
2807 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2809 switch(symbol->ID) {
2810 case T___builtin_alloca:
2811 return make_function_1_type(type_void_ptr, type_size_t);
2812 case T___builtin_nan:
2813 return make_function_1_type(type_double, type_string);
2814 case T___builtin_nanf:
2815 return make_function_1_type(type_float, type_string);
2816 case T___builtin_nand:
2817 return make_function_1_type(type_long_double, type_string);
2818 case T___builtin_va_end:
2819 return make_function_1_type(type_void, type_valist);
2821 panic("not implemented builtin symbol found");
2826 * performs automatic type cast as described in § 6.3.2.1
2828 static type_t *automatic_type_conversion(type_t *orig_type)
2830 if(orig_type == NULL)
2833 type_t *type = skip_typeref(orig_type);
2834 if(is_type_array(type)) {
2835 array_type_t *array_type = &type->array;
2836 type_t *element_type = array_type->element_type;
2837 unsigned qualifiers = array_type->type.qualifiers;
2839 return make_pointer_type(element_type, qualifiers);
2842 if(is_type_function(type)) {
2843 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
2850 * reverts the automatic casts of array to pointer types and function
2851 * to function-pointer types as defined § 6.3.2.1
2853 type_t *revert_automatic_type_conversion(const expression_t *expression)
2855 if(expression->base.datatype == NULL)
2858 switch(expression->kind) {
2859 case EXPR_REFERENCE: {
2860 const reference_expression_t *ref = &expression->reference;
2861 return ref->declaration->type;
2864 const select_expression_t *select = &expression->select;
2865 return select->compound_entry->type;
2867 case EXPR_UNARY_DEREFERENCE: {
2868 expression_t *value = expression->unary.value;
2869 type_t *type = skip_typeref(value->base.datatype);
2870 pointer_type_t *pointer_type = &type->pointer;
2872 return pointer_type->points_to;
2874 case EXPR_BUILTIN_SYMBOL: {
2875 const builtin_symbol_expression_t *builtin
2876 = &expression->builtin_symbol;
2877 return get_builtin_symbol_type(builtin->symbol);
2879 case EXPR_ARRAY_ACCESS: {
2880 const array_access_expression_t *array_access
2881 = &expression->array_access;
2882 const expression_t *array_ref = array_access->array_ref;
2883 type_t *type_left = skip_typeref(array_ref->base.datatype);
2884 assert(is_type_pointer(type_left));
2885 pointer_type_t *pointer_type = &type_left->pointer;
2886 return pointer_type->points_to;
2893 return expression->base.datatype;
2896 static expression_t *parse_reference(void)
2898 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2900 reference_expression_t *ref = &expression->reference;
2901 ref->symbol = token.v.symbol;
2903 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2905 source_position_t source_position = token.source_position;
2908 if(declaration == NULL) {
2910 /* an implicitly defined function */
2911 if(token.type == '(') {
2912 warningf(HERE, "implicit declaration of function '%s'\n", ref->symbol->string);
2914 declaration = create_implicit_function(ref->symbol,
2919 errorf(HERE, "unknown symbol '%s' found.\n", ref->symbol->string);
2924 type_t *type = declaration->type;
2925 /* we always do the auto-type conversions; the & and sizeof parser contains
2926 * code to revert this! */
2927 type = automatic_type_conversion(type);
2929 ref->declaration = declaration;
2930 ref->expression.datatype = type;
2935 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2939 /* TODO check if explicit cast is allowed and issue warnings/errors */
2942 static expression_t *parse_cast(void)
2944 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
2946 cast->base.source_position = token.source_position;
2948 type_t *type = parse_typename();
2951 expression_t *value = parse_sub_expression(20);
2953 check_cast_allowed(value, type);
2955 cast->base.datatype = type;
2956 cast->unary.value = value;
2961 static expression_t *parse_statement_expression(void)
2963 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
2965 statement_t *statement = parse_compound_statement();
2966 expression->statement.statement = statement;
2967 if(statement == NULL) {
2972 assert(statement->kind == STATEMENT_COMPOUND);
2973 compound_statement_t *compound_statement = &statement->compound;
2975 /* find last statement and use it's type */
2976 const statement_t *last_statement = NULL;
2977 const statement_t *iter = compound_statement->statements;
2978 for( ; iter != NULL; iter = iter->base.next) {
2979 last_statement = iter;
2982 if(last_statement->kind == STATEMENT_EXPRESSION) {
2983 const expression_statement_t *expression_statement
2984 = &last_statement->expression;
2985 expression->base.datatype
2986 = expression_statement->expression->base.datatype;
2988 expression->base.datatype = type_void;
2996 static expression_t *parse_brace_expression(void)
3000 switch(token.type) {
3002 /* gcc extension: a statement expression */
3003 return parse_statement_expression();
3007 return parse_cast();
3009 if(is_typedef_symbol(token.v.symbol)) {
3010 return parse_cast();
3014 expression_t *result = parse_expression();
3020 static expression_t *parse_function_keyword(void)
3025 if (current_function == NULL) {
3026 errorf(HERE, "'__func__' used outside of a function");
3029 string_literal_expression_t *expression
3030 = allocate_ast_zero(sizeof(expression[0]));
3032 expression->expression.kind = EXPR_FUNCTION;
3033 expression->expression.datatype = type_string;
3034 expression->value = current_function->symbol->string;
3036 return (expression_t*) expression;
3039 static expression_t *parse_pretty_function_keyword(void)
3041 eat(T___PRETTY_FUNCTION__);
3044 if (current_function == NULL) {
3045 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3048 string_literal_expression_t *expression
3049 = allocate_ast_zero(sizeof(expression[0]));
3051 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3052 expression->expression.datatype = type_string;
3053 expression->value = current_function->symbol->string;
3055 return (expression_t*) expression;
3058 static designator_t *parse_designator(void)
3060 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3062 if(token.type != T_IDENTIFIER) {
3063 parse_error_expected("while parsing member designator",
3068 result->symbol = token.v.symbol;
3071 designator_t *last_designator = result;
3073 if(token.type == '.') {
3075 if(token.type != T_IDENTIFIER) {
3076 parse_error_expected("while parsing member designator",
3081 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3082 designator->symbol = token.v.symbol;
3085 last_designator->next = designator;
3086 last_designator = designator;
3089 if(token.type == '[') {
3091 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3092 designator->array_access = parse_expression();
3093 if(designator->array_access == NULL) {
3099 last_designator->next = designator;
3100 last_designator = designator;
3109 static expression_t *parse_offsetof(void)
3111 eat(T___builtin_offsetof);
3113 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3114 expression->base.datatype = type_size_t;
3117 expression->offsetofe.type = parse_typename();
3119 expression->offsetofe.designator = parse_designator();
3125 static expression_t *parse_va_start(void)
3127 eat(T___builtin_va_start);
3129 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3132 expression->va_starte.ap = parse_assignment_expression();
3134 expression_t *const expr = parse_assignment_expression();
3135 if (expr->kind == EXPR_REFERENCE) {
3136 declaration_t *const decl = expr->reference.declaration;
3137 if (decl->parent_context == ¤t_function->context &&
3138 decl->next == NULL) {
3139 expression->va_starte.parameter = decl;
3144 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3146 return create_invalid_expression();
3149 static expression_t *parse_va_arg(void)
3151 eat(T___builtin_va_arg);
3153 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3156 expression->va_arge.ap = parse_assignment_expression();
3158 expression->base.datatype = parse_typename();
3164 static expression_t *parse_builtin_symbol(void)
3166 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3168 symbol_t *symbol = token.v.symbol;
3170 expression->builtin_symbol.symbol = symbol;
3173 type_t *type = get_builtin_symbol_type(symbol);
3174 type = automatic_type_conversion(type);
3176 expression->base.datatype = type;
3180 static expression_t *parse_compare_builtin(void)
3182 expression_t *expression;
3184 switch(token.type) {
3185 case T___builtin_isgreater:
3186 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3188 case T___builtin_isgreaterequal:
3189 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3191 case T___builtin_isless:
3192 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3194 case T___builtin_islessequal:
3195 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3197 case T___builtin_islessgreater:
3198 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3200 case T___builtin_isunordered:
3201 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3204 panic("invalid compare builtin found");
3210 expression->binary.left = parse_assignment_expression();
3212 expression->binary.right = parse_assignment_expression();
3215 type_t *orig_type_left = expression->binary.left->base.datatype;
3216 type_t *orig_type_right = expression->binary.right->base.datatype;
3217 if(orig_type_left == NULL || orig_type_right == NULL)
3220 type_t *type_left = skip_typeref(orig_type_left);
3221 type_t *type_right = skip_typeref(orig_type_right);
3222 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3223 type_error_incompatible("invalid operands in comparison",
3224 token.source_position, type_left, type_right);
3226 semantic_comparison(&expression->binary);
3232 static expression_t *parse_builtin_expect(void)
3234 eat(T___builtin_expect);
3236 expression_t *expression
3237 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3240 expression->binary.left = parse_assignment_expression();
3242 expression->binary.right = parse_constant_expression();
3245 expression->base.datatype = expression->binary.left->base.datatype;
3250 static expression_t *parse_assume(void) {
3253 expression_t *expression
3254 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3257 expression->unary.value = parse_expression();
3260 expression->base.datatype = type_void;
3264 static expression_t *parse_alignof(void) {
3267 expression_t *expression
3268 = allocate_expression_zero(EXPR_ALIGNOF);
3271 expression->alignofe.type = parse_typename();
3274 expression->base.datatype = type_size_t;
3278 static expression_t *parse_primary_expression(void)
3280 switch(token.type) {
3282 return parse_int_const();
3283 case T_FLOATINGPOINT:
3284 return parse_float_const();
3285 case T_STRING_LITERAL: /* TODO merge */
3286 return parse_string_const();
3287 case T_WIDE_STRING_LITERAL:
3288 return parse_wide_string_const();
3290 return parse_reference();
3291 case T___FUNCTION__:
3293 return parse_function_keyword();
3294 case T___PRETTY_FUNCTION__:
3295 return parse_pretty_function_keyword();
3296 case T___builtin_offsetof:
3297 return parse_offsetof();
3298 case T___builtin_va_start:
3299 return parse_va_start();
3300 case T___builtin_va_arg:
3301 return parse_va_arg();
3302 case T___builtin_expect:
3303 return parse_builtin_expect();
3304 case T___builtin_nanf:
3305 case T___builtin_alloca:
3306 case T___builtin_va_end:
3307 return parse_builtin_symbol();
3308 case T___builtin_isgreater:
3309 case T___builtin_isgreaterequal:
3310 case T___builtin_isless:
3311 case T___builtin_islessequal:
3312 case T___builtin_islessgreater:
3313 case T___builtin_isunordered:
3314 return parse_compare_builtin();
3316 return parse_alignof();
3318 return parse_assume();
3321 return parse_brace_expression();
3324 errorf(HERE, "unexpected token '%K'", &token);
3327 return create_invalid_expression();
3330 static expression_t *parse_array_expression(unsigned precedence,
3337 expression_t *inside = parse_expression();
3339 array_access_expression_t *array_access
3340 = allocate_ast_zero(sizeof(array_access[0]));
3342 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3344 type_t *type_left = left->base.datatype;
3345 type_t *type_inside = inside->base.datatype;
3346 type_t *return_type = NULL;
3348 if(type_left != NULL && type_inside != NULL) {
3349 type_left = skip_typeref(type_left);
3350 type_inside = skip_typeref(type_inside);
3352 if(is_type_pointer(type_left)) {
3353 pointer_type_t *pointer = &type_left->pointer;
3354 return_type = pointer->points_to;
3355 array_access->array_ref = left;
3356 array_access->index = inside;
3357 } else if(is_type_pointer(type_inside)) {
3358 pointer_type_t *pointer = &type_inside->pointer;
3359 return_type = pointer->points_to;
3360 array_access->array_ref = inside;
3361 array_access->index = left;
3362 array_access->flipped = true;
3364 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3367 array_access->array_ref = left;
3368 array_access->index = inside;
3371 if(token.type != ']') {
3372 parse_error_expected("Problem while parsing array access", ']', 0);
3373 return (expression_t*) array_access;
3377 return_type = automatic_type_conversion(return_type);
3378 array_access->expression.datatype = return_type;
3380 return (expression_t*) array_access;
3383 static expression_t *parse_sizeof(unsigned precedence)
3387 sizeof_expression_t *sizeof_expression
3388 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3389 sizeof_expression->expression.kind = EXPR_SIZEOF;
3390 sizeof_expression->expression.datatype = type_size_t;
3392 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3394 sizeof_expression->type = parse_typename();
3397 expression_t *expression = parse_sub_expression(precedence);
3398 expression->base.datatype = revert_automatic_type_conversion(expression);
3400 sizeof_expression->type = expression->base.datatype;
3401 sizeof_expression->size_expression = expression;
3404 return (expression_t*) sizeof_expression;
3407 static expression_t *parse_select_expression(unsigned precedence,
3408 expression_t *compound)
3411 assert(token.type == '.' || token.type == T_MINUSGREATER);
3413 bool is_pointer = (token.type == T_MINUSGREATER);
3416 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3417 select->select.compound = compound;
3419 if(token.type != T_IDENTIFIER) {
3420 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3423 symbol_t *symbol = token.v.symbol;
3424 select->select.symbol = symbol;
3427 type_t *orig_type = compound->base.datatype;
3428 if(orig_type == NULL)
3429 return create_invalid_expression();
3431 type_t *type = skip_typeref(orig_type);
3433 type_t *type_left = type;
3435 if(type->kind != TYPE_POINTER) {
3436 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3437 return create_invalid_expression();
3439 pointer_type_t *pointer_type = &type->pointer;
3440 type_left = pointer_type->points_to;
3442 type_left = skip_typeref(type_left);
3444 if(type_left->kind != TYPE_COMPOUND_STRUCT
3445 && type_left->kind != TYPE_COMPOUND_UNION) {
3446 errorf(HERE, "request for member '%s' in something not a struct or union, but '%T'", symbol->string, type_left);
3447 return create_invalid_expression();
3450 compound_type_t *compound_type = &type_left->compound;
3451 declaration_t *declaration = compound_type->declaration;
3453 if(!declaration->init.is_defined) {
3454 errorf(HERE, "request for member '%s' of incomplete type '%T'", symbol->string, type_left);
3455 return create_invalid_expression();
3458 declaration_t *iter = declaration->context.declarations;
3459 for( ; iter != NULL; iter = iter->next) {
3460 if(iter->symbol == symbol) {
3465 errorf(HERE, "'%T' has no member names '%s'", type_left, symbol->string);
3466 return create_invalid_expression();
3469 /* we always do the auto-type conversions; the & and sizeof parser contains
3470 * code to revert this! */
3471 type_t *expression_type = automatic_type_conversion(iter->type);
3473 select->select.compound_entry = iter;
3474 select->base.datatype = expression_type;
3478 static expression_t *parse_call_expression(unsigned precedence,
3479 expression_t *expression)
3482 expression_t *result = allocate_expression_zero(EXPR_CALL);
3484 call_expression_t *call = &result->call;
3485 call->function = expression;
3487 function_type_t *function_type = NULL;
3488 type_t *orig_type = expression->base.datatype;
3489 if(orig_type != NULL) {
3490 type_t *type = skip_typeref(orig_type);
3492 if(is_type_pointer(type)) {
3493 pointer_type_t *pointer_type = &type->pointer;
3495 type = skip_typeref(pointer_type->points_to);
3497 if (is_type_function(type)) {
3498 function_type = &type->function;
3499 call->expression.datatype = function_type->return_type;
3502 if(function_type == NULL) {
3503 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3505 function_type = NULL;
3506 call->expression.datatype = NULL;
3510 /* parse arguments */
3513 if(token.type != ')') {
3514 call_argument_t *last_argument = NULL;
3517 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3519 argument->expression = parse_assignment_expression();
3520 if(last_argument == NULL) {
3521 call->arguments = argument;
3523 last_argument->next = argument;
3525 last_argument = argument;
3527 if(token.type != ',')
3534 if(function_type != NULL) {
3535 function_parameter_t *parameter = function_type->parameters;
3536 call_argument_t *argument = call->arguments;
3537 for( ; parameter != NULL && argument != NULL;
3538 parameter = parameter->next, argument = argument->next) {
3539 type_t *expected_type = parameter->type;
3540 /* TODO report context in error messages */
3541 argument->expression = create_implicit_cast(argument->expression,
3544 /* too few parameters */
3545 if(parameter != NULL) {
3546 errorf(HERE, "too few arguments to function '%E'", expression);
3547 } else if(argument != NULL) {
3548 /* too many parameters */
3549 if(!function_type->variadic
3550 && !function_type->unspecified_parameters) {
3551 errorf(HERE, "too many arguments to function '%E'", expression);
3553 /* do default promotion */
3554 for( ; argument != NULL; argument = argument->next) {
3555 type_t *type = argument->expression->base.datatype;
3560 type = skip_typeref(type);
3561 if(is_type_integer(type)) {
3562 type = promote_integer(type);
3563 } else if(type == type_float) {
3567 argument->expression
3568 = create_implicit_cast(argument->expression, type);
3571 check_format(&result->call);
3574 check_format(&result->call);
3581 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3583 static bool same_compound_type(const type_t *type1, const type_t *type2)
3585 if(!is_type_compound(type1))
3587 if(type1->kind != type2->kind)
3590 const compound_type_t *compound1 = &type1->compound;
3591 const compound_type_t *compound2 = &type2->compound;
3593 return compound1->declaration == compound2->declaration;
3596 static expression_t *parse_conditional_expression(unsigned precedence,
3597 expression_t *expression)
3601 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3603 conditional_expression_t *conditional = &result->conditional;
3604 conditional->condition = expression;
3607 type_t *condition_type_orig = expression->base.datatype;
3608 if(condition_type_orig != NULL) {
3609 type_t *condition_type = skip_typeref(condition_type_orig);
3610 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3611 type_error("expected a scalar type in conditional condition",
3612 expression->base.source_position, condition_type_orig);
3616 expression_t *true_expression = parse_expression();
3618 expression_t *false_expression = parse_sub_expression(precedence);
3620 conditional->true_expression = true_expression;
3621 conditional->false_expression = false_expression;
3623 type_t *orig_true_type = true_expression->base.datatype;
3624 type_t *orig_false_type = false_expression->base.datatype;
3625 if(orig_true_type == NULL || orig_false_type == NULL)
3628 type_t *true_type = skip_typeref(orig_true_type);
3629 type_t *false_type = skip_typeref(orig_false_type);
3632 type_t *result_type = NULL;
3633 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3634 result_type = semantic_arithmetic(true_type, false_type);
3636 true_expression = create_implicit_cast(true_expression, result_type);
3637 false_expression = create_implicit_cast(false_expression, result_type);
3639 conditional->true_expression = true_expression;
3640 conditional->false_expression = false_expression;
3641 conditional->expression.datatype = result_type;
3642 } else if (same_compound_type(true_type, false_type)
3643 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3644 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3645 /* just take 1 of the 2 types */
3646 result_type = true_type;
3647 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3648 && pointers_compatible(true_type, false_type)) {
3650 result_type = true_type;
3653 type_error_incompatible("while parsing conditional",
3654 expression->base.source_position, true_type,
3658 conditional->expression.datatype = result_type;
3662 static expression_t *parse_extension(unsigned precedence)
3664 eat(T___extension__);
3666 /* TODO enable extensions */
3668 return parse_sub_expression(precedence);
3671 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3673 eat(T___builtin_classify_type);
3675 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3676 result->base.datatype = type_int;
3679 expression_t *expression = parse_sub_expression(precedence);
3681 result->classify_type.type_expression = expression;
3686 static void semantic_incdec(unary_expression_t *expression)
3688 type_t *orig_type = expression->value->base.datatype;
3689 if(orig_type == NULL)
3692 type_t *type = skip_typeref(orig_type);
3693 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3694 /* TODO: improve error message */
3695 errorf(HERE, "operation needs an arithmetic or pointer type");
3699 expression->expression.datatype = orig_type;
3702 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3704 type_t *orig_type = expression->value->base.datatype;
3705 if(orig_type == NULL)
3708 type_t *type = skip_typeref(orig_type);
3709 if(!is_type_arithmetic(type)) {
3710 /* TODO: improve error message */
3711 errorf(HERE, "operation needs an arithmetic type");
3715 expression->expression.datatype = orig_type;
3718 static void semantic_unexpr_scalar(unary_expression_t *expression)
3720 type_t *orig_type = expression->value->base.datatype;
3721 if(orig_type == NULL)
3724 type_t *type = skip_typeref(orig_type);
3725 if (!is_type_scalar(type)) {
3726 errorf(HERE, "operand of ! must be of scalar type");
3730 expression->expression.datatype = orig_type;
3733 static void semantic_unexpr_integer(unary_expression_t *expression)
3735 type_t *orig_type = expression->value->base.datatype;
3736 if(orig_type == NULL)
3739 type_t *type = skip_typeref(orig_type);
3740 if (!is_type_integer(type)) {
3741 errorf(HERE, "operand of ~ must be of integer type");
3745 expression->expression.datatype = orig_type;
3748 static void semantic_dereference(unary_expression_t *expression)
3750 type_t *orig_type = expression->value->base.datatype;
3751 if(orig_type == NULL)
3754 type_t *type = skip_typeref(orig_type);
3755 if(!is_type_pointer(type)) {
3756 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
3760 pointer_type_t *pointer_type = &type->pointer;
3761 type_t *result_type = pointer_type->points_to;
3763 result_type = automatic_type_conversion(result_type);
3764 expression->expression.datatype = result_type;
3767 static void semantic_take_addr(unary_expression_t *expression)
3769 expression_t *value = expression->value;
3770 value->base.datatype = revert_automatic_type_conversion(value);
3772 type_t *orig_type = value->base.datatype;
3773 if(orig_type == NULL)
3776 if(value->kind == EXPR_REFERENCE) {
3777 reference_expression_t *reference = (reference_expression_t*) value;
3778 declaration_t *declaration = reference->declaration;
3779 if(declaration != NULL) {
3780 declaration->address_taken = 1;
3784 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3787 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3788 static expression_t *parse_##unexpression_type(unsigned precedence) \
3792 expression_t *unary_expression \
3793 = allocate_expression_zero(unexpression_type); \
3794 unary_expression->unary.value = parse_sub_expression(precedence); \
3796 sfunc(&unary_expression->unary); \
3798 return unary_expression; \
3801 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
3802 semantic_unexpr_arithmetic)
3803 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
3804 semantic_unexpr_arithmetic)
3805 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
3806 semantic_unexpr_scalar)
3807 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
3808 semantic_dereference)
3809 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
3811 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
3812 semantic_unexpr_integer)
3813 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
3815 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
3818 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3820 static expression_t *parse_##unexpression_type(unsigned precedence, \
3821 expression_t *left) \
3823 (void) precedence; \
3826 expression_t *unary_expression \
3827 = allocate_expression_zero(unexpression_type); \
3828 unary_expression->unary.value = left; \
3830 sfunc(&unary_expression->unary); \
3832 return unary_expression; \
3835 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
3836 EXPR_UNARY_POSTFIX_INCREMENT,
3838 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
3839 EXPR_UNARY_POSTFIX_DECREMENT,
3842 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3844 /* TODO: handle complex + imaginary types */
3846 /* § 6.3.1.8 Usual arithmetic conversions */
3847 if(type_left == type_long_double || type_right == type_long_double) {
3848 return type_long_double;
3849 } else if(type_left == type_double || type_right == type_double) {
3851 } else if(type_left == type_float || type_right == type_float) {
3855 type_right = promote_integer(type_right);
3856 type_left = promote_integer(type_left);
3858 if(type_left == type_right)
3861 bool signed_left = is_type_signed(type_left);
3862 bool signed_right = is_type_signed(type_right);
3863 int rank_left = get_rank(type_left);
3864 int rank_right = get_rank(type_right);
3865 if(rank_left < rank_right) {
3866 if(signed_left == signed_right || !signed_right) {
3872 if(signed_left == signed_right || !signed_left) {
3880 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3882 expression_t *left = expression->left;
3883 expression_t *right = expression->right;
3884 type_t *orig_type_left = left->base.datatype;
3885 type_t *orig_type_right = right->base.datatype;
3887 if(orig_type_left == NULL || orig_type_right == NULL)
3890 type_t *type_left = skip_typeref(orig_type_left);
3891 type_t *type_right = skip_typeref(orig_type_right);
3893 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3894 /* TODO: improve error message */
3895 errorf(HERE, "operation needs arithmetic types");
3899 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3900 expression->left = create_implicit_cast(left, arithmetic_type);
3901 expression->right = create_implicit_cast(right, arithmetic_type);
3902 expression->expression.datatype = arithmetic_type;
3905 static void semantic_shift_op(binary_expression_t *expression)
3907 expression_t *left = expression->left;
3908 expression_t *right = expression->right;
3909 type_t *orig_type_left = left->base.datatype;
3910 type_t *orig_type_right = right->base.datatype;
3912 if(orig_type_left == NULL || orig_type_right == NULL)
3915 type_t *type_left = skip_typeref(orig_type_left);
3916 type_t *type_right = skip_typeref(orig_type_right);
3918 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3919 /* TODO: improve error message */
3920 errorf(HERE, "operation needs integer types");
3924 type_left = promote_integer(type_left);
3925 type_right = promote_integer(type_right);
3927 expression->left = create_implicit_cast(left, type_left);
3928 expression->right = create_implicit_cast(right, type_right);
3929 expression->expression.datatype = type_left;
3932 static void semantic_add(binary_expression_t *expression)
3934 expression_t *left = expression->left;
3935 expression_t *right = expression->right;
3936 type_t *orig_type_left = left->base.datatype;
3937 type_t *orig_type_right = right->base.datatype;
3939 if(orig_type_left == NULL || orig_type_right == NULL)
3942 type_t *type_left = skip_typeref(orig_type_left);
3943 type_t *type_right = skip_typeref(orig_type_right);
3946 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3947 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3948 expression->left = create_implicit_cast(left, arithmetic_type);
3949 expression->right = create_implicit_cast(right, arithmetic_type);
3950 expression->expression.datatype = arithmetic_type;
3952 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3953 expression->expression.datatype = type_left;
3954 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3955 expression->expression.datatype = type_right;
3957 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
3961 static void semantic_sub(binary_expression_t *expression)
3963 expression_t *left = expression->left;
3964 expression_t *right = expression->right;
3965 type_t *orig_type_left = left->base.datatype;
3966 type_t *orig_type_right = right->base.datatype;
3968 if(orig_type_left == NULL || orig_type_right == NULL)
3971 type_t *type_left = skip_typeref(orig_type_left);
3972 type_t *type_right = skip_typeref(orig_type_right);
3975 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3976 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3977 expression->left = create_implicit_cast(left, arithmetic_type);
3978 expression->right = create_implicit_cast(right, arithmetic_type);
3979 expression->expression.datatype = arithmetic_type;
3981 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3982 expression->expression.datatype = type_left;
3983 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
3984 if(!pointers_compatible(type_left, type_right)) {
3985 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
3987 expression->expression.datatype = type_ptrdiff_t;
3990 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
3994 static void semantic_comparison(binary_expression_t *expression)
3996 expression_t *left = expression->left;
3997 expression_t *right = expression->right;
3998 type_t *orig_type_left = left->base.datatype;
3999 type_t *orig_type_right = right->base.datatype;
4001 if(orig_type_left == NULL || orig_type_right == NULL)
4004 type_t *type_left = skip_typeref(orig_type_left);
4005 type_t *type_right = skip_typeref(orig_type_right);
4007 /* TODO non-arithmetic types */
4008 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4009 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4010 expression->left = create_implicit_cast(left, arithmetic_type);
4011 expression->right = create_implicit_cast(right, arithmetic_type);
4012 expression->expression.datatype = arithmetic_type;
4013 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4014 /* TODO check compatibility */
4015 } else if (is_type_pointer(type_left)) {
4016 expression->right = create_implicit_cast(right, type_left);
4017 } else if (is_type_pointer(type_right)) {
4018 expression->left = create_implicit_cast(left, type_right);
4020 type_error_incompatible("invalid operands in comparison",
4021 token.source_position, type_left, type_right);
4023 expression->expression.datatype = type_int;
4026 static void semantic_arithmetic_assign(binary_expression_t *expression)
4028 expression_t *left = expression->left;
4029 expression_t *right = expression->right;
4030 type_t *orig_type_left = left->base.datatype;
4031 type_t *orig_type_right = right->base.datatype;
4033 if(orig_type_left == NULL || orig_type_right == NULL)
4036 type_t *type_left = skip_typeref(orig_type_left);
4037 type_t *type_right = skip_typeref(orig_type_right);
4039 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4040 /* TODO: improve error message */
4041 errorf(HERE, "operation needs arithmetic types");
4045 /* combined instructions are tricky. We can't create an implicit cast on
4046 * the left side, because we need the uncasted form for the store.
4047 * The ast2firm pass has to know that left_type must be right_type
4048 * for the arithmeitc operation and create a cast by itself */
4049 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4050 expression->right = create_implicit_cast(right, arithmetic_type);
4051 expression->expression.datatype = type_left;
4054 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4056 expression_t *left = expression->left;
4057 expression_t *right = expression->right;
4058 type_t *orig_type_left = left->base.datatype;
4059 type_t *orig_type_right = right->base.datatype;
4061 if(orig_type_left == NULL || orig_type_right == NULL)
4064 type_t *type_left = skip_typeref(orig_type_left);
4065 type_t *type_right = skip_typeref(orig_type_right);
4067 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4068 /* combined instructions are tricky. We can't create an implicit cast on
4069 * the left side, because we need the uncasted form for the store.
4070 * The ast2firm pass has to know that left_type must be right_type
4071 * for the arithmeitc operation and create a cast by itself */
4072 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4073 expression->right = create_implicit_cast(right, arithmetic_type);
4074 expression->expression.datatype = type_left;
4075 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4076 expression->expression.datatype = type_left;
4078 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4083 static void semantic_logical_op(binary_expression_t *expression)
4085 expression_t *left = expression->left;
4086 expression_t *right = expression->right;
4087 type_t *orig_type_left = left->base.datatype;
4088 type_t *orig_type_right = right->base.datatype;
4090 if(orig_type_left == NULL || orig_type_right == NULL)
4093 type_t *type_left = skip_typeref(orig_type_left);
4094 type_t *type_right = skip_typeref(orig_type_right);
4096 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4097 /* TODO: improve error message */
4098 errorf(HERE, "operation needs scalar types");
4102 expression->expression.datatype = type_int;
4105 static bool has_const_fields(type_t *type)
4112 static void semantic_binexpr_assign(binary_expression_t *expression)
4114 expression_t *left = expression->left;
4115 type_t *orig_type_left = left->base.datatype;
4117 if(orig_type_left == NULL)
4120 type_t *type_left = revert_automatic_type_conversion(left);
4121 type_left = skip_typeref(orig_type_left);
4123 /* must be a modifiable lvalue */
4124 if (is_type_array(type_left)) {
4125 errorf(HERE, "cannot assign to arrays ('%E')", left);
4128 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4129 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left, orig_type_left);
4132 if(is_type_incomplete(type_left)) {
4133 errorf(HERE, "left-hand side of assignment '%E' has incomplete type '%T'", left, orig_type_left);
4136 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4137 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields", left, orig_type_left);
4141 semantic_assign(orig_type_left, &expression->right, "assignment");
4143 expression->expression.datatype = orig_type_left;
4146 static void semantic_comma(binary_expression_t *expression)
4148 expression->expression.datatype = expression->right->base.datatype;
4151 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4152 static expression_t *parse_##binexpression_type(unsigned precedence, \
4153 expression_t *left) \
4157 expression_t *right = parse_sub_expression(precedence + lr); \
4159 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4160 binexpr->binary.left = left; \
4161 binexpr->binary.right = right; \
4162 sfunc(&binexpr->binary); \
4167 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4168 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4169 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4170 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4171 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4172 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4173 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4174 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4175 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4177 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4178 semantic_comparison, 1)
4179 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4180 semantic_comparison, 1)
4181 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4182 semantic_comparison, 1)
4183 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4184 semantic_comparison, 1)
4186 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4187 semantic_binexpr_arithmetic, 1)
4188 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4189 semantic_binexpr_arithmetic, 1)
4190 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4191 semantic_binexpr_arithmetic, 1)
4192 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4193 semantic_logical_op, 1)
4194 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4195 semantic_logical_op, 1)
4196 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4197 semantic_shift_op, 1)
4198 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4199 semantic_shift_op, 1)
4200 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4201 semantic_arithmetic_addsubb_assign, 0)
4202 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4203 semantic_arithmetic_addsubb_assign, 0)
4204 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4205 semantic_arithmetic_assign, 0)
4206 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4207 semantic_arithmetic_assign, 0)
4208 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4209 semantic_arithmetic_assign, 0)
4210 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4211 semantic_arithmetic_assign, 0)
4212 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4213 semantic_arithmetic_assign, 0)
4214 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4215 semantic_arithmetic_assign, 0)
4216 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4217 semantic_arithmetic_assign, 0)
4218 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4219 semantic_arithmetic_assign, 0)
4221 static expression_t *parse_sub_expression(unsigned precedence)
4223 if(token.type < 0) {
4224 return expected_expression_error();
4227 expression_parser_function_t *parser
4228 = &expression_parsers[token.type];
4229 source_position_t source_position = token.source_position;
4232 if(parser->parser != NULL) {
4233 left = parser->parser(parser->precedence);
4235 left = parse_primary_expression();
4237 assert(left != NULL);
4238 left->base.source_position = source_position;
4241 if(token.type < 0) {
4242 return expected_expression_error();
4245 parser = &expression_parsers[token.type];
4246 if(parser->infix_parser == NULL)
4248 if(parser->infix_precedence < precedence)
4251 left = parser->infix_parser(parser->infix_precedence, left);
4253 assert(left != NULL);
4254 assert(left->kind != EXPR_UNKNOWN);
4255 left->base.source_position = source_position;
4261 static expression_t *parse_expression(void)
4263 return parse_sub_expression(1);
4268 static void register_expression_parser(parse_expression_function parser,
4269 int token_type, unsigned precedence)
4271 expression_parser_function_t *entry = &expression_parsers[token_type];
4273 if(entry->parser != NULL) {
4274 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4275 panic("trying to register multiple expression parsers for a token");
4277 entry->parser = parser;
4278 entry->precedence = precedence;
4281 static void register_infix_parser(parse_expression_infix_function parser,
4282 int token_type, unsigned precedence)
4284 expression_parser_function_t *entry = &expression_parsers[token_type];
4286 if(entry->infix_parser != NULL) {
4287 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4288 panic("trying to register multiple infix expression parsers for a "
4291 entry->infix_parser = parser;
4292 entry->infix_precedence = precedence;
4295 static void init_expression_parsers(void)
4297 memset(&expression_parsers, 0, sizeof(expression_parsers));
4299 register_infix_parser(parse_array_expression, '[', 30);
4300 register_infix_parser(parse_call_expression, '(', 30);
4301 register_infix_parser(parse_select_expression, '.', 30);
4302 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4303 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4305 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4308 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4309 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4310 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4311 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4312 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4313 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4314 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4315 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4316 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4317 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4318 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4319 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4320 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4321 T_EXCLAMATIONMARKEQUAL, 13);
4322 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4323 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4324 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4325 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4326 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4327 register_infix_parser(parse_conditional_expression, '?', 7);
4328 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4329 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4330 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4331 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4332 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4333 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4334 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4335 T_LESSLESSEQUAL, 2);
4336 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4337 T_GREATERGREATEREQUAL, 2);
4338 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4340 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4342 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4345 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4347 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4348 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4349 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4350 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4351 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4352 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4353 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4355 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4357 register_expression_parser(parse_sizeof, T_sizeof, 25);
4358 register_expression_parser(parse_extension, T___extension__, 25);
4359 register_expression_parser(parse_builtin_classify_type,
4360 T___builtin_classify_type, 25);
4363 static asm_constraint_t *parse_asm_constraints(void)
4365 asm_constraint_t *result = NULL;
4366 asm_constraint_t *last = NULL;
4368 while(token.type == T_STRING_LITERAL || token.type == '[') {
4369 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4370 memset(constraint, 0, sizeof(constraint[0]));
4372 if(token.type == '[') {
4374 if(token.type != T_IDENTIFIER) {
4375 parse_error_expected("while parsing asm constraint",
4379 constraint->symbol = token.v.symbol;
4384 constraint->constraints = parse_string_literals();
4386 constraint->expression = parse_expression();
4390 last->next = constraint;
4392 result = constraint;
4396 if(token.type != ',')
4404 static asm_clobber_t *parse_asm_clobbers(void)
4406 asm_clobber_t *result = NULL;
4407 asm_clobber_t *last = NULL;
4409 while(token.type == T_STRING_LITERAL) {
4410 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4411 clobber->clobber = parse_string_literals();
4414 last->next = clobber;
4420 if(token.type != ',')
4428 static statement_t *parse_asm_statement(void)
4432 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4433 statement->base.source_position = token.source_position;
4435 asm_statement_t *asm_statement = &statement->asms;
4437 if(token.type == T_volatile) {
4439 asm_statement->is_volatile = true;
4443 asm_statement->asm_text = parse_string_literals();
4445 if(token.type != ':')
4449 asm_statement->inputs = parse_asm_constraints();
4450 if(token.type != ':')
4454 asm_statement->outputs = parse_asm_constraints();
4455 if(token.type != ':')
4459 asm_statement->clobbers = parse_asm_clobbers();
4467 static statement_t *parse_case_statement(void)
4471 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4473 statement->base.source_position = token.source_position;
4474 statement->case_label.expression = parse_expression();
4477 statement->case_label.label_statement = parse_statement();
4482 static statement_t *parse_default_statement(void)
4486 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4488 statement->base.source_position = token.source_position;
4491 statement->label.label_statement = parse_statement();
4496 static declaration_t *get_label(symbol_t *symbol)
4498 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4499 assert(current_function != NULL);
4500 /* if we found a label in the same function, then we already created the
4502 if(candidate != NULL
4503 && candidate->parent_context == ¤t_function->context) {
4507 /* otherwise we need to create a new one */
4508 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4509 declaration->namespc = NAMESPACE_LABEL;
4510 declaration->symbol = symbol;
4512 label_push(declaration);
4517 static statement_t *parse_label_statement(void)
4519 assert(token.type == T_IDENTIFIER);
4520 symbol_t *symbol = token.v.symbol;
4523 declaration_t *label = get_label(symbol);
4525 /* if source position is already set then the label is defined twice,
4526 * otherwise it was just mentioned in a goto so far */
4527 if(label->source_position.input_name != NULL) {
4528 errorf(HERE, "duplicate label '%s'\n", symbol->string);
4529 errorf(label->source_position, "previous definition of '%s' was here\n", symbol->string);
4531 label->source_position = token.source_position;
4534 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4536 label_statement->statement.kind = STATEMENT_LABEL;
4537 label_statement->statement.source_position = token.source_position;
4538 label_statement->label = label;
4542 if(token.type == '}') {
4543 /* TODO only warn? */
4544 errorf(HERE, "label at end of compound statement");
4545 return (statement_t*) label_statement;
4547 label_statement->label_statement = parse_statement();
4550 return (statement_t*) label_statement;
4553 static statement_t *parse_if(void)
4557 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4558 statement->statement.kind = STATEMENT_IF;
4559 statement->statement.source_position = token.source_position;
4562 statement->condition = parse_expression();
4565 statement->true_statement = parse_statement();
4566 if(token.type == T_else) {
4568 statement->false_statement = parse_statement();
4571 return (statement_t*) statement;
4574 static statement_t *parse_switch(void)
4578 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4579 statement->statement.kind = STATEMENT_SWITCH;
4580 statement->statement.source_position = token.source_position;
4583 statement->expression = parse_expression();
4585 statement->body = parse_statement();
4587 return (statement_t*) statement;
4590 static statement_t *parse_while(void)
4594 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4595 statement->statement.kind = STATEMENT_WHILE;
4596 statement->statement.source_position = token.source_position;
4599 statement->condition = parse_expression();
4601 statement->body = parse_statement();
4603 return (statement_t*) statement;
4606 static statement_t *parse_do(void)
4610 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4611 statement->statement.kind = STATEMENT_DO_WHILE;
4612 statement->statement.source_position = token.source_position;
4614 statement->body = parse_statement();
4617 statement->condition = parse_expression();
4621 return (statement_t*) statement;
4624 static statement_t *parse_for(void)
4628 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4629 statement->statement.kind = STATEMENT_FOR;
4630 statement->statement.source_position = token.source_position;
4634 int top = environment_top();
4635 context_t *last_context = context;
4636 set_context(&statement->context);
4638 if(token.type != ';') {
4639 if(is_declaration_specifier(&token, false)) {
4640 parse_declaration(record_declaration);
4642 statement->initialisation = parse_expression();
4649 if(token.type != ';') {
4650 statement->condition = parse_expression();
4653 if(token.type != ')') {
4654 statement->step = parse_expression();
4657 statement->body = parse_statement();
4659 assert(context == &statement->context);
4660 set_context(last_context);
4661 environment_pop_to(top);
4663 return (statement_t*) statement;
4666 static statement_t *parse_goto(void)
4670 if(token.type != T_IDENTIFIER) {
4671 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4675 symbol_t *symbol = token.v.symbol;
4678 declaration_t *label = get_label(symbol);
4680 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4682 statement->statement.kind = STATEMENT_GOTO;
4683 statement->statement.source_position = token.source_position;
4685 statement->label = label;
4689 return (statement_t*) statement;
4692 static statement_t *parse_continue(void)
4697 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4698 statement->kind = STATEMENT_CONTINUE;
4699 statement->base.source_position = token.source_position;
4704 static statement_t *parse_break(void)
4709 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4710 statement->kind = STATEMENT_BREAK;
4711 statement->base.source_position = token.source_position;
4716 static statement_t *parse_return(void)
4720 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4722 statement->statement.kind = STATEMENT_RETURN;
4723 statement->statement.source_position = token.source_position;
4725 assert(is_type_function(current_function->type));
4726 function_type_t *function_type = ¤t_function->type->function;
4727 type_t *return_type = function_type->return_type;
4729 expression_t *return_value = NULL;
4730 if(token.type != ';') {
4731 return_value = parse_expression();
4735 if(return_type == NULL)
4736 return (statement_t*) statement;
4737 if(return_value != NULL && return_value->base.datatype == NULL)
4738 return (statement_t*) statement;
4740 return_type = skip_typeref(return_type);
4742 if(return_value != NULL) {
4743 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4745 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4746 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4747 warningf(HERE, "'return' with a value, in function returning void");
4748 return_value = NULL;
4750 if(return_type != NULL) {
4751 semantic_assign(return_type, &return_value, "'return'");
4755 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4756 warningf(HERE, "'return' without value, in function returning non-void");
4759 statement->return_value = return_value;
4761 return (statement_t*) statement;
4764 static statement_t *parse_declaration_statement(void)
4766 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4768 statement->base.source_position = token.source_position;
4770 declaration_t *before = last_declaration;
4771 parse_declaration(record_declaration);
4773 if(before == NULL) {
4774 statement->declaration.declarations_begin = context->declarations;
4776 statement->declaration.declarations_begin = before->next;
4778 statement->declaration.declarations_end = last_declaration;
4783 static statement_t *parse_expression_statement(void)
4785 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4787 statement->base.source_position = token.source_position;
4788 statement->expression.expression = parse_expression();
4795 static statement_t *parse_statement(void)
4797 statement_t *statement = NULL;
4799 /* declaration or statement */
4800 switch(token.type) {
4802 statement = parse_asm_statement();
4806 statement = parse_case_statement();
4810 statement = parse_default_statement();
4814 statement = parse_compound_statement();
4818 statement = parse_if();
4822 statement = parse_switch();
4826 statement = parse_while();
4830 statement = parse_do();
4834 statement = parse_for();
4838 statement = parse_goto();
4842 statement = parse_continue();
4846 statement = parse_break();
4850 statement = parse_return();
4859 if(look_ahead(1)->type == ':') {
4860 statement = parse_label_statement();
4864 if(is_typedef_symbol(token.v.symbol)) {
4865 statement = parse_declaration_statement();
4869 statement = parse_expression_statement();
4872 case T___extension__:
4873 /* this can be a prefix to a declaration or an expression statement */
4874 /* we simply eat it now and parse the rest with tail recursion */
4877 } while(token.type == T___extension__);
4878 statement = parse_statement();
4882 statement = parse_declaration_statement();
4886 statement = parse_expression_statement();
4890 assert(statement == NULL
4891 || statement->base.source_position.input_name != NULL);
4896 static statement_t *parse_compound_statement(void)
4898 compound_statement_t *compound_statement
4899 = allocate_ast_zero(sizeof(compound_statement[0]));
4900 compound_statement->statement.kind = STATEMENT_COMPOUND;
4901 compound_statement->statement.source_position = token.source_position;
4905 int top = environment_top();
4906 context_t *last_context = context;
4907 set_context(&compound_statement->context);
4909 statement_t *last_statement = NULL;
4911 while(token.type != '}' && token.type != T_EOF) {
4912 statement_t *statement = parse_statement();
4913 if(statement == NULL)
4916 if(last_statement != NULL) {
4917 last_statement->base.next = statement;
4919 compound_statement->statements = statement;
4922 while(statement->base.next != NULL)
4923 statement = statement->base.next;
4925 last_statement = statement;
4928 if(token.type != '}') {
4929 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
4933 assert(context == &compound_statement->context);
4934 set_context(last_context);
4935 environment_pop_to(top);
4937 return (statement_t*) compound_statement;
4940 static void initialize_builtins(void)
4942 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
4943 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
4944 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
4945 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
4946 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
4947 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
4948 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
4949 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
4951 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
4952 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
4953 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
4954 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
4957 static translation_unit_t *parse_translation_unit(void)
4959 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4961 assert(global_context == NULL);
4962 global_context = &unit->context;
4964 assert(context == NULL);
4965 set_context(&unit->context);
4967 initialize_builtins();
4969 while(token.type != T_EOF) {
4970 parse_external_declaration();
4973 assert(context == &unit->context);
4975 last_declaration = NULL;
4977 assert(global_context == &unit->context);
4978 global_context = NULL;
4983 translation_unit_t *parse(void)
4985 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4986 label_stack = NEW_ARR_F(stack_entry_t, 0);
4987 found_error = false;
4989 type_set_output(stderr);
4990 ast_set_output(stderr);
4992 lookahead_bufpos = 0;
4993 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4996 translation_unit_t *unit = parse_translation_unit();
4998 DEL_ARR_F(environment_stack);
4999 DEL_ARR_F(label_stack);
5007 void init_parser(void)
5009 init_expression_parsers();
5010 obstack_init(&temp_obst);
5012 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5013 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5016 void exit_parser(void)
5018 obstack_free(&temp_obst, NULL);