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_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
184 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
185 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
186 [EXPR_VA_START] = sizeof(va_start_expression_t),
187 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
188 [EXPR_STATEMENT] = sizeof(statement_expression_t),
190 if(type >= EXPR_UNARY_FIRST && type <= EXPR_UNARY_LAST) {
191 return sizes[EXPR_UNARY_FIRST];
193 if(type >= EXPR_BINARY_FIRST && type <= EXPR_BINARY_LAST) {
194 return sizes[EXPR_BINARY_FIRST];
196 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
197 assert(sizes[type] != 0);
201 static expression_t *allocate_expression_zero(expression_kind_t kind)
203 size_t size = get_expression_struct_size(kind);
204 expression_t *res = allocate_ast_zero(size);
206 res->base.kind = kind;
210 static size_t get_type_struct_size(type_kind_t kind)
212 static const size_t sizes[] = {
213 [TYPE_ATOMIC] = sizeof(atomic_type_t),
214 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
215 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
216 [TYPE_ENUM] = sizeof(enum_type_t),
217 [TYPE_FUNCTION] = sizeof(function_type_t),
218 [TYPE_POINTER] = sizeof(pointer_type_t),
219 [TYPE_ARRAY] = sizeof(array_type_t),
220 [TYPE_BUILTIN] = sizeof(builtin_type_t),
221 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
222 [TYPE_TYPEOF] = sizeof(typeof_type_t),
224 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
225 assert(kind <= TYPE_TYPEOF);
226 assert(sizes[kind] != 0);
230 static type_t *allocate_type_zero(type_kind_t kind)
232 size_t size = get_type_struct_size(kind);
233 type_t *res = obstack_alloc(type_obst, size);
234 memset(res, 0, size);
236 res->base.kind = kind;
240 static size_t get_initializer_size(initializer_kind_t kind)
242 static const size_t sizes[] = {
243 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
244 [INITIALIZER_STRING] = sizeof(initializer_string_t),
245 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
246 [INITIALIZER_LIST] = sizeof(initializer_list_t)
248 assert(kind < sizeof(sizes) / sizeof(*sizes));
249 assert(sizes[kind] != 0);
253 static initializer_t *allocate_initializer(initializer_kind_t kind)
255 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
261 static void free_type(void *type)
263 obstack_free(type_obst, type);
267 * returns the top element of the environment stack
269 static size_t environment_top(void)
271 return ARR_LEN(environment_stack);
274 static size_t label_top(void)
276 return ARR_LEN(label_stack);
281 static inline void next_token(void)
283 token = lookahead_buffer[lookahead_bufpos];
284 lookahead_buffer[lookahead_bufpos] = lexer_token;
287 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
290 print_token(stderr, &token);
291 fprintf(stderr, "\n");
295 static inline const token_t *look_ahead(int num)
297 assert(num > 0 && num <= MAX_LOOKAHEAD);
298 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
299 return &lookahead_buffer[pos];
302 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
304 static void parse_error_expected(const char *message, ...)
306 if(message != NULL) {
307 errorf(HERE, "%s", message);
310 va_start(ap, message);
311 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
315 static void type_error(const char *msg, const source_position_t source_position,
318 errorf(source_position, "%s, but found type '%T'", msg, type);
321 static void type_error_incompatible(const char *msg,
322 const source_position_t source_position, type_t *type1, type_t *type2)
324 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
327 static void eat_block(void)
329 if(token.type == '{')
332 while(token.type != '}') {
333 if(token.type == T_EOF)
335 if(token.type == '{') {
344 static void eat_statement(void)
346 while(token.type != ';') {
347 if(token.type == T_EOF)
349 if(token.type == '}')
351 if(token.type == '{') {
360 static void eat_paren(void)
362 if(token.type == '(')
365 while(token.type != ')') {
366 if(token.type == T_EOF)
368 if(token.type == ')' || token.type == ';' || token.type == '}') {
371 if(token.type == '(') {
375 if(token.type == '{') {
384 #define expect(expected) \
385 if(UNLIKELY(token.type != (expected))) { \
386 parse_error_expected(NULL, (expected), 0); \
392 #define expect_block(expected) \
393 if(UNLIKELY(token.type != (expected))) { \
394 parse_error_expected(NULL, (expected), 0); \
400 #define expect_void(expected) \
401 if(UNLIKELY(token.type != (expected))) { \
402 parse_error_expected(NULL, (expected), 0); \
408 static void set_context(context_t *new_context)
410 context = new_context;
412 last_declaration = new_context->declarations;
413 if(last_declaration != NULL) {
414 while(last_declaration->next != NULL) {
415 last_declaration = last_declaration->next;
421 * called when we find a 2nd declarator for an identifier we already have a
424 static bool is_compatible_declaration(declaration_t *declaration,
425 declaration_t *previous)
427 /* happens for K&R style function parameters */
428 if(previous->type == NULL) {
429 previous->type = declaration->type;
433 type_t *type1 = skip_typeref(declaration->type);
434 type_t *type2 = skip_typeref(previous->type);
436 return types_compatible(type1, type2);
439 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
441 declaration_t *declaration = symbol->declaration;
442 for( ; declaration != NULL; declaration = declaration->symbol_next) {
443 if(declaration->namespc == namespc)
450 static const char *get_namespace_prefix(namespace_t namespc)
453 case NAMESPACE_NORMAL:
455 case NAMESPACE_UNION:
457 case NAMESPACE_STRUCT:
461 case NAMESPACE_LABEL:
464 panic("invalid namespace found");
468 * pushs an environment_entry on the environment stack and links the
469 * corresponding symbol to the new entry
471 static declaration_t *stack_push(stack_entry_t **stack_ptr,
472 declaration_t *declaration,
473 context_t *parent_context)
475 symbol_t *symbol = declaration->symbol;
476 namespace_t namespc = (namespace_t)declaration->namespc;
478 /* a declaration should be only pushed once */
479 declaration->parent_context = parent_context;
481 declaration_t *previous_declaration = get_declaration(symbol, namespc);
482 assert(declaration != previous_declaration);
483 if(previous_declaration != NULL
484 && previous_declaration->parent_context == context) {
485 if(!is_compatible_declaration(declaration, previous_declaration)) {
486 errorf(declaration->source_position, "definition of symbol '%s%s' with type '%T'", get_namespace_prefix(namespc), symbol->string, declaration->type);
487 errorf(previous_declaration->source_position, "is incompatible with previous declaration of type '%T'", previous_declaration->type);
489 unsigned old_storage_class = previous_declaration->storage_class;
490 unsigned new_storage_class = declaration->storage_class;
491 type_t *type = previous_declaration->type;
492 type = skip_typeref(type);
494 if (current_function == NULL) {
495 if (old_storage_class != STORAGE_CLASS_STATIC &&
496 new_storage_class == STORAGE_CLASS_STATIC) {
497 errorf(declaration->source_position, "static declaration of '%s' follows non-static declaration", symbol->string);
498 errorf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
500 if (old_storage_class == STORAGE_CLASS_EXTERN) {
501 if (new_storage_class == STORAGE_CLASS_NONE) {
502 previous_declaration->storage_class = STORAGE_CLASS_NONE;
504 } else if(!is_type_function(type)) {
505 warningf(declaration->source_position, "redundant declaration for '%s'\n", symbol->string);
506 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
510 if (old_storage_class == STORAGE_CLASS_EXTERN &&
511 new_storage_class == STORAGE_CLASS_EXTERN) {
512 warningf(declaration->source_position, "redundant extern declaration for '%s'\n", symbol->string);
513 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
515 if (old_storage_class == new_storage_class) {
516 errorf(declaration->source_position, "redeclaration of '%s'\n", symbol->string);
518 errorf(declaration->source_position, "redeclaration of '%s' with different linkage\n", symbol->string);
520 errorf(previous_declaration->source_position, "previous declaration of '%s' was here", symbol->string);
524 return previous_declaration;
527 /* remember old declaration */
529 entry.symbol = symbol;
530 entry.old_declaration = symbol->declaration;
531 entry.namespc = (unsigned short) namespc;
532 ARR_APP1(stack_entry_t, *stack_ptr, entry);
534 /* replace/add declaration into declaration list of the symbol */
535 if(symbol->declaration == NULL) {
536 symbol->declaration = declaration;
538 declaration_t *iter_last = NULL;
539 declaration_t *iter = symbol->declaration;
540 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
541 /* replace an entry? */
542 if(iter->namespc == namespc) {
543 if(iter_last == NULL) {
544 symbol->declaration = declaration;
546 iter_last->symbol_next = declaration;
548 declaration->symbol_next = iter->symbol_next;
553 assert(iter_last->symbol_next == NULL);
554 iter_last->symbol_next = declaration;
561 static declaration_t *environment_push(declaration_t *declaration)
563 assert(declaration->source_position.input_name != NULL);
564 return stack_push(&environment_stack, declaration, context);
567 static declaration_t *label_push(declaration_t *declaration)
569 return stack_push(&label_stack, declaration, ¤t_function->context);
573 * pops symbols from the environment stack until @p new_top is the top element
575 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
577 stack_entry_t *stack = *stack_ptr;
578 size_t top = ARR_LEN(stack);
581 assert(new_top <= top);
585 for(i = top; i > new_top; --i) {
586 stack_entry_t *entry = &stack[i - 1];
588 declaration_t *old_declaration = entry->old_declaration;
589 symbol_t *symbol = entry->symbol;
590 namespace_t namespc = (namespace_t)entry->namespc;
592 /* replace/remove declaration */
593 declaration_t *declaration = symbol->declaration;
594 assert(declaration != NULL);
595 if(declaration->namespc == namespc) {
596 if(old_declaration == NULL) {
597 symbol->declaration = declaration->symbol_next;
599 symbol->declaration = old_declaration;
602 declaration_t *iter_last = declaration;
603 declaration_t *iter = declaration->symbol_next;
604 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
605 /* replace an entry? */
606 if(iter->namespc == namespc) {
607 assert(iter_last != NULL);
608 iter_last->symbol_next = old_declaration;
609 old_declaration->symbol_next = iter->symbol_next;
613 assert(iter != NULL);
617 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
620 static void environment_pop_to(size_t new_top)
622 stack_pop_to(&environment_stack, new_top);
625 static void label_pop_to(size_t new_top)
627 stack_pop_to(&label_stack, new_top);
631 static int get_rank(const type_t *type)
633 assert(!is_typeref(type));
634 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
635 * and esp. footnote 108). However we can't fold constants (yet), so we
636 * can't decide whether unsigned int is possible, while int always works.
637 * (unsigned int would be preferable when possible... for stuff like
638 * struct { enum { ... } bla : 4; } ) */
639 if(type->kind == TYPE_ENUM)
640 return ATOMIC_TYPE_INT;
642 assert(type->kind == TYPE_ATOMIC);
643 const atomic_type_t *atomic_type = &type->atomic;
644 atomic_type_type_t atype = atomic_type->atype;
648 static type_t *promote_integer(type_t *type)
650 if(get_rank(type) < ATOMIC_TYPE_INT)
656 static expression_t *create_cast_expression(expression_t *expression,
659 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
661 cast->unary.value = expression;
662 cast->base.datatype = dest_type;
667 static bool is_null_pointer_constant(const expression_t *expression)
669 /* skip void* cast */
670 if(expression->kind == EXPR_UNARY_CAST
671 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
672 expression = expression->unary.value;
675 /* TODO: not correct yet, should be any constant integer expression
676 * which evaluates to 0 */
677 if (expression->kind != EXPR_CONST)
680 type_t *const type = skip_typeref(expression->base.datatype);
681 if (!is_type_integer(type))
684 return expression->conste.v.int_value == 0;
687 static expression_t *create_implicit_cast(expression_t *expression,
690 type_t *source_type = expression->base.datatype;
692 if(source_type == NULL)
695 source_type = skip_typeref(source_type);
696 dest_type = skip_typeref(dest_type);
698 if(source_type == dest_type)
701 switch (dest_type->kind) {
703 /* TODO warning for implicitly converting to enum */
705 if (source_type->kind != TYPE_ATOMIC &&
706 source_type->kind != TYPE_ENUM) {
707 panic("casting of non-atomic types not implemented yet");
710 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
711 type_error_incompatible("can't cast types",
712 expression->base.source_position, source_type,
717 return create_cast_expression(expression, dest_type);
720 switch (source_type->kind) {
722 if (is_null_pointer_constant(expression)) {
723 return create_cast_expression(expression, dest_type);
728 if (pointers_compatible(source_type, dest_type)) {
729 return create_cast_expression(expression, dest_type);
734 array_type_t *array_type = &source_type->array;
735 pointer_type_t *pointer_type = &dest_type->pointer;
736 if (types_compatible(array_type->element_type,
737 pointer_type->points_to)) {
738 return create_cast_expression(expression, dest_type);
744 panic("casting of non-atomic types not implemented yet");
747 type_error_incompatible("can't implicitly cast types",
748 expression->base.source_position, source_type, dest_type);
752 panic("casting of non-atomic types not implemented yet");
756 /** Implements the rules from § 6.5.16.1 */
757 static void semantic_assign(type_t *orig_type_left, expression_t **right,
760 type_t *orig_type_right = (*right)->base.datatype;
762 if(orig_type_right == NULL)
765 type_t *const type_left = skip_typeref(orig_type_left);
766 type_t *const type_right = skip_typeref(orig_type_right);
768 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
769 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
770 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
771 && is_type_pointer(type_right))) {
772 *right = create_implicit_cast(*right, type_left);
776 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
777 pointer_type_t *pointer_type_left = &type_left->pointer;
778 pointer_type_t *pointer_type_right = &type_right->pointer;
779 type_t *points_to_left = pointer_type_left->points_to;
780 type_t *points_to_right = pointer_type_right->points_to;
782 points_to_left = skip_typeref(points_to_left);
783 points_to_right = skip_typeref(points_to_right);
785 /* the left type has all qualifiers from the right type */
786 unsigned missing_qualifiers
787 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
788 if(missing_qualifiers != 0) {
789 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
793 points_to_left = get_unqualified_type(points_to_left);
794 points_to_right = get_unqualified_type(points_to_right);
796 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
797 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
798 && !types_compatible(points_to_left, points_to_right)) {
799 goto incompatible_assign_types;
802 *right = create_implicit_cast(*right, type_left);
806 if (is_type_compound(type_left)
807 && types_compatible(type_left, type_right)) {
808 *right = create_implicit_cast(*right, type_left);
812 incompatible_assign_types:
813 /* TODO: improve error message */
814 errorf(HERE, "incompatible types in %s", context);
815 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
818 static expression_t *parse_constant_expression(void)
820 /* start parsing at precedence 7 (conditional expression) */
821 expression_t *result = parse_sub_expression(7);
823 if(!is_constant_expression(result)) {
824 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
830 static expression_t *parse_assignment_expression(void)
832 /* start parsing at precedence 2 (assignment expression) */
833 return parse_sub_expression(2);
836 static type_t *make_global_typedef(const char *name, type_t *type)
838 symbol_t *const symbol = symbol_table_insert(name);
840 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
841 declaration->namespc = NAMESPACE_NORMAL;
842 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
843 declaration->type = type;
844 declaration->symbol = symbol;
845 declaration->source_position = builtin_source_position;
847 record_declaration(declaration);
849 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
850 typedef_type->typedeft.declaration = declaration;
855 static const char *parse_string_literals(void)
857 assert(token.type == T_STRING_LITERAL);
858 const char *result = token.v.string;
862 while(token.type == T_STRING_LITERAL) {
863 result = concat_strings(result, token.v.string);
870 static void parse_attributes(void)
874 case T___attribute__: {
882 errorf(HERE, "EOF while parsing attribute");
901 if(token.type != T_STRING_LITERAL) {
902 parse_error_expected("while parsing assembler attribute",
907 parse_string_literals();
912 goto attributes_finished;
921 static designator_t *parse_designation(void)
923 if(token.type != '[' && token.type != '.')
926 designator_t *result = NULL;
927 designator_t *last = NULL;
930 designator_t *designator;
933 designator = allocate_ast_zero(sizeof(designator[0]));
935 designator->array_access = parse_constant_expression();
939 designator = allocate_ast_zero(sizeof(designator[0]));
941 if(token.type != T_IDENTIFIER) {
942 parse_error_expected("while parsing designator",
946 designator->symbol = token.v.symbol;
954 assert(designator != NULL);
956 last->next = designator;
965 static initializer_t *initializer_from_string(array_type_t *type,
968 /* TODO: check len vs. size of array type */
971 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
972 initializer->string.string = string;
977 static initializer_t *initializer_from_wide_string(array_type_t *const type,
978 wide_string_t *const string)
980 /* TODO: check len vs. size of array type */
983 initializer_t *const initializer =
984 allocate_initializer(INITIALIZER_WIDE_STRING);
985 initializer->wide_string.string = *string;
990 static initializer_t *initializer_from_expression(type_t *type,
991 expression_t *expression)
993 /* TODO check that expression is a constant expression */
995 /* § 6.7.8.14/15 char array may be initialized by string literals */
996 type_t *const expr_type = expression->base.datatype;
997 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
998 array_type_t *const array_type = &type->array;
999 type_t *const element_type = skip_typeref(array_type->element_type);
1001 if (element_type->kind == TYPE_ATOMIC) {
1002 switch (expression->kind) {
1003 case EXPR_STRING_LITERAL:
1004 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1005 return initializer_from_string(array_type,
1006 expression->string.value);
1009 case EXPR_WIDE_STRING_LITERAL: {
1010 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1011 if (get_unqualified_type(element_type) == bare_wchar_type) {
1012 return initializer_from_wide_string(array_type,
1013 &expression->wide_string.value);
1022 type_t *expression_type = skip_typeref(expression->base.datatype);
1023 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1024 semantic_assign(type, &expression, "initializer");
1026 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1027 result->value.value = expression;
1035 static initializer_t *parse_sub_initializer(type_t *type,
1036 expression_t *expression,
1037 type_t *expression_type);
1039 static initializer_t *parse_sub_initializer_elem(type_t *type)
1041 if(token.type == '{') {
1042 return parse_sub_initializer(type, NULL, NULL);
1045 expression_t *expression = parse_assignment_expression();
1046 type_t *expression_type = skip_typeref(expression->base.datatype);
1048 return parse_sub_initializer(type, expression, expression_type);
1051 static bool had_initializer_brace_warning;
1053 static initializer_t *parse_sub_initializer(type_t *type,
1054 expression_t *expression,
1055 type_t *expression_type)
1057 if(is_type_scalar(type)) {
1058 /* there might be extra {} hierarchies */
1059 if(token.type == '{') {
1061 if(!had_initializer_brace_warning) {
1062 warningf(HERE, "braces around scalar initializer");
1063 had_initializer_brace_warning = true;
1065 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1066 if(token.type == ',') {
1068 /* TODO: warn about excessive elements */
1074 if(expression == NULL) {
1075 expression = parse_assignment_expression();
1077 return initializer_from_expression(type, expression);
1080 /* does the expression match the currently looked at object to initialize */
1081 if(expression != NULL) {
1082 initializer_t *result = initializer_from_expression(type, expression);
1087 bool read_paren = false;
1088 if(token.type == '{') {
1093 /* descend into subtype */
1094 initializer_t *result = NULL;
1095 initializer_t **elems;
1096 if(is_type_array(type)) {
1097 array_type_t *array_type = &type->array;
1098 type_t *element_type = array_type->element_type;
1099 element_type = skip_typeref(element_type);
1102 had_initializer_brace_warning = false;
1103 if(expression == NULL) {
1104 sub = parse_sub_initializer_elem(element_type);
1106 sub = parse_sub_initializer(element_type, expression,
1110 /* didn't match the subtypes -> try the parent type */
1112 assert(!read_paren);
1116 elems = NEW_ARR_F(initializer_t*, 0);
1117 ARR_APP1(initializer_t*, elems, sub);
1120 if(token.type == '}')
1123 if(token.type == '}')
1126 sub = parse_sub_initializer_elem(element_type);
1128 /* TODO error, do nicer cleanup */
1129 errorf(HERE, "member initializer didn't match");
1133 ARR_APP1(initializer_t*, elems, sub);
1136 assert(is_type_compound(type));
1137 compound_type_t *compound_type = &type->compound;
1138 context_t *context = &compound_type->declaration->context;
1140 declaration_t *first = context->declarations;
1143 type_t *first_type = first->type;
1144 first_type = skip_typeref(first_type);
1147 had_initializer_brace_warning = false;
1148 if(expression == NULL) {
1149 sub = parse_sub_initializer_elem(first_type);
1151 sub = parse_sub_initializer(first_type, expression,expression_type);
1154 /* didn't match the subtypes -> try our parent type */
1156 assert(!read_paren);
1160 elems = NEW_ARR_F(initializer_t*, 0);
1161 ARR_APP1(initializer_t*, elems, sub);
1163 declaration_t *iter = first->next;
1164 for( ; iter != NULL; iter = iter->next) {
1165 if(iter->symbol == NULL)
1167 if(iter->namespc != NAMESPACE_NORMAL)
1170 if(token.type == '}')
1173 if(token.type == '}')
1176 type_t *iter_type = iter->type;
1177 iter_type = skip_typeref(iter_type);
1179 sub = parse_sub_initializer_elem(iter_type);
1181 /* TODO error, do nicer cleanup */
1182 errorf(HERE, "member initializer didn't match");
1186 ARR_APP1(initializer_t*, elems, sub);
1190 int len = ARR_LEN(elems);
1191 size_t elems_size = sizeof(initializer_t*) * len;
1193 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1195 init->initializer.kind = INITIALIZER_LIST;
1197 memcpy(init->initializers, elems, elems_size);
1200 result = (initializer_t*) init;
1203 if(token.type == ',')
1210 static initializer_t *parse_initializer(type_t *type)
1212 initializer_t *result;
1214 type = skip_typeref(type);
1216 if(token.type != '{') {
1217 expression_t *expression = parse_assignment_expression();
1218 initializer_t *initializer = initializer_from_expression(type, expression);
1219 if(initializer == NULL) {
1220 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1225 if(is_type_scalar(type)) {
1229 expression_t *expression = parse_assignment_expression();
1230 result = initializer_from_expression(type, expression);
1232 if(token.type == ',')
1238 result = parse_sub_initializer(type, NULL, NULL);
1246 static declaration_t *parse_compound_type_specifier(bool is_struct)
1254 symbol_t *symbol = NULL;
1255 declaration_t *declaration = NULL;
1257 if (token.type == T___attribute__) {
1262 if(token.type == T_IDENTIFIER) {
1263 symbol = token.v.symbol;
1267 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1269 declaration = get_declaration(symbol, NAMESPACE_UNION);
1271 } else if(token.type != '{') {
1273 parse_error_expected("while parsing struct type specifier",
1274 T_IDENTIFIER, '{', 0);
1276 parse_error_expected("while parsing union type specifier",
1277 T_IDENTIFIER, '{', 0);
1283 if(declaration == NULL) {
1284 declaration = allocate_ast_zero(sizeof(declaration[0]));
1287 declaration->namespc = NAMESPACE_STRUCT;
1289 declaration->namespc = NAMESPACE_UNION;
1291 declaration->source_position = token.source_position;
1292 declaration->symbol = symbol;
1293 record_declaration(declaration);
1296 if(token.type == '{') {
1297 if(declaration->init.is_defined) {
1298 assert(symbol != NULL);
1299 errorf(HERE, "multiple definition of %s %s", is_struct ? "struct" : "union", symbol->string);
1300 declaration->context.declarations = NULL;
1302 declaration->init.is_defined = true;
1304 int top = environment_top();
1305 context_t *last_context = context;
1306 set_context(&declaration->context);
1308 parse_compound_type_entries();
1311 assert(context == &declaration->context);
1312 set_context(last_context);
1313 environment_pop_to(top);
1319 static void parse_enum_entries(enum_type_t *const enum_type)
1323 if(token.type == '}') {
1325 errorf(HERE, "empty enum not allowed");
1330 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1332 if(token.type != T_IDENTIFIER) {
1333 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1337 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1338 entry->type = (type_t*) enum_type;
1339 entry->symbol = token.v.symbol;
1340 entry->source_position = token.source_position;
1343 if(token.type == '=') {
1345 entry->init.enum_value = parse_constant_expression();
1350 record_declaration(entry);
1352 if(token.type != ',')
1355 } while(token.type != '}');
1360 static type_t *parse_enum_specifier(void)
1364 declaration_t *declaration;
1367 if(token.type == T_IDENTIFIER) {
1368 symbol = token.v.symbol;
1371 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1372 } else if(token.type != '{') {
1373 parse_error_expected("while parsing enum type specifier",
1374 T_IDENTIFIER, '{', 0);
1381 if(declaration == NULL) {
1382 declaration = allocate_ast_zero(sizeof(declaration[0]));
1384 declaration->namespc = NAMESPACE_ENUM;
1385 declaration->source_position = token.source_position;
1386 declaration->symbol = symbol;
1389 type_t *const type = allocate_type_zero(TYPE_ENUM);
1390 type->enumt.declaration = declaration;
1392 if(token.type == '{') {
1393 if(declaration->init.is_defined) {
1394 errorf(HERE, "multiple definitions of enum %s", symbol->string);
1396 record_declaration(declaration);
1397 declaration->init.is_defined = 1;
1399 parse_enum_entries(&type->enumt);
1407 * if a symbol is a typedef to another type, return true
1409 static bool is_typedef_symbol(symbol_t *symbol)
1411 const declaration_t *const declaration =
1412 get_declaration(symbol, NAMESPACE_NORMAL);
1414 declaration != NULL &&
1415 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1418 static type_t *parse_typeof(void)
1426 expression_t *expression = NULL;
1429 switch(token.type) {
1430 case T___extension__:
1431 /* this can be a prefix to a typename or an expression */
1432 /* we simply eat it now. */
1435 } while(token.type == T___extension__);
1439 if(is_typedef_symbol(token.v.symbol)) {
1440 type = parse_typename();
1442 expression = parse_expression();
1443 type = expression->base.datatype;
1448 type = parse_typename();
1452 expression = parse_expression();
1453 type = expression->base.datatype;
1459 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1460 typeof_type->typeoft.expression = expression;
1461 typeof_type->typeoft.typeof_type = type;
1467 SPECIFIER_SIGNED = 1 << 0,
1468 SPECIFIER_UNSIGNED = 1 << 1,
1469 SPECIFIER_LONG = 1 << 2,
1470 SPECIFIER_INT = 1 << 3,
1471 SPECIFIER_DOUBLE = 1 << 4,
1472 SPECIFIER_CHAR = 1 << 5,
1473 SPECIFIER_SHORT = 1 << 6,
1474 SPECIFIER_LONG_LONG = 1 << 7,
1475 SPECIFIER_FLOAT = 1 << 8,
1476 SPECIFIER_BOOL = 1 << 9,
1477 SPECIFIER_VOID = 1 << 10,
1478 #ifdef PROVIDE_COMPLEX
1479 SPECIFIER_COMPLEX = 1 << 11,
1480 SPECIFIER_IMAGINARY = 1 << 12,
1484 static type_t *create_builtin_type(symbol_t *const symbol,
1485 type_t *const real_type)
1487 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1488 type->builtin.symbol = symbol;
1489 type->builtin.real_type = real_type;
1491 type_t *result = typehash_insert(type);
1492 if (type != result) {
1499 static type_t *get_typedef_type(symbol_t *symbol)
1501 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1502 if(declaration == NULL
1503 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1506 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1507 type->typedeft.declaration = declaration;
1512 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1514 type_t *type = NULL;
1515 unsigned type_qualifiers = 0;
1516 unsigned type_specifiers = 0;
1519 specifiers->source_position = token.source_position;
1522 switch(token.type) {
1525 #define MATCH_STORAGE_CLASS(token, class) \
1527 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1528 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1530 specifiers->storage_class = class; \
1534 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1535 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1536 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1537 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1538 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1541 switch (specifiers->storage_class) {
1542 case STORAGE_CLASS_NONE:
1543 specifiers->storage_class = STORAGE_CLASS_THREAD;
1546 case STORAGE_CLASS_EXTERN:
1547 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1550 case STORAGE_CLASS_STATIC:
1551 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1555 errorf(HERE, "multiple storage classes in declaration specifiers");
1561 /* type qualifiers */
1562 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1564 type_qualifiers |= qualifier; \
1568 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1569 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1570 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1572 case T___extension__:
1577 /* type specifiers */
1578 #define MATCH_SPECIFIER(token, specifier, name) \
1581 if(type_specifiers & specifier) { \
1582 errorf(HERE, "multiple " name " type specifiers given"); \
1584 type_specifiers |= specifier; \
1588 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1589 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1590 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1591 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1592 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1593 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1594 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1595 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1596 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1597 #ifdef PROVIDE_COMPLEX
1598 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1599 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1602 /* only in microsoft mode */
1603 specifiers->decl_modifiers |= DM_FORCEINLINE;
1607 specifiers->is_inline = true;
1612 if(type_specifiers & SPECIFIER_LONG_LONG) {
1613 errorf(HERE, "multiple type specifiers given");
1614 } else if(type_specifiers & SPECIFIER_LONG) {
1615 type_specifiers |= SPECIFIER_LONG_LONG;
1617 type_specifiers |= SPECIFIER_LONG;
1621 /* TODO: if type != NULL for the following rules should issue
1624 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1626 type->compound.declaration = parse_compound_type_specifier(true);
1630 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1632 type->compound.declaration = parse_compound_type_specifier(false);
1636 type = parse_enum_specifier();
1639 type = parse_typeof();
1641 case T___builtin_va_list:
1642 type = duplicate_type(type_valist);
1646 case T___attribute__:
1651 case T_IDENTIFIER: {
1652 type_t *typedef_type = get_typedef_type(token.v.symbol);
1654 if(typedef_type == NULL)
1655 goto finish_specifiers;
1658 type = typedef_type;
1662 /* function specifier */
1664 goto finish_specifiers;
1671 atomic_type_type_t atomic_type;
1673 /* match valid basic types */
1674 switch(type_specifiers) {
1675 case SPECIFIER_VOID:
1676 atomic_type = ATOMIC_TYPE_VOID;
1678 case SPECIFIER_CHAR:
1679 atomic_type = ATOMIC_TYPE_CHAR;
1681 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1682 atomic_type = ATOMIC_TYPE_SCHAR;
1684 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1685 atomic_type = ATOMIC_TYPE_UCHAR;
1687 case SPECIFIER_SHORT:
1688 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1689 case SPECIFIER_SHORT | SPECIFIER_INT:
1690 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1691 atomic_type = ATOMIC_TYPE_SHORT;
1693 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1694 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1695 atomic_type = ATOMIC_TYPE_USHORT;
1698 case SPECIFIER_SIGNED:
1699 case SPECIFIER_SIGNED | SPECIFIER_INT:
1700 atomic_type = ATOMIC_TYPE_INT;
1702 case SPECIFIER_UNSIGNED:
1703 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1704 atomic_type = ATOMIC_TYPE_UINT;
1706 case SPECIFIER_LONG:
1707 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1708 case SPECIFIER_LONG | SPECIFIER_INT:
1709 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1710 atomic_type = ATOMIC_TYPE_LONG;
1712 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1713 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1714 atomic_type = ATOMIC_TYPE_ULONG;
1716 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1717 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1718 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1719 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1721 atomic_type = ATOMIC_TYPE_LONGLONG;
1723 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1724 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1726 atomic_type = ATOMIC_TYPE_ULONGLONG;
1728 case SPECIFIER_FLOAT:
1729 atomic_type = ATOMIC_TYPE_FLOAT;
1731 case SPECIFIER_DOUBLE:
1732 atomic_type = ATOMIC_TYPE_DOUBLE;
1734 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1735 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1737 case SPECIFIER_BOOL:
1738 atomic_type = ATOMIC_TYPE_BOOL;
1740 #ifdef PROVIDE_COMPLEX
1741 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1742 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1744 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1745 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1747 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1748 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1750 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1751 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1753 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1754 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1756 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1757 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1761 /* invalid specifier combination, give an error message */
1762 if(type_specifiers == 0) {
1764 warningf(HERE, "no type specifiers in declaration, using int");
1765 atomic_type = ATOMIC_TYPE_INT;
1768 errorf(HERE, "no type specifiers given in declaration");
1770 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1771 (type_specifiers & SPECIFIER_UNSIGNED)) {
1772 errorf(HERE, "signed and unsigned specifiers gives");
1773 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1774 errorf(HERE, "only integer types can be signed or unsigned");
1776 errorf(HERE, "multiple datatypes in declaration");
1778 atomic_type = ATOMIC_TYPE_INVALID;
1781 type = allocate_type_zero(TYPE_ATOMIC);
1782 type->atomic.atype = atomic_type;
1785 if(type_specifiers != 0) {
1786 errorf(HERE, "multiple datatypes in declaration");
1790 type->base.qualifiers = type_qualifiers;
1792 type_t *result = typehash_insert(type);
1793 if(newtype && result != type) {
1797 specifiers->type = result;
1800 static type_qualifiers_t parse_type_qualifiers(void)
1802 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1805 switch(token.type) {
1806 /* type qualifiers */
1807 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1808 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1809 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1812 return type_qualifiers;
1817 static declaration_t *parse_identifier_list(void)
1819 declaration_t *declarations = NULL;
1820 declaration_t *last_declaration = NULL;
1822 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1824 declaration->source_position = token.source_position;
1825 declaration->symbol = token.v.symbol;
1828 if(last_declaration != NULL) {
1829 last_declaration->next = declaration;
1831 declarations = declaration;
1833 last_declaration = declaration;
1835 if(token.type != ',')
1838 } while(token.type == T_IDENTIFIER);
1840 return declarations;
1843 static void semantic_parameter(declaration_t *declaration)
1845 /* TODO: improve error messages */
1847 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1848 errorf(HERE, "typedef not allowed in parameter list");
1849 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1850 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1851 errorf(HERE, "parameter may only have none or register storage class");
1854 type_t *orig_type = declaration->type;
1855 if(orig_type == NULL)
1857 type_t *type = skip_typeref(orig_type);
1859 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1860 * into a pointer. § 6.7.5.3 (7) */
1861 if (is_type_array(type)) {
1862 const array_type_t *arr_type = &type->array;
1863 type_t *element_type = arr_type->element_type;
1865 type = make_pointer_type(element_type, type->base.qualifiers);
1867 declaration->type = type;
1870 if(is_type_incomplete(type)) {
1871 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%s'", orig_type, declaration->symbol->string);
1875 static declaration_t *parse_parameter(void)
1877 declaration_specifiers_t specifiers;
1878 memset(&specifiers, 0, sizeof(specifiers));
1880 parse_declaration_specifiers(&specifiers);
1882 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1884 semantic_parameter(declaration);
1889 static declaration_t *parse_parameters(function_type_t *type)
1891 if(token.type == T_IDENTIFIER) {
1892 symbol_t *symbol = token.v.symbol;
1893 if(!is_typedef_symbol(symbol)) {
1894 type->kr_style_parameters = true;
1895 return parse_identifier_list();
1899 if(token.type == ')') {
1900 type->unspecified_parameters = 1;
1903 if(token.type == T_void && look_ahead(1)->type == ')') {
1908 declaration_t *declarations = NULL;
1909 declaration_t *declaration;
1910 declaration_t *last_declaration = NULL;
1911 function_parameter_t *parameter;
1912 function_parameter_t *last_parameter = NULL;
1915 switch(token.type) {
1919 return declarations;
1922 case T___extension__:
1924 declaration = parse_parameter();
1926 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1927 memset(parameter, 0, sizeof(parameter[0]));
1928 parameter->type = declaration->type;
1930 if(last_parameter != NULL) {
1931 last_declaration->next = declaration;
1932 last_parameter->next = parameter;
1934 type->parameters = parameter;
1935 declarations = declaration;
1937 last_parameter = parameter;
1938 last_declaration = declaration;
1942 return declarations;
1944 if(token.type != ',')
1945 return declarations;
1955 } construct_type_type_t;
1957 typedef struct construct_type_t construct_type_t;
1958 struct construct_type_t {
1959 construct_type_type_t type;
1960 construct_type_t *next;
1963 typedef struct parsed_pointer_t parsed_pointer_t;
1964 struct parsed_pointer_t {
1965 construct_type_t construct_type;
1966 type_qualifiers_t type_qualifiers;
1969 typedef struct construct_function_type_t construct_function_type_t;
1970 struct construct_function_type_t {
1971 construct_type_t construct_type;
1972 type_t *function_type;
1975 typedef struct parsed_array_t parsed_array_t;
1976 struct parsed_array_t {
1977 construct_type_t construct_type;
1978 type_qualifiers_t type_qualifiers;
1984 typedef struct construct_base_type_t construct_base_type_t;
1985 struct construct_base_type_t {
1986 construct_type_t construct_type;
1990 static construct_type_t *parse_pointer_declarator(void)
1994 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1995 memset(pointer, 0, sizeof(pointer[0]));
1996 pointer->construct_type.type = CONSTRUCT_POINTER;
1997 pointer->type_qualifiers = parse_type_qualifiers();
1999 return (construct_type_t*) pointer;
2002 static construct_type_t *parse_array_declarator(void)
2006 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2007 memset(array, 0, sizeof(array[0]));
2008 array->construct_type.type = CONSTRUCT_ARRAY;
2010 if(token.type == T_static) {
2011 array->is_static = true;
2015 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2016 if(type_qualifiers != 0) {
2017 if(token.type == T_static) {
2018 array->is_static = true;
2022 array->type_qualifiers = type_qualifiers;
2024 if(token.type == '*' && look_ahead(1)->type == ']') {
2025 array->is_variable = true;
2027 } else if(token.type != ']') {
2028 array->size = parse_assignment_expression();
2033 return (construct_type_t*) array;
2036 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2040 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2042 declaration_t *parameters = parse_parameters(&type->function);
2043 if(declaration != NULL) {
2044 declaration->context.declarations = parameters;
2047 construct_function_type_t *construct_function_type =
2048 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2049 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2050 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2051 construct_function_type->function_type = type;
2055 return (construct_type_t*) construct_function_type;
2058 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2059 bool may_be_abstract)
2061 /* construct a single linked list of construct_type_t's which describe
2062 * how to construct the final declarator type */
2063 construct_type_t *first = NULL;
2064 construct_type_t *last = NULL;
2067 while(token.type == '*') {
2068 construct_type_t *type = parse_pointer_declarator();
2079 /* TODO: find out if this is correct */
2082 construct_type_t *inner_types = NULL;
2084 switch(token.type) {
2086 if(declaration == NULL) {
2087 errorf(HERE, "no identifier expected in typename");
2089 declaration->symbol = token.v.symbol;
2090 declaration->source_position = token.source_position;
2096 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2102 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2103 /* avoid a loop in the outermost scope, because eat_statement doesn't
2105 if(token.type == '}' && current_function == NULL) {
2113 construct_type_t *p = last;
2116 construct_type_t *type;
2117 switch(token.type) {
2119 type = parse_function_declarator(declaration);
2122 type = parse_array_declarator();
2125 goto declarator_finished;
2128 /* insert in the middle of the list (behind p) */
2130 type->next = p->next;
2141 declarator_finished:
2144 /* append inner_types at the end of the list, we don't to set last anymore
2145 * as it's not needed anymore */
2147 assert(first == NULL);
2148 first = inner_types;
2150 last->next = inner_types;
2156 static type_t *construct_declarator_type(construct_type_t *construct_list,
2159 construct_type_t *iter = construct_list;
2160 for( ; iter != NULL; iter = iter->next) {
2161 switch(iter->type) {
2162 case CONSTRUCT_INVALID:
2163 panic("invalid type construction found");
2164 case CONSTRUCT_FUNCTION: {
2165 construct_function_type_t *construct_function_type
2166 = (construct_function_type_t*) iter;
2168 type_t *function_type = construct_function_type->function_type;
2170 function_type->function.return_type = type;
2172 type = function_type;
2176 case CONSTRUCT_POINTER: {
2177 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2178 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2179 pointer_type->pointer.points_to = type;
2180 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2182 type = pointer_type;
2186 case CONSTRUCT_ARRAY: {
2187 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2188 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2190 array_type->base.qualifiers = parsed_array->type_qualifiers;
2191 array_type->array.element_type = type;
2192 array_type->array.is_static = parsed_array->is_static;
2193 array_type->array.is_variable = parsed_array->is_variable;
2194 array_type->array.size = parsed_array->size;
2201 type_t *hashed_type = typehash_insert(type);
2202 if(hashed_type != type) {
2203 /* the function type was constructed earlier freeing it here will
2204 * destroy other types... */
2205 if(iter->type != CONSTRUCT_FUNCTION) {
2215 static declaration_t *parse_declarator(
2216 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2218 type_t *type = specifiers->type;
2219 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2220 declaration->storage_class = specifiers->storage_class;
2221 declaration->decl_modifiers = specifiers->decl_modifiers;
2222 declaration->is_inline = specifiers->is_inline;
2224 construct_type_t *construct_type
2225 = parse_inner_declarator(declaration, may_be_abstract);
2226 declaration->type = construct_declarator_type(construct_type, type);
2228 if(construct_type != NULL) {
2229 obstack_free(&temp_obst, construct_type);
2235 static type_t *parse_abstract_declarator(type_t *base_type)
2237 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2239 type_t *result = construct_declarator_type(construct_type, base_type);
2240 if(construct_type != NULL) {
2241 obstack_free(&temp_obst, construct_type);
2247 static declaration_t *record_declaration(declaration_t *declaration)
2249 assert(declaration->parent_context == NULL);
2250 assert(context != NULL);
2252 symbol_t *symbol = declaration->symbol;
2253 if(symbol != NULL) {
2254 declaration_t *alias = environment_push(declaration);
2255 if(alias != declaration)
2258 declaration->parent_context = context;
2261 if(last_declaration != NULL) {
2262 last_declaration->next = declaration;
2264 context->declarations = declaration;
2266 last_declaration = declaration;
2271 static void parser_error_multiple_definition(declaration_t *declaration,
2272 const source_position_t source_position)
2274 errorf(source_position, "multiple definition of symbol '%s'", declaration->symbol->string);
2275 errorf(declaration->source_position, "this is the location of the previous definition.");
2278 static bool is_declaration_specifier(const token_t *token,
2279 bool only_type_specifiers)
2281 switch(token->type) {
2285 return is_typedef_symbol(token->v.symbol);
2287 case T___extension__:
2290 return !only_type_specifiers;
2297 static void parse_init_declarator_rest(declaration_t *declaration)
2301 type_t *orig_type = declaration->type;
2302 type_t *type = NULL;
2303 if(orig_type != NULL)
2304 type = skip_typeref(orig_type);
2306 if(declaration->init.initializer != NULL) {
2307 parser_error_multiple_definition(declaration, token.source_position);
2310 initializer_t *initializer = parse_initializer(type);
2312 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2313 * the array type size */
2314 if(type != NULL && is_type_array(type) && initializer != NULL) {
2315 array_type_t *array_type = &type->array;
2317 if(array_type->size == NULL) {
2318 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2320 cnst->base.datatype = type_size_t;
2322 switch (initializer->kind) {
2323 case INITIALIZER_LIST: {
2324 initializer_list_t *const list = &initializer->list;
2325 cnst->conste.v.int_value = list->len;
2329 case INITIALIZER_STRING: {
2330 initializer_string_t *const string = &initializer->string;
2331 cnst->conste.v.int_value = strlen(string->string) + 1;
2335 case INITIALIZER_WIDE_STRING: {
2336 initializer_wide_string_t *const string = &initializer->wide_string;
2337 cnst->conste.v.int_value = string->string.size;
2342 panic("invalid initializer type");
2345 array_type->size = cnst;
2349 if(type != NULL && is_type_function(type)) {
2350 errorf(declaration->source_position, "initializers not allowed for function types at declator '%s' (type '%T')", declaration->symbol->string, orig_type);
2352 declaration->init.initializer = initializer;
2356 /* parse rest of a declaration without any declarator */
2357 static void parse_anonymous_declaration_rest(
2358 const declaration_specifiers_t *specifiers,
2359 parsed_declaration_func finished_declaration)
2363 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2365 declaration->type = specifiers->type;
2366 declaration->storage_class = specifiers->storage_class;
2367 declaration->source_position = specifiers->source_position;
2369 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2370 warningf(declaration->source_position, "useless storage class in empty declaration");
2373 type_t *type = declaration->type;
2374 switch (type->kind) {
2375 case TYPE_COMPOUND_STRUCT:
2376 case TYPE_COMPOUND_UNION: {
2377 const compound_type_t *compound_type = &type->compound;
2378 if (compound_type->declaration->symbol == NULL) {
2379 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2388 warningf(declaration->source_position, "empty declaration");
2392 finished_declaration(declaration);
2395 static void parse_declaration_rest(declaration_t *ndeclaration,
2396 const declaration_specifiers_t *specifiers,
2397 parsed_declaration_func finished_declaration)
2400 declaration_t *declaration = finished_declaration(ndeclaration);
2402 type_t *orig_type = declaration->type;
2403 type_t *type = skip_typeref(orig_type);
2405 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2406 warningf(declaration->source_position, "variable '%s' declared 'inline'\n", declaration->symbol->string);
2409 if(token.type == '=') {
2410 parse_init_declarator_rest(declaration);
2413 if(token.type != ',')
2417 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2422 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2424 /* TODO: check that it was actually a parameter that gets a type */
2426 /* we should have a declaration for the parameter in the current
2428 return record_declaration(declaration);
2431 static void parse_declaration(parsed_declaration_func finished_declaration)
2433 declaration_specifiers_t specifiers;
2434 memset(&specifiers, 0, sizeof(specifiers));
2435 parse_declaration_specifiers(&specifiers);
2437 if(token.type == ';') {
2438 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2440 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2441 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2445 static void parse_kr_declaration_list(declaration_t *declaration)
2447 type_t *type = skip_typeref(declaration->type);
2448 if(!is_type_function(type))
2451 if(!type->function.kr_style_parameters)
2454 /* push function parameters */
2455 int top = environment_top();
2456 context_t *last_context = context;
2457 set_context(&declaration->context);
2459 declaration_t *parameter = declaration->context.declarations;
2460 for( ; parameter != NULL; parameter = parameter->next) {
2461 environment_push(parameter);
2464 /* parse declaration list */
2465 while(is_declaration_specifier(&token, false)) {
2466 parse_declaration(finished_kr_declaration);
2469 /* pop function parameters */
2470 assert(context == &declaration->context);
2471 set_context(last_context);
2472 environment_pop_to(top);
2474 /* update function type */
2475 type_t *new_type = duplicate_type(type);
2476 new_type->function.kr_style_parameters = false;
2478 function_parameter_t *parameters = NULL;
2479 function_parameter_t *last_parameter = NULL;
2481 declaration_t *parameter_declaration = declaration->context.declarations;
2482 for( ; parameter_declaration != NULL;
2483 parameter_declaration = parameter_declaration->next) {
2484 type_t *parameter_type = parameter_declaration->type;
2485 if(parameter_type == NULL) {
2487 errorf(HERE, "no type specified for function parameter '%s'", parameter_declaration->symbol->string);
2489 warningf(HERE, "no type specified for function parameter '%s', using int", parameter_declaration->symbol->string);
2490 parameter_type = type_int;
2491 parameter_declaration->type = parameter_type;
2495 semantic_parameter(parameter_declaration);
2496 parameter_type = parameter_declaration->type;
2498 function_parameter_t *function_parameter
2499 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2500 memset(function_parameter, 0, sizeof(function_parameter[0]));
2502 function_parameter->type = parameter_type;
2503 if(last_parameter != NULL) {
2504 last_parameter->next = function_parameter;
2506 parameters = function_parameter;
2508 last_parameter = function_parameter;
2510 new_type->function.parameters = parameters;
2512 type = typehash_insert(new_type);
2513 if(type != new_type) {
2514 obstack_free(type_obst, new_type);
2517 declaration->type = type;
2520 static void parse_external_declaration(void)
2522 /* function-definitions and declarations both start with declaration
2524 declaration_specifiers_t specifiers;
2525 memset(&specifiers, 0, sizeof(specifiers));
2526 parse_declaration_specifiers(&specifiers);
2528 /* must be a declaration */
2529 if(token.type == ';') {
2530 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2534 /* declarator is common to both function-definitions and declarations */
2535 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2537 /* must be a declaration */
2538 if(token.type == ',' || token.type == '=' || token.type == ';') {
2539 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2543 /* must be a function definition */
2544 parse_kr_declaration_list(ndeclaration);
2546 if(token.type != '{') {
2547 parse_error_expected("while parsing function definition", '{', 0);
2552 type_t *type = ndeclaration->type;
2558 /* note that we don't skip typerefs: the standard doesn't allow them here
2559 * (so we can't use is_type_function here) */
2560 if(type->kind != TYPE_FUNCTION) {
2561 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2566 /* § 6.7.5.3 (14) a function definition with () means no
2567 * parameters (and not unspecified parameters) */
2568 if(type->function.unspecified_parameters) {
2569 type_t *duplicate = duplicate_type(type);
2570 duplicate->function.unspecified_parameters = false;
2572 type = typehash_insert(duplicate);
2573 if(type != duplicate) {
2574 obstack_free(type_obst, duplicate);
2576 ndeclaration->type = type;
2579 declaration_t *declaration = record_declaration(ndeclaration);
2580 if(ndeclaration != declaration) {
2581 memcpy(&declaration->context, &ndeclaration->context,
2582 sizeof(declaration->context));
2584 type = skip_typeref(declaration->type);
2586 /* push function parameters and switch context */
2587 int top = environment_top();
2588 context_t *last_context = context;
2589 set_context(&declaration->context);
2591 declaration_t *parameter = declaration->context.declarations;
2592 for( ; parameter != NULL; parameter = parameter->next) {
2593 environment_push(parameter);
2596 if(declaration->init.statement != NULL) {
2597 parser_error_multiple_definition(declaration, token.source_position);
2599 goto end_of_parse_external_declaration;
2601 /* parse function body */
2602 int label_stack_top = label_top();
2603 declaration_t *old_current_function = current_function;
2604 current_function = declaration;
2606 declaration->init.statement = parse_compound_statement();
2608 assert(current_function == declaration);
2609 current_function = old_current_function;
2610 label_pop_to(label_stack_top);
2613 end_of_parse_external_declaration:
2614 assert(context == &declaration->context);
2615 set_context(last_context);
2616 environment_pop_to(top);
2619 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2622 if(token.type == ':') {
2624 parse_constant_expression();
2625 /* TODO (bitfields) */
2627 declaration_t *declaration = parse_declarator(specifiers, /*may_be_abstract=*/true);
2629 /* TODO: check constraints for struct declarations */
2630 /* TODO: check for doubled fields */
2631 record_declaration(declaration);
2633 if(token.type == ':') {
2635 parse_constant_expression();
2636 /* TODO (bitfields) */
2640 if(token.type != ',')
2647 static void parse_compound_type_entries(void)
2651 while(token.type != '}' && token.type != T_EOF) {
2652 declaration_specifiers_t specifiers;
2653 memset(&specifiers, 0, sizeof(specifiers));
2654 parse_declaration_specifiers(&specifiers);
2656 parse_struct_declarators(&specifiers);
2658 if(token.type == T_EOF) {
2659 errorf(HERE, "EOF while parsing struct");
2664 static type_t *parse_typename(void)
2666 declaration_specifiers_t specifiers;
2667 memset(&specifiers, 0, sizeof(specifiers));
2668 parse_declaration_specifiers(&specifiers);
2669 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2670 /* TODO: improve error message, user does probably not know what a
2671 * storage class is...
2673 errorf(HERE, "typename may not have a storage class");
2676 type_t *result = parse_abstract_declarator(specifiers.type);
2684 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2685 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2686 expression_t *left);
2688 typedef struct expression_parser_function_t expression_parser_function_t;
2689 struct expression_parser_function_t {
2690 unsigned precedence;
2691 parse_expression_function parser;
2692 unsigned infix_precedence;
2693 parse_expression_infix_function infix_parser;
2696 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2698 static expression_t *create_invalid_expression(void)
2700 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2701 expression->base.source_position = token.source_position;
2705 static expression_t *expected_expression_error(void)
2707 errorf(HERE, "expected expression, got token '%K'", &token);
2711 return create_invalid_expression();
2714 static expression_t *parse_string_const(void)
2716 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2717 cnst->base.datatype = type_string;
2718 cnst->string.value = parse_string_literals();
2723 static expression_t *parse_wide_string_const(void)
2725 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2726 cnst->base.datatype = type_wchar_t_ptr;
2727 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2732 static expression_t *parse_int_const(void)
2734 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2735 cnst->base.datatype = token.datatype;
2736 cnst->conste.v.int_value = token.v.intvalue;
2743 static expression_t *parse_float_const(void)
2745 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2746 cnst->base.datatype = token.datatype;
2747 cnst->conste.v.float_value = token.v.floatvalue;
2754 static declaration_t *create_implicit_function(symbol_t *symbol,
2755 const source_position_t source_position)
2757 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2758 ntype->function.return_type = type_int;
2759 ntype->function.unspecified_parameters = true;
2761 type_t *type = typehash_insert(ntype);
2766 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2768 declaration->storage_class = STORAGE_CLASS_EXTERN;
2769 declaration->type = type;
2770 declaration->symbol = symbol;
2771 declaration->source_position = source_position;
2773 /* prepend the implicit definition to the global context
2774 * this is safe since the symbol wasn't declared as anything else yet
2776 assert(symbol->declaration == NULL);
2778 context_t *last_context = context;
2779 context = global_context;
2781 environment_push(declaration);
2782 declaration->next = context->declarations;
2783 context->declarations = declaration;
2785 context = last_context;
2790 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2792 function_parameter_t *parameter
2793 = obstack_alloc(type_obst, sizeof(parameter[0]));
2794 memset(parameter, 0, sizeof(parameter[0]));
2795 parameter->type = argument_type;
2797 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2798 type->function.return_type = return_type;
2799 type->function.parameters = parameter;
2801 type_t *result = typehash_insert(type);
2802 if(result != type) {
2809 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2811 switch(symbol->ID) {
2812 case T___builtin_alloca:
2813 return make_function_1_type(type_void_ptr, type_size_t);
2814 case T___builtin_nan:
2815 return make_function_1_type(type_double, type_string);
2816 case T___builtin_nanf:
2817 return make_function_1_type(type_float, type_string);
2818 case T___builtin_nand:
2819 return make_function_1_type(type_long_double, type_string);
2820 case T___builtin_va_end:
2821 return make_function_1_type(type_void, type_valist);
2823 panic("not implemented builtin symbol found");
2828 * performs automatic type cast as described in § 6.3.2.1
2830 static type_t *automatic_type_conversion(type_t *orig_type)
2832 if(orig_type == NULL)
2835 type_t *type = skip_typeref(orig_type);
2836 if(is_type_array(type)) {
2837 array_type_t *array_type = &type->array;
2838 type_t *element_type = array_type->element_type;
2839 unsigned qualifiers = array_type->type.qualifiers;
2841 return make_pointer_type(element_type, qualifiers);
2844 if(is_type_function(type)) {
2845 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
2852 * reverts the automatic casts of array to pointer types and function
2853 * to function-pointer types as defined § 6.3.2.1
2855 type_t *revert_automatic_type_conversion(const expression_t *expression)
2857 if(expression->base.datatype == NULL)
2860 switch(expression->kind) {
2861 case EXPR_REFERENCE: {
2862 const reference_expression_t *ref = &expression->reference;
2863 return ref->declaration->type;
2866 const select_expression_t *select = &expression->select;
2867 return select->compound_entry->type;
2869 case EXPR_UNARY_DEREFERENCE: {
2870 expression_t *value = expression->unary.value;
2871 type_t *type = skip_typeref(value->base.datatype);
2872 pointer_type_t *pointer_type = &type->pointer;
2874 return pointer_type->points_to;
2876 case EXPR_BUILTIN_SYMBOL: {
2877 const builtin_symbol_expression_t *builtin
2878 = &expression->builtin_symbol;
2879 return get_builtin_symbol_type(builtin->symbol);
2881 case EXPR_ARRAY_ACCESS: {
2882 const array_access_expression_t *array_access
2883 = &expression->array_access;
2884 const expression_t *array_ref = array_access->array_ref;
2885 type_t *type_left = skip_typeref(array_ref->base.datatype);
2886 assert(is_type_pointer(type_left));
2887 pointer_type_t *pointer_type = &type_left->pointer;
2888 return pointer_type->points_to;
2895 return expression->base.datatype;
2898 static expression_t *parse_reference(void)
2900 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2902 reference_expression_t *ref = &expression->reference;
2903 ref->symbol = token.v.symbol;
2905 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2907 source_position_t source_position = token.source_position;
2910 if(declaration == NULL) {
2912 /* an implicitly defined function */
2913 if(token.type == '(') {
2914 warningf(HERE, "implicit declaration of function '%s'\n", ref->symbol->string);
2916 declaration = create_implicit_function(ref->symbol,
2921 errorf(HERE, "unknown symbol '%s' found.\n", ref->symbol->string);
2926 type_t *type = declaration->type;
2927 /* we always do the auto-type conversions; the & and sizeof parser contains
2928 * code to revert this! */
2929 type = automatic_type_conversion(type);
2931 ref->declaration = declaration;
2932 ref->expression.datatype = type;
2937 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2941 /* TODO check if explicit cast is allowed and issue warnings/errors */
2944 static expression_t *parse_cast(void)
2946 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
2948 cast->base.source_position = token.source_position;
2950 type_t *type = parse_typename();
2953 expression_t *value = parse_sub_expression(20);
2955 check_cast_allowed(value, type);
2957 cast->base.datatype = type;
2958 cast->unary.value = value;
2963 static expression_t *parse_statement_expression(void)
2965 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
2967 statement_t *statement = parse_compound_statement();
2968 expression->statement.statement = statement;
2969 if(statement == NULL) {
2974 assert(statement->kind == STATEMENT_COMPOUND);
2975 compound_statement_t *compound_statement = &statement->compound;
2977 /* find last statement and use it's type */
2978 const statement_t *last_statement = NULL;
2979 const statement_t *iter = compound_statement->statements;
2980 for( ; iter != NULL; iter = iter->base.next) {
2981 last_statement = iter;
2984 if(last_statement->kind == STATEMENT_EXPRESSION) {
2985 const expression_statement_t *expression_statement
2986 = &last_statement->expression;
2987 expression->base.datatype
2988 = expression_statement->expression->base.datatype;
2990 expression->base.datatype = type_void;
2998 static expression_t *parse_brace_expression(void)
3002 switch(token.type) {
3004 /* gcc extension: a statement expression */
3005 return parse_statement_expression();
3009 return parse_cast();
3011 if(is_typedef_symbol(token.v.symbol)) {
3012 return parse_cast();
3016 expression_t *result = parse_expression();
3022 static expression_t *parse_function_keyword(void)
3027 if (current_function == NULL) {
3028 errorf(HERE, "'__func__' used outside of a function");
3031 string_literal_expression_t *expression
3032 = allocate_ast_zero(sizeof(expression[0]));
3034 expression->expression.kind = EXPR_FUNCTION;
3035 expression->expression.datatype = type_string;
3036 expression->value = current_function->symbol->string;
3038 return (expression_t*) expression;
3041 static expression_t *parse_pretty_function_keyword(void)
3043 eat(T___PRETTY_FUNCTION__);
3046 if (current_function == NULL) {
3047 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3050 string_literal_expression_t *expression
3051 = allocate_ast_zero(sizeof(expression[0]));
3053 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3054 expression->expression.datatype = type_string;
3055 expression->value = current_function->symbol->string;
3057 return (expression_t*) expression;
3060 static designator_t *parse_designator(void)
3062 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3064 if(token.type != T_IDENTIFIER) {
3065 parse_error_expected("while parsing member designator",
3070 result->symbol = token.v.symbol;
3073 designator_t *last_designator = result;
3075 if(token.type == '.') {
3077 if(token.type != T_IDENTIFIER) {
3078 parse_error_expected("while parsing member designator",
3083 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3084 designator->symbol = token.v.symbol;
3087 last_designator->next = designator;
3088 last_designator = designator;
3091 if(token.type == '[') {
3093 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3094 designator->array_access = parse_expression();
3095 if(designator->array_access == NULL) {
3101 last_designator->next = designator;
3102 last_designator = designator;
3111 static expression_t *parse_offsetof(void)
3113 eat(T___builtin_offsetof);
3115 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3116 expression->base.datatype = type_size_t;
3119 expression->offsetofe.type = parse_typename();
3121 expression->offsetofe.designator = parse_designator();
3127 static expression_t *parse_va_start(void)
3129 eat(T___builtin_va_start);
3131 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3134 expression->va_starte.ap = parse_assignment_expression();
3136 expression_t *const expr = parse_assignment_expression();
3137 if (expr->kind == EXPR_REFERENCE) {
3138 declaration_t *const decl = expr->reference.declaration;
3139 if (decl->parent_context == ¤t_function->context &&
3140 decl->next == NULL) {
3141 expression->va_starte.parameter = decl;
3146 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3148 return create_invalid_expression();
3151 static expression_t *parse_va_arg(void)
3153 eat(T___builtin_va_arg);
3155 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3158 expression->va_arge.ap = parse_assignment_expression();
3160 expression->base.datatype = parse_typename();
3166 static expression_t *parse_builtin_symbol(void)
3168 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3170 symbol_t *symbol = token.v.symbol;
3172 expression->builtin_symbol.symbol = symbol;
3175 type_t *type = get_builtin_symbol_type(symbol);
3176 type = automatic_type_conversion(type);
3178 expression->base.datatype = type;
3182 static expression_t *parse_builtin_constant(void)
3184 eat(T___builtin_constant_p);
3186 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3189 expression->builtin_constant.value = parse_expression();
3191 expression->base.datatype = type_int;
3196 static expression_t *parse_builtin_prefetch(void)
3198 eat(T___builtin_prefetch);
3200 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3203 expression->builtin_prefetch.adr = parse_expression();
3204 if (token.type == ',') {
3206 expression->builtin_prefetch.rw = parse_expression();
3208 if (token.type == ',') {
3210 expression->builtin_prefetch.locality = parse_expression();
3213 expression->base.datatype = type_void;
3218 static expression_t *parse_compare_builtin(void)
3220 expression_t *expression;
3222 switch(token.type) {
3223 case T___builtin_isgreater:
3224 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3226 case T___builtin_isgreaterequal:
3227 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3229 case T___builtin_isless:
3230 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3232 case T___builtin_islessequal:
3233 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3235 case T___builtin_islessgreater:
3236 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3238 case T___builtin_isunordered:
3239 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3242 panic("invalid compare builtin found");
3248 expression->binary.left = parse_assignment_expression();
3250 expression->binary.right = parse_assignment_expression();
3253 type_t *orig_type_left = expression->binary.left->base.datatype;
3254 type_t *orig_type_right = expression->binary.right->base.datatype;
3255 if(orig_type_left == NULL || orig_type_right == NULL)
3258 type_t *type_left = skip_typeref(orig_type_left);
3259 type_t *type_right = skip_typeref(orig_type_right);
3260 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3261 type_error_incompatible("invalid operands in comparison",
3262 token.source_position, type_left, type_right);
3264 semantic_comparison(&expression->binary);
3270 static expression_t *parse_builtin_expect(void)
3272 eat(T___builtin_expect);
3274 expression_t *expression
3275 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3278 expression->binary.left = parse_assignment_expression();
3280 expression->binary.right = parse_constant_expression();
3283 expression->base.datatype = expression->binary.left->base.datatype;
3288 static expression_t *parse_assume(void) {
3291 expression_t *expression
3292 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3295 expression->unary.value = parse_expression();
3298 expression->base.datatype = type_void;
3302 static expression_t *parse_alignof(void) {
3305 expression_t *expression
3306 = allocate_expression_zero(EXPR_ALIGNOF);
3309 expression->alignofe.type = parse_typename();
3312 expression->base.datatype = type_size_t;
3316 static expression_t *parse_primary_expression(void)
3318 switch(token.type) {
3320 return parse_int_const();
3321 case T_FLOATINGPOINT:
3322 return parse_float_const();
3323 case T_STRING_LITERAL: /* TODO merge */
3324 return parse_string_const();
3325 case T_WIDE_STRING_LITERAL:
3326 return parse_wide_string_const();
3328 return parse_reference();
3329 case T___FUNCTION__:
3331 return parse_function_keyword();
3332 case T___PRETTY_FUNCTION__:
3333 return parse_pretty_function_keyword();
3334 case T___builtin_offsetof:
3335 return parse_offsetof();
3336 case T___builtin_va_start:
3337 return parse_va_start();
3338 case T___builtin_va_arg:
3339 return parse_va_arg();
3340 case T___builtin_expect:
3341 return parse_builtin_expect();
3342 case T___builtin_nanf:
3343 case T___builtin_alloca:
3344 case T___builtin_va_end:
3345 return parse_builtin_symbol();
3346 case T___builtin_isgreater:
3347 case T___builtin_isgreaterequal:
3348 case T___builtin_isless:
3349 case T___builtin_islessequal:
3350 case T___builtin_islessgreater:
3351 case T___builtin_isunordered:
3352 return parse_compare_builtin();
3353 case T___builtin_constant_p:
3354 return parse_builtin_constant();
3355 case T___builtin_prefetch:
3356 return parse_builtin_prefetch();
3358 return parse_alignof();
3360 return parse_assume();
3363 return parse_brace_expression();
3366 errorf(HERE, "unexpected token '%K'", &token);
3369 return create_invalid_expression();
3372 static expression_t *parse_array_expression(unsigned precedence,
3379 expression_t *inside = parse_expression();
3381 array_access_expression_t *array_access
3382 = allocate_ast_zero(sizeof(array_access[0]));
3384 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3386 type_t *type_left = left->base.datatype;
3387 type_t *type_inside = inside->base.datatype;
3388 type_t *return_type = NULL;
3390 if(type_left != NULL && type_inside != NULL) {
3391 type_left = skip_typeref(type_left);
3392 type_inside = skip_typeref(type_inside);
3394 if(is_type_pointer(type_left)) {
3395 pointer_type_t *pointer = &type_left->pointer;
3396 return_type = pointer->points_to;
3397 array_access->array_ref = left;
3398 array_access->index = inside;
3399 } else if(is_type_pointer(type_inside)) {
3400 pointer_type_t *pointer = &type_inside->pointer;
3401 return_type = pointer->points_to;
3402 array_access->array_ref = inside;
3403 array_access->index = left;
3404 array_access->flipped = true;
3406 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3409 array_access->array_ref = left;
3410 array_access->index = inside;
3413 if(token.type != ']') {
3414 parse_error_expected("Problem while parsing array access", ']', 0);
3415 return (expression_t*) array_access;
3419 return_type = automatic_type_conversion(return_type);
3420 array_access->expression.datatype = return_type;
3422 return (expression_t*) array_access;
3425 static expression_t *parse_sizeof(unsigned precedence)
3429 sizeof_expression_t *sizeof_expression
3430 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3431 sizeof_expression->expression.kind = EXPR_SIZEOF;
3432 sizeof_expression->expression.datatype = type_size_t;
3434 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3436 sizeof_expression->type = parse_typename();
3439 expression_t *expression = parse_sub_expression(precedence);
3440 expression->base.datatype = revert_automatic_type_conversion(expression);
3442 sizeof_expression->type = expression->base.datatype;
3443 sizeof_expression->size_expression = expression;
3446 return (expression_t*) sizeof_expression;
3449 static expression_t *parse_select_expression(unsigned precedence,
3450 expression_t *compound)
3453 assert(token.type == '.' || token.type == T_MINUSGREATER);
3455 bool is_pointer = (token.type == T_MINUSGREATER);
3458 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3459 select->select.compound = compound;
3461 if(token.type != T_IDENTIFIER) {
3462 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3465 symbol_t *symbol = token.v.symbol;
3466 select->select.symbol = symbol;
3469 type_t *orig_type = compound->base.datatype;
3470 if(orig_type == NULL)
3471 return create_invalid_expression();
3473 type_t *type = skip_typeref(orig_type);
3475 type_t *type_left = type;
3477 if(type->kind != TYPE_POINTER) {
3478 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3479 return create_invalid_expression();
3481 pointer_type_t *pointer_type = &type->pointer;
3482 type_left = pointer_type->points_to;
3484 type_left = skip_typeref(type_left);
3486 if(type_left->kind != TYPE_COMPOUND_STRUCT
3487 && type_left->kind != TYPE_COMPOUND_UNION) {
3488 errorf(HERE, "request for member '%s' in something not a struct or union, but '%T'", symbol->string, type_left);
3489 return create_invalid_expression();
3492 compound_type_t *compound_type = &type_left->compound;
3493 declaration_t *declaration = compound_type->declaration;
3495 if(!declaration->init.is_defined) {
3496 errorf(HERE, "request for member '%s' of incomplete type '%T'", symbol->string, type_left);
3497 return create_invalid_expression();
3500 declaration_t *iter = declaration->context.declarations;
3501 for( ; iter != NULL; iter = iter->next) {
3502 if(iter->symbol == symbol) {
3507 errorf(HERE, "'%T' has no member names '%s'", type_left, symbol->string);
3508 return create_invalid_expression();
3511 /* we always do the auto-type conversions; the & and sizeof parser contains
3512 * code to revert this! */
3513 type_t *expression_type = automatic_type_conversion(iter->type);
3515 select->select.compound_entry = iter;
3516 select->base.datatype = expression_type;
3520 static expression_t *parse_call_expression(unsigned precedence,
3521 expression_t *expression)
3524 expression_t *result = allocate_expression_zero(EXPR_CALL);
3526 call_expression_t *call = &result->call;
3527 call->function = expression;
3529 function_type_t *function_type = NULL;
3530 type_t *orig_type = expression->base.datatype;
3531 if(orig_type != NULL) {
3532 type_t *type = skip_typeref(orig_type);
3534 if(is_type_pointer(type)) {
3535 pointer_type_t *pointer_type = &type->pointer;
3537 type = skip_typeref(pointer_type->points_to);
3539 if (is_type_function(type)) {
3540 function_type = &type->function;
3541 call->expression.datatype = function_type->return_type;
3544 if(function_type == NULL) {
3545 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3547 function_type = NULL;
3548 call->expression.datatype = NULL;
3552 /* parse arguments */
3555 if(token.type != ')') {
3556 call_argument_t *last_argument = NULL;
3559 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3561 argument->expression = parse_assignment_expression();
3562 if(last_argument == NULL) {
3563 call->arguments = argument;
3565 last_argument->next = argument;
3567 last_argument = argument;
3569 if(token.type != ',')
3576 if(function_type != NULL) {
3577 function_parameter_t *parameter = function_type->parameters;
3578 call_argument_t *argument = call->arguments;
3579 for( ; parameter != NULL && argument != NULL;
3580 parameter = parameter->next, argument = argument->next) {
3581 type_t *expected_type = parameter->type;
3582 /* TODO report context in error messages */
3583 argument->expression = create_implicit_cast(argument->expression,
3586 /* too few parameters */
3587 if(parameter != NULL) {
3588 errorf(HERE, "too few arguments to function '%E'", expression);
3589 } else if(argument != NULL) {
3590 /* too many parameters */
3591 if(!function_type->variadic
3592 && !function_type->unspecified_parameters) {
3593 errorf(HERE, "too many arguments to function '%E'", expression);
3595 /* do default promotion */
3596 for( ; argument != NULL; argument = argument->next) {
3597 type_t *type = argument->expression->base.datatype;
3602 type = skip_typeref(type);
3603 if(is_type_integer(type)) {
3604 type = promote_integer(type);
3605 } else if(type == type_float) {
3609 argument->expression
3610 = create_implicit_cast(argument->expression, type);
3613 check_format(&result->call);
3616 check_format(&result->call);
3623 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3625 static bool same_compound_type(const type_t *type1, const type_t *type2)
3627 if(!is_type_compound(type1))
3629 if(type1->kind != type2->kind)
3632 const compound_type_t *compound1 = &type1->compound;
3633 const compound_type_t *compound2 = &type2->compound;
3635 return compound1->declaration == compound2->declaration;
3638 static expression_t *parse_conditional_expression(unsigned precedence,
3639 expression_t *expression)
3643 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3645 conditional_expression_t *conditional = &result->conditional;
3646 conditional->condition = expression;
3649 type_t *condition_type_orig = expression->base.datatype;
3650 if(condition_type_orig != NULL) {
3651 type_t *condition_type = skip_typeref(condition_type_orig);
3652 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3653 type_error("expected a scalar type in conditional condition",
3654 expression->base.source_position, condition_type_orig);
3658 expression_t *true_expression = parse_expression();
3660 expression_t *false_expression = parse_sub_expression(precedence);
3662 conditional->true_expression = true_expression;
3663 conditional->false_expression = false_expression;
3665 type_t *orig_true_type = true_expression->base.datatype;
3666 type_t *orig_false_type = false_expression->base.datatype;
3667 if(orig_true_type == NULL || orig_false_type == NULL)
3670 type_t *true_type = skip_typeref(orig_true_type);
3671 type_t *false_type = skip_typeref(orig_false_type);
3674 type_t *result_type = NULL;
3675 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3676 result_type = semantic_arithmetic(true_type, false_type);
3678 true_expression = create_implicit_cast(true_expression, result_type);
3679 false_expression = create_implicit_cast(false_expression, result_type);
3681 conditional->true_expression = true_expression;
3682 conditional->false_expression = false_expression;
3683 conditional->expression.datatype = result_type;
3684 } else if (same_compound_type(true_type, false_type)
3685 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3686 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3687 /* just take 1 of the 2 types */
3688 result_type = true_type;
3689 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3690 && pointers_compatible(true_type, false_type)) {
3692 result_type = true_type;
3695 type_error_incompatible("while parsing conditional",
3696 expression->base.source_position, true_type,
3700 conditional->expression.datatype = result_type;
3704 static expression_t *parse_extension(unsigned precedence)
3706 eat(T___extension__);
3708 /* TODO enable extensions */
3710 return parse_sub_expression(precedence);
3713 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3715 eat(T___builtin_classify_type);
3717 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3718 result->base.datatype = type_int;
3721 expression_t *expression = parse_sub_expression(precedence);
3723 result->classify_type.type_expression = expression;
3728 static void semantic_incdec(unary_expression_t *expression)
3730 type_t *orig_type = expression->value->base.datatype;
3731 if(orig_type == NULL)
3734 type_t *type = skip_typeref(orig_type);
3735 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3736 /* TODO: improve error message */
3737 errorf(HERE, "operation needs an arithmetic or pointer type");
3741 expression->expression.datatype = orig_type;
3744 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3746 type_t *orig_type = expression->value->base.datatype;
3747 if(orig_type == NULL)
3750 type_t *type = skip_typeref(orig_type);
3751 if(!is_type_arithmetic(type)) {
3752 /* TODO: improve error message */
3753 errorf(HERE, "operation needs an arithmetic type");
3757 expression->expression.datatype = orig_type;
3760 static void semantic_unexpr_scalar(unary_expression_t *expression)
3762 type_t *orig_type = expression->value->base.datatype;
3763 if(orig_type == NULL)
3766 type_t *type = skip_typeref(orig_type);
3767 if (!is_type_scalar(type)) {
3768 errorf(HERE, "operand of ! must be of scalar type");
3772 expression->expression.datatype = orig_type;
3775 static void semantic_unexpr_integer(unary_expression_t *expression)
3777 type_t *orig_type = expression->value->base.datatype;
3778 if(orig_type == NULL)
3781 type_t *type = skip_typeref(orig_type);
3782 if (!is_type_integer(type)) {
3783 errorf(HERE, "operand of ~ must be of integer type");
3787 expression->expression.datatype = orig_type;
3790 static void semantic_dereference(unary_expression_t *expression)
3792 type_t *orig_type = expression->value->base.datatype;
3793 if(orig_type == NULL)
3796 type_t *type = skip_typeref(orig_type);
3797 if(!is_type_pointer(type)) {
3798 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
3802 pointer_type_t *pointer_type = &type->pointer;
3803 type_t *result_type = pointer_type->points_to;
3805 result_type = automatic_type_conversion(result_type);
3806 expression->expression.datatype = result_type;
3809 static void semantic_take_addr(unary_expression_t *expression)
3811 expression_t *value = expression->value;
3812 value->base.datatype = revert_automatic_type_conversion(value);
3814 type_t *orig_type = value->base.datatype;
3815 if(orig_type == NULL)
3818 if(value->kind == EXPR_REFERENCE) {
3819 reference_expression_t *reference = (reference_expression_t*) value;
3820 declaration_t *declaration = reference->declaration;
3821 if(declaration != NULL) {
3822 declaration->address_taken = 1;
3826 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3829 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3830 static expression_t *parse_##unexpression_type(unsigned precedence) \
3834 expression_t *unary_expression \
3835 = allocate_expression_zero(unexpression_type); \
3836 unary_expression->unary.value = parse_sub_expression(precedence); \
3838 sfunc(&unary_expression->unary); \
3840 return unary_expression; \
3843 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
3844 semantic_unexpr_arithmetic)
3845 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
3846 semantic_unexpr_arithmetic)
3847 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
3848 semantic_unexpr_scalar)
3849 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
3850 semantic_dereference)
3851 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
3853 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
3854 semantic_unexpr_integer)
3855 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
3857 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
3860 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3862 static expression_t *parse_##unexpression_type(unsigned precedence, \
3863 expression_t *left) \
3865 (void) precedence; \
3868 expression_t *unary_expression \
3869 = allocate_expression_zero(unexpression_type); \
3870 unary_expression->unary.value = left; \
3872 sfunc(&unary_expression->unary); \
3874 return unary_expression; \
3877 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
3878 EXPR_UNARY_POSTFIX_INCREMENT,
3880 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
3881 EXPR_UNARY_POSTFIX_DECREMENT,
3884 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3886 /* TODO: handle complex + imaginary types */
3888 /* § 6.3.1.8 Usual arithmetic conversions */
3889 if(type_left == type_long_double || type_right == type_long_double) {
3890 return type_long_double;
3891 } else if(type_left == type_double || type_right == type_double) {
3893 } else if(type_left == type_float || type_right == type_float) {
3897 type_right = promote_integer(type_right);
3898 type_left = promote_integer(type_left);
3900 if(type_left == type_right)
3903 bool signed_left = is_type_signed(type_left);
3904 bool signed_right = is_type_signed(type_right);
3905 int rank_left = get_rank(type_left);
3906 int rank_right = get_rank(type_right);
3907 if(rank_left < rank_right) {
3908 if(signed_left == signed_right || !signed_right) {
3914 if(signed_left == signed_right || !signed_left) {
3922 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3924 expression_t *left = expression->left;
3925 expression_t *right = expression->right;
3926 type_t *orig_type_left = left->base.datatype;
3927 type_t *orig_type_right = right->base.datatype;
3929 if(orig_type_left == NULL || orig_type_right == NULL)
3932 type_t *type_left = skip_typeref(orig_type_left);
3933 type_t *type_right = skip_typeref(orig_type_right);
3935 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3936 /* TODO: improve error message */
3937 errorf(HERE, "operation needs arithmetic types");
3941 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3942 expression->left = create_implicit_cast(left, arithmetic_type);
3943 expression->right = create_implicit_cast(right, arithmetic_type);
3944 expression->expression.datatype = arithmetic_type;
3947 static void semantic_shift_op(binary_expression_t *expression)
3949 expression_t *left = expression->left;
3950 expression_t *right = expression->right;
3951 type_t *orig_type_left = left->base.datatype;
3952 type_t *orig_type_right = right->base.datatype;
3954 if(orig_type_left == NULL || orig_type_right == NULL)
3957 type_t *type_left = skip_typeref(orig_type_left);
3958 type_t *type_right = skip_typeref(orig_type_right);
3960 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3961 /* TODO: improve error message */
3962 errorf(HERE, "operation needs integer types");
3966 type_left = promote_integer(type_left);
3967 type_right = promote_integer(type_right);
3969 expression->left = create_implicit_cast(left, type_left);
3970 expression->right = create_implicit_cast(right, type_right);
3971 expression->expression.datatype = type_left;
3974 static void semantic_add(binary_expression_t *expression)
3976 expression_t *left = expression->left;
3977 expression_t *right = expression->right;
3978 type_t *orig_type_left = left->base.datatype;
3979 type_t *orig_type_right = right->base.datatype;
3981 if(orig_type_left == NULL || orig_type_right == NULL)
3984 type_t *type_left = skip_typeref(orig_type_left);
3985 type_t *type_right = skip_typeref(orig_type_right);
3988 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3989 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3990 expression->left = create_implicit_cast(left, arithmetic_type);
3991 expression->right = create_implicit_cast(right, arithmetic_type);
3992 expression->expression.datatype = arithmetic_type;
3994 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3995 expression->expression.datatype = type_left;
3996 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3997 expression->expression.datatype = type_right;
3999 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4003 static void semantic_sub(binary_expression_t *expression)
4005 expression_t *left = expression->left;
4006 expression_t *right = expression->right;
4007 type_t *orig_type_left = left->base.datatype;
4008 type_t *orig_type_right = right->base.datatype;
4010 if(orig_type_left == NULL || orig_type_right == NULL)
4013 type_t *type_left = skip_typeref(orig_type_left);
4014 type_t *type_right = skip_typeref(orig_type_right);
4017 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4018 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4019 expression->left = create_implicit_cast(left, arithmetic_type);
4020 expression->right = create_implicit_cast(right, arithmetic_type);
4021 expression->expression.datatype = arithmetic_type;
4023 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4024 expression->expression.datatype = type_left;
4025 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4026 if(!pointers_compatible(type_left, type_right)) {
4027 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4029 expression->expression.datatype = type_ptrdiff_t;
4032 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4036 static void semantic_comparison(binary_expression_t *expression)
4038 expression_t *left = expression->left;
4039 expression_t *right = expression->right;
4040 type_t *orig_type_left = left->base.datatype;
4041 type_t *orig_type_right = right->base.datatype;
4043 if(orig_type_left == NULL || orig_type_right == NULL)
4046 type_t *type_left = skip_typeref(orig_type_left);
4047 type_t *type_right = skip_typeref(orig_type_right);
4049 /* TODO non-arithmetic types */
4050 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4051 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4052 expression->left = create_implicit_cast(left, arithmetic_type);
4053 expression->right = create_implicit_cast(right, arithmetic_type);
4054 expression->expression.datatype = arithmetic_type;
4055 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4056 /* TODO check compatibility */
4057 } else if (is_type_pointer(type_left)) {
4058 expression->right = create_implicit_cast(right, type_left);
4059 } else if (is_type_pointer(type_right)) {
4060 expression->left = create_implicit_cast(left, type_right);
4062 type_error_incompatible("invalid operands in comparison",
4063 token.source_position, type_left, type_right);
4065 expression->expression.datatype = type_int;
4068 static void semantic_arithmetic_assign(binary_expression_t *expression)
4070 expression_t *left = expression->left;
4071 expression_t *right = expression->right;
4072 type_t *orig_type_left = left->base.datatype;
4073 type_t *orig_type_right = right->base.datatype;
4075 if(orig_type_left == NULL || orig_type_right == NULL)
4078 type_t *type_left = skip_typeref(orig_type_left);
4079 type_t *type_right = skip_typeref(orig_type_right);
4081 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4082 /* TODO: improve error message */
4083 errorf(HERE, "operation needs arithmetic types");
4087 /* combined instructions are tricky. We can't create an implicit cast on
4088 * the left side, because we need the uncasted form for the store.
4089 * The ast2firm pass has to know that left_type must be right_type
4090 * for the arithmeitc operation and create a cast by itself */
4091 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4092 expression->right = create_implicit_cast(right, arithmetic_type);
4093 expression->expression.datatype = type_left;
4096 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4098 expression_t *left = expression->left;
4099 expression_t *right = expression->right;
4100 type_t *orig_type_left = left->base.datatype;
4101 type_t *orig_type_right = right->base.datatype;
4103 if(orig_type_left == NULL || orig_type_right == NULL)
4106 type_t *type_left = skip_typeref(orig_type_left);
4107 type_t *type_right = skip_typeref(orig_type_right);
4109 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4110 /* combined instructions are tricky. We can't create an implicit cast on
4111 * the left side, because we need the uncasted form for the store.
4112 * The ast2firm pass has to know that left_type must be right_type
4113 * for the arithmeitc operation and create a cast by itself */
4114 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4115 expression->right = create_implicit_cast(right, arithmetic_type);
4116 expression->expression.datatype = type_left;
4117 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4118 expression->expression.datatype = type_left;
4120 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4125 static void semantic_logical_op(binary_expression_t *expression)
4127 expression_t *left = expression->left;
4128 expression_t *right = expression->right;
4129 type_t *orig_type_left = left->base.datatype;
4130 type_t *orig_type_right = right->base.datatype;
4132 if(orig_type_left == NULL || orig_type_right == NULL)
4135 type_t *type_left = skip_typeref(orig_type_left);
4136 type_t *type_right = skip_typeref(orig_type_right);
4138 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4139 /* TODO: improve error message */
4140 errorf(HERE, "operation needs scalar types");
4144 expression->expression.datatype = type_int;
4147 static bool has_const_fields(type_t *type)
4154 static void semantic_binexpr_assign(binary_expression_t *expression)
4156 expression_t *left = expression->left;
4157 type_t *orig_type_left = left->base.datatype;
4159 if(orig_type_left == NULL)
4162 type_t *type_left = revert_automatic_type_conversion(left);
4163 type_left = skip_typeref(orig_type_left);
4165 /* must be a modifiable lvalue */
4166 if (is_type_array(type_left)) {
4167 errorf(HERE, "cannot assign to arrays ('%E')", left);
4170 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4171 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left, orig_type_left);
4174 if(is_type_incomplete(type_left)) {
4175 errorf(HERE, "left-hand side of assignment '%E' has incomplete type '%T'", left, orig_type_left);
4178 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4179 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields", left, orig_type_left);
4183 semantic_assign(orig_type_left, &expression->right, "assignment");
4185 expression->expression.datatype = orig_type_left;
4188 static void semantic_comma(binary_expression_t *expression)
4190 expression->expression.datatype = expression->right->base.datatype;
4193 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4194 static expression_t *parse_##binexpression_type(unsigned precedence, \
4195 expression_t *left) \
4199 expression_t *right = parse_sub_expression(precedence + lr); \
4201 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4202 binexpr->binary.left = left; \
4203 binexpr->binary.right = right; \
4204 sfunc(&binexpr->binary); \
4209 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4210 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4211 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4212 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4213 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4214 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4215 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4216 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4217 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4219 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4220 semantic_comparison, 1)
4221 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4222 semantic_comparison, 1)
4223 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4224 semantic_comparison, 1)
4225 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4226 semantic_comparison, 1)
4228 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4229 semantic_binexpr_arithmetic, 1)
4230 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4231 semantic_binexpr_arithmetic, 1)
4232 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4233 semantic_binexpr_arithmetic, 1)
4234 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4235 semantic_logical_op, 1)
4236 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4237 semantic_logical_op, 1)
4238 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4239 semantic_shift_op, 1)
4240 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4241 semantic_shift_op, 1)
4242 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4243 semantic_arithmetic_addsubb_assign, 0)
4244 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4245 semantic_arithmetic_addsubb_assign, 0)
4246 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4247 semantic_arithmetic_assign, 0)
4248 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4249 semantic_arithmetic_assign, 0)
4250 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4251 semantic_arithmetic_assign, 0)
4252 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4253 semantic_arithmetic_assign, 0)
4254 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4255 semantic_arithmetic_assign, 0)
4256 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4257 semantic_arithmetic_assign, 0)
4258 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4259 semantic_arithmetic_assign, 0)
4260 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4261 semantic_arithmetic_assign, 0)
4263 static expression_t *parse_sub_expression(unsigned precedence)
4265 if(token.type < 0) {
4266 return expected_expression_error();
4269 expression_parser_function_t *parser
4270 = &expression_parsers[token.type];
4271 source_position_t source_position = token.source_position;
4274 if(parser->parser != NULL) {
4275 left = parser->parser(parser->precedence);
4277 left = parse_primary_expression();
4279 assert(left != NULL);
4280 left->base.source_position = source_position;
4283 if(token.type < 0) {
4284 return expected_expression_error();
4287 parser = &expression_parsers[token.type];
4288 if(parser->infix_parser == NULL)
4290 if(parser->infix_precedence < precedence)
4293 left = parser->infix_parser(parser->infix_precedence, left);
4295 assert(left != NULL);
4296 assert(left->kind != EXPR_UNKNOWN);
4297 left->base.source_position = source_position;
4303 static expression_t *parse_expression(void)
4305 return parse_sub_expression(1);
4310 static void register_expression_parser(parse_expression_function parser,
4311 int token_type, unsigned precedence)
4313 expression_parser_function_t *entry = &expression_parsers[token_type];
4315 if(entry->parser != NULL) {
4316 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4317 panic("trying to register multiple expression parsers for a token");
4319 entry->parser = parser;
4320 entry->precedence = precedence;
4323 static void register_infix_parser(parse_expression_infix_function parser,
4324 int token_type, unsigned precedence)
4326 expression_parser_function_t *entry = &expression_parsers[token_type];
4328 if(entry->infix_parser != NULL) {
4329 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4330 panic("trying to register multiple infix expression parsers for a "
4333 entry->infix_parser = parser;
4334 entry->infix_precedence = precedence;
4337 static void init_expression_parsers(void)
4339 memset(&expression_parsers, 0, sizeof(expression_parsers));
4341 register_infix_parser(parse_array_expression, '[', 30);
4342 register_infix_parser(parse_call_expression, '(', 30);
4343 register_infix_parser(parse_select_expression, '.', 30);
4344 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4345 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4347 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4350 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4351 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4352 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4353 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4354 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4355 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4356 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4357 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4358 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4359 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4360 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4361 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4362 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4363 T_EXCLAMATIONMARKEQUAL, 13);
4364 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4365 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4366 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4367 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4368 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4369 register_infix_parser(parse_conditional_expression, '?', 7);
4370 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4371 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4372 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4373 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4374 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4375 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4376 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4377 T_LESSLESSEQUAL, 2);
4378 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4379 T_GREATERGREATEREQUAL, 2);
4380 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4382 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4384 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4387 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4389 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4390 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4391 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4392 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4393 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4394 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4395 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4397 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4399 register_expression_parser(parse_sizeof, T_sizeof, 25);
4400 register_expression_parser(parse_extension, T___extension__, 25);
4401 register_expression_parser(parse_builtin_classify_type,
4402 T___builtin_classify_type, 25);
4405 static asm_constraint_t *parse_asm_constraints(void)
4407 asm_constraint_t *result = NULL;
4408 asm_constraint_t *last = NULL;
4410 while(token.type == T_STRING_LITERAL || token.type == '[') {
4411 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4412 memset(constraint, 0, sizeof(constraint[0]));
4414 if(token.type == '[') {
4416 if(token.type != T_IDENTIFIER) {
4417 parse_error_expected("while parsing asm constraint",
4421 constraint->symbol = token.v.symbol;
4426 constraint->constraints = parse_string_literals();
4428 constraint->expression = parse_expression();
4432 last->next = constraint;
4434 result = constraint;
4438 if(token.type != ',')
4446 static asm_clobber_t *parse_asm_clobbers(void)
4448 asm_clobber_t *result = NULL;
4449 asm_clobber_t *last = NULL;
4451 while(token.type == T_STRING_LITERAL) {
4452 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4453 clobber->clobber = parse_string_literals();
4456 last->next = clobber;
4462 if(token.type != ',')
4470 static statement_t *parse_asm_statement(void)
4474 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4475 statement->base.source_position = token.source_position;
4477 asm_statement_t *asm_statement = &statement->asms;
4479 if(token.type == T_volatile) {
4481 asm_statement->is_volatile = true;
4485 asm_statement->asm_text = parse_string_literals();
4487 if(token.type != ':')
4491 asm_statement->inputs = parse_asm_constraints();
4492 if(token.type != ':')
4496 asm_statement->outputs = parse_asm_constraints();
4497 if(token.type != ':')
4501 asm_statement->clobbers = parse_asm_clobbers();
4509 static statement_t *parse_case_statement(void)
4513 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4515 statement->base.source_position = token.source_position;
4516 statement->case_label.expression = parse_expression();
4519 statement->case_label.label_statement = parse_statement();
4524 static statement_t *parse_default_statement(void)
4528 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4530 statement->base.source_position = token.source_position;
4533 statement->label.label_statement = parse_statement();
4538 static declaration_t *get_label(symbol_t *symbol)
4540 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4541 assert(current_function != NULL);
4542 /* if we found a label in the same function, then we already created the
4544 if(candidate != NULL
4545 && candidate->parent_context == ¤t_function->context) {
4549 /* otherwise we need to create a new one */
4550 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4551 declaration->namespc = NAMESPACE_LABEL;
4552 declaration->symbol = symbol;
4554 label_push(declaration);
4559 static statement_t *parse_label_statement(void)
4561 assert(token.type == T_IDENTIFIER);
4562 symbol_t *symbol = token.v.symbol;
4565 declaration_t *label = get_label(symbol);
4567 /* if source position is already set then the label is defined twice,
4568 * otherwise it was just mentioned in a goto so far */
4569 if(label->source_position.input_name != NULL) {
4570 errorf(HERE, "duplicate label '%s'\n", symbol->string);
4571 errorf(label->source_position, "previous definition of '%s' was here\n", symbol->string);
4573 label->source_position = token.source_position;
4576 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4578 label_statement->statement.kind = STATEMENT_LABEL;
4579 label_statement->statement.source_position = token.source_position;
4580 label_statement->label = label;
4584 if(token.type == '}') {
4585 /* TODO only warn? */
4586 errorf(HERE, "label at end of compound statement");
4587 return (statement_t*) label_statement;
4589 label_statement->label_statement = parse_statement();
4592 return (statement_t*) label_statement;
4595 static statement_t *parse_if(void)
4599 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4600 statement->statement.kind = STATEMENT_IF;
4601 statement->statement.source_position = token.source_position;
4604 statement->condition = parse_expression();
4607 statement->true_statement = parse_statement();
4608 if(token.type == T_else) {
4610 statement->false_statement = parse_statement();
4613 return (statement_t*) statement;
4616 static statement_t *parse_switch(void)
4620 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4621 statement->statement.kind = STATEMENT_SWITCH;
4622 statement->statement.source_position = token.source_position;
4625 statement->expression = parse_expression();
4627 statement->body = parse_statement();
4629 return (statement_t*) statement;
4632 static statement_t *parse_while(void)
4636 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4637 statement->statement.kind = STATEMENT_WHILE;
4638 statement->statement.source_position = token.source_position;
4641 statement->condition = parse_expression();
4643 statement->body = parse_statement();
4645 return (statement_t*) statement;
4648 static statement_t *parse_do(void)
4652 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4653 statement->statement.kind = STATEMENT_DO_WHILE;
4654 statement->statement.source_position = token.source_position;
4656 statement->body = parse_statement();
4659 statement->condition = parse_expression();
4663 return (statement_t*) statement;
4666 static statement_t *parse_for(void)
4670 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4671 statement->statement.kind = STATEMENT_FOR;
4672 statement->statement.source_position = token.source_position;
4676 int top = environment_top();
4677 context_t *last_context = context;
4678 set_context(&statement->context);
4680 if(token.type != ';') {
4681 if(is_declaration_specifier(&token, false)) {
4682 parse_declaration(record_declaration);
4684 statement->initialisation = parse_expression();
4691 if(token.type != ';') {
4692 statement->condition = parse_expression();
4695 if(token.type != ')') {
4696 statement->step = parse_expression();
4699 statement->body = parse_statement();
4701 assert(context == &statement->context);
4702 set_context(last_context);
4703 environment_pop_to(top);
4705 return (statement_t*) statement;
4708 static statement_t *parse_goto(void)
4712 if(token.type != T_IDENTIFIER) {
4713 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4717 symbol_t *symbol = token.v.symbol;
4720 declaration_t *label = get_label(symbol);
4722 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4724 statement->statement.kind = STATEMENT_GOTO;
4725 statement->statement.source_position = token.source_position;
4727 statement->label = label;
4731 return (statement_t*) statement;
4734 static statement_t *parse_continue(void)
4739 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4740 statement->kind = STATEMENT_CONTINUE;
4741 statement->base.source_position = token.source_position;
4746 static statement_t *parse_break(void)
4751 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4752 statement->kind = STATEMENT_BREAK;
4753 statement->base.source_position = token.source_position;
4758 static statement_t *parse_return(void)
4762 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4764 statement->statement.kind = STATEMENT_RETURN;
4765 statement->statement.source_position = token.source_position;
4767 assert(is_type_function(current_function->type));
4768 function_type_t *function_type = ¤t_function->type->function;
4769 type_t *return_type = function_type->return_type;
4771 expression_t *return_value = NULL;
4772 if(token.type != ';') {
4773 return_value = parse_expression();
4777 if(return_type == NULL)
4778 return (statement_t*) statement;
4779 if(return_value != NULL && return_value->base.datatype == NULL)
4780 return (statement_t*) statement;
4782 return_type = skip_typeref(return_type);
4784 if(return_value != NULL) {
4785 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4787 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4788 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4789 warningf(HERE, "'return' with a value, in function returning void");
4790 return_value = NULL;
4792 if(return_type != NULL) {
4793 semantic_assign(return_type, &return_value, "'return'");
4797 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4798 warningf(HERE, "'return' without value, in function returning non-void");
4801 statement->return_value = return_value;
4803 return (statement_t*) statement;
4806 static statement_t *parse_declaration_statement(void)
4808 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4810 statement->base.source_position = token.source_position;
4812 declaration_t *before = last_declaration;
4813 parse_declaration(record_declaration);
4815 if(before == NULL) {
4816 statement->declaration.declarations_begin = context->declarations;
4818 statement->declaration.declarations_begin = before->next;
4820 statement->declaration.declarations_end = last_declaration;
4825 static statement_t *parse_expression_statement(void)
4827 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4829 statement->base.source_position = token.source_position;
4830 statement->expression.expression = parse_expression();
4837 static statement_t *parse_statement(void)
4839 statement_t *statement = NULL;
4841 /* declaration or statement */
4842 switch(token.type) {
4844 statement = parse_asm_statement();
4848 statement = parse_case_statement();
4852 statement = parse_default_statement();
4856 statement = parse_compound_statement();
4860 statement = parse_if();
4864 statement = parse_switch();
4868 statement = parse_while();
4872 statement = parse_do();
4876 statement = parse_for();
4880 statement = parse_goto();
4884 statement = parse_continue();
4888 statement = parse_break();
4892 statement = parse_return();
4901 if(look_ahead(1)->type == ':') {
4902 statement = parse_label_statement();
4906 if(is_typedef_symbol(token.v.symbol)) {
4907 statement = parse_declaration_statement();
4911 statement = parse_expression_statement();
4914 case T___extension__:
4915 /* this can be a prefix to a declaration or an expression statement */
4916 /* we simply eat it now and parse the rest with tail recursion */
4919 } while(token.type == T___extension__);
4920 statement = parse_statement();
4924 statement = parse_declaration_statement();
4928 statement = parse_expression_statement();
4932 assert(statement == NULL
4933 || statement->base.source_position.input_name != NULL);
4938 static statement_t *parse_compound_statement(void)
4940 compound_statement_t *compound_statement
4941 = allocate_ast_zero(sizeof(compound_statement[0]));
4942 compound_statement->statement.kind = STATEMENT_COMPOUND;
4943 compound_statement->statement.source_position = token.source_position;
4947 int top = environment_top();
4948 context_t *last_context = context;
4949 set_context(&compound_statement->context);
4951 statement_t *last_statement = NULL;
4953 while(token.type != '}' && token.type != T_EOF) {
4954 statement_t *statement = parse_statement();
4955 if(statement == NULL)
4958 if(last_statement != NULL) {
4959 last_statement->base.next = statement;
4961 compound_statement->statements = statement;
4964 while(statement->base.next != NULL)
4965 statement = statement->base.next;
4967 last_statement = statement;
4970 if(token.type != '}') {
4971 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
4975 assert(context == &compound_statement->context);
4976 set_context(last_context);
4977 environment_pop_to(top);
4979 return (statement_t*) compound_statement;
4982 static void initialize_builtins(void)
4984 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
4985 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
4986 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
4987 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
4988 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
4989 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
4990 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
4991 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
4993 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
4994 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
4995 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
4996 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
4999 static translation_unit_t *parse_translation_unit(void)
5001 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5003 assert(global_context == NULL);
5004 global_context = &unit->context;
5006 assert(context == NULL);
5007 set_context(&unit->context);
5009 initialize_builtins();
5011 while(token.type != T_EOF) {
5012 parse_external_declaration();
5015 assert(context == &unit->context);
5017 last_declaration = NULL;
5019 assert(global_context == &unit->context);
5020 global_context = NULL;
5025 translation_unit_t *parse(void)
5027 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5028 label_stack = NEW_ARR_F(stack_entry_t, 0);
5029 found_error = false;
5031 type_set_output(stderr);
5032 ast_set_output(stderr);
5034 lookahead_bufpos = 0;
5035 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5038 translation_unit_t *unit = parse_translation_unit();
5040 DEL_ARR_F(environment_stack);
5041 DEL_ARR_F(label_stack);
5049 void init_parser(void)
5051 init_expression_parsers();
5052 obstack_init(&temp_obst);
5054 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5055 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5058 void exit_parser(void)
5060 obstack_free(&temp_obst, NULL);