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_OFFSETOF] = sizeof(offsetof_expression_t),
185 [EXPR_VA_START] = sizeof(va_start_expression_t),
186 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
187 [EXPR_STATEMENT] = sizeof(statement_expression_t),
189 if(type >= EXPR_UNARY_FIRST && type <= EXPR_UNARY_LAST) {
190 return sizes[EXPR_UNARY_FIRST];
192 if(type >= EXPR_BINARY_FIRST && type <= EXPR_BINARY_LAST) {
193 return sizes[EXPR_BINARY_FIRST];
195 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
196 assert(sizes[type] != 0);
200 static expression_t *allocate_expression_zero(expression_kind_t kind)
202 size_t size = get_expression_struct_size(kind);
203 expression_t *res = allocate_ast_zero(size);
205 res->base.kind = kind;
209 static size_t get_type_struct_size(type_kind_t kind)
211 static const size_t sizes[] = {
212 [TYPE_ATOMIC] = sizeof(atomic_type_t),
213 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
214 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
215 [TYPE_ENUM] = sizeof(enum_type_t),
216 [TYPE_FUNCTION] = sizeof(function_type_t),
217 [TYPE_POINTER] = sizeof(pointer_type_t),
218 [TYPE_ARRAY] = sizeof(array_type_t),
219 [TYPE_BUILTIN] = sizeof(builtin_type_t),
220 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
221 [TYPE_TYPEOF] = sizeof(typeof_type_t),
223 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
224 assert(kind <= TYPE_TYPEOF);
225 assert(sizes[kind] != 0);
229 static type_t *allocate_type_zero(type_kind_t kind)
231 size_t size = get_type_struct_size(kind);
232 type_t *res = obstack_alloc(type_obst, size);
233 memset(res, 0, size);
235 res->base.kind = kind;
239 static size_t get_initializer_size(initializer_kind_t kind)
241 static const size_t sizes[] = {
242 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
243 [INITIALIZER_STRING] = sizeof(initializer_string_t),
244 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
245 [INITIALIZER_LIST] = sizeof(initializer_list_t)
247 assert(kind < sizeof(sizes) / sizeof(*sizes));
248 assert(sizes[kind] != 0);
252 static initializer_t *allocate_initializer(initializer_kind_t kind)
254 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
260 static void free_type(void *type)
262 obstack_free(type_obst, type);
266 * returns the top element of the environment stack
268 static size_t environment_top(void)
270 return ARR_LEN(environment_stack);
273 static size_t label_top(void)
275 return ARR_LEN(label_stack);
280 static inline void next_token(void)
282 token = lookahead_buffer[lookahead_bufpos];
283 lookahead_buffer[lookahead_bufpos] = lexer_token;
286 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
289 print_token(stderr, &token);
290 fprintf(stderr, "\n");
294 static inline const token_t *look_ahead(int num)
296 assert(num > 0 && num <= MAX_LOOKAHEAD);
297 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
298 return &lookahead_buffer[pos];
301 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
303 static void parse_error_expected(const char *message, ...)
305 if(message != NULL) {
306 errorf(HERE, "%s", message);
309 va_start(ap, message);
310 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
314 static void type_error(const char *msg, const source_position_t source_position,
317 errorf(source_position, "%s, but found type '%T'", msg, type);
320 static void type_error_incompatible(const char *msg,
321 const source_position_t source_position, type_t *type1, type_t *type2)
323 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
326 static void eat_block(void)
328 if(token.type == '{')
331 while(token.type != '}') {
332 if(token.type == T_EOF)
334 if(token.type == '{') {
343 static void eat_statement(void)
345 while(token.type != ';') {
346 if(token.type == T_EOF)
348 if(token.type == '}')
350 if(token.type == '{') {
359 static void eat_paren(void)
361 if(token.type == '(')
364 while(token.type != ')') {
365 if(token.type == T_EOF)
367 if(token.type == ')' || token.type == ';' || token.type == '}') {
370 if(token.type == '(') {
374 if(token.type == '{') {
383 #define expect(expected) \
384 if(UNLIKELY(token.type != (expected))) { \
385 parse_error_expected(NULL, (expected), 0); \
391 #define expect_block(expected) \
392 if(UNLIKELY(token.type != (expected))) { \
393 parse_error_expected(NULL, (expected), 0); \
399 #define expect_void(expected) \
400 if(UNLIKELY(token.type != (expected))) { \
401 parse_error_expected(NULL, (expected), 0); \
407 static void set_context(context_t *new_context)
409 context = new_context;
411 last_declaration = new_context->declarations;
412 if(last_declaration != NULL) {
413 while(last_declaration->next != NULL) {
414 last_declaration = last_declaration->next;
420 * called when we find a 2nd declarator for an identifier we already have a
423 static bool is_compatible_declaration(declaration_t *declaration,
424 declaration_t *previous)
426 /* happens for K&R style function parameters */
427 if(previous->type == NULL) {
428 previous->type = declaration->type;
432 type_t *type1 = skip_typeref(declaration->type);
433 type_t *type2 = skip_typeref(previous->type);
435 return types_compatible(type1, type2);
438 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
440 declaration_t *declaration = symbol->declaration;
441 for( ; declaration != NULL; declaration = declaration->symbol_next) {
442 if(declaration->namespc == namespc)
449 static const char *get_namespace_prefix(namespace_t namespc)
452 case NAMESPACE_NORMAL:
454 case NAMESPACE_UNION:
456 case NAMESPACE_STRUCT:
460 case NAMESPACE_LABEL:
463 panic("invalid namespace found");
467 * pushs an environment_entry on the environment stack and links the
468 * corresponding symbol to the new entry
470 static declaration_t *stack_push(stack_entry_t **stack_ptr,
471 declaration_t *declaration,
472 context_t *parent_context)
474 symbol_t *symbol = declaration->symbol;
475 namespace_t namespc = (namespace_t)declaration->namespc;
477 /* a declaration should be only pushed once */
478 declaration->parent_context = parent_context;
480 declaration_t *previous_declaration = get_declaration(symbol, namespc);
481 assert(declaration != previous_declaration);
482 if(previous_declaration != NULL
483 && previous_declaration->parent_context == context) {
484 if(!is_compatible_declaration(declaration, previous_declaration)) {
485 errorf(declaration->source_position, "definition of symbol '%s%s' with type '%T'", get_namespace_prefix(namespc), symbol->string, declaration->type);
486 errorf(previous_declaration->source_position, "is incompatible with previous declaration of type '%T'", previous_declaration->type);
488 unsigned old_storage_class = previous_declaration->storage_class;
489 unsigned new_storage_class = declaration->storage_class;
490 type_t *type = previous_declaration->type;
491 type = skip_typeref(type);
493 if (current_function == NULL) {
494 if (old_storage_class != STORAGE_CLASS_STATIC &&
495 new_storage_class == STORAGE_CLASS_STATIC) {
496 errorf(declaration->source_position, "static declaration of '%s' follows non-static declaration", symbol->string);
497 errorf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
499 if (old_storage_class == STORAGE_CLASS_EXTERN) {
500 if (new_storage_class == STORAGE_CLASS_NONE) {
501 previous_declaration->storage_class = STORAGE_CLASS_NONE;
503 } else if(!is_type_function(type)) {
504 warningf(declaration->source_position, "redundant declaration for '%s'\n", symbol->string);
505 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
509 if (old_storage_class == STORAGE_CLASS_EXTERN &&
510 new_storage_class == STORAGE_CLASS_EXTERN) {
511 warningf(declaration->source_position, "redundant extern declaration for '%s'\n", symbol->string);
512 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
514 if (old_storage_class == new_storage_class) {
515 errorf(declaration->source_position, "redeclaration of '%s'\n", symbol->string);
517 errorf(declaration->source_position, "redeclaration of '%s' with different linkage\n", symbol->string);
519 errorf(previous_declaration->source_position, "previous declaration of '%s' was here", symbol->string);
523 return previous_declaration;
526 /* remember old declaration */
528 entry.symbol = symbol;
529 entry.old_declaration = symbol->declaration;
530 entry.namespc = (unsigned short) namespc;
531 ARR_APP1(stack_entry_t, *stack_ptr, entry);
533 /* replace/add declaration into declaration list of the symbol */
534 if(symbol->declaration == NULL) {
535 symbol->declaration = declaration;
537 declaration_t *iter_last = NULL;
538 declaration_t *iter = symbol->declaration;
539 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
540 /* replace an entry? */
541 if(iter->namespc == namespc) {
542 if(iter_last == NULL) {
543 symbol->declaration = declaration;
545 iter_last->symbol_next = declaration;
547 declaration->symbol_next = iter->symbol_next;
552 assert(iter_last->symbol_next == NULL);
553 iter_last->symbol_next = declaration;
560 static declaration_t *environment_push(declaration_t *declaration)
562 assert(declaration->source_position.input_name != NULL);
563 return stack_push(&environment_stack, declaration, context);
566 static declaration_t *label_push(declaration_t *declaration)
568 return stack_push(&label_stack, declaration, ¤t_function->context);
572 * pops symbols from the environment stack until @p new_top is the top element
574 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
576 stack_entry_t *stack = *stack_ptr;
577 size_t top = ARR_LEN(stack);
580 assert(new_top <= top);
584 for(i = top; i > new_top; --i) {
585 stack_entry_t *entry = &stack[i - 1];
587 declaration_t *old_declaration = entry->old_declaration;
588 symbol_t *symbol = entry->symbol;
589 namespace_t namespc = (namespace_t)entry->namespc;
591 /* replace/remove declaration */
592 declaration_t *declaration = symbol->declaration;
593 assert(declaration != NULL);
594 if(declaration->namespc == namespc) {
595 if(old_declaration == NULL) {
596 symbol->declaration = declaration->symbol_next;
598 symbol->declaration = old_declaration;
601 declaration_t *iter_last = declaration;
602 declaration_t *iter = declaration->symbol_next;
603 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
604 /* replace an entry? */
605 if(iter->namespc == namespc) {
606 assert(iter_last != NULL);
607 iter_last->symbol_next = old_declaration;
608 old_declaration->symbol_next = iter->symbol_next;
612 assert(iter != NULL);
616 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
619 static void environment_pop_to(size_t new_top)
621 stack_pop_to(&environment_stack, new_top);
624 static void label_pop_to(size_t new_top)
626 stack_pop_to(&label_stack, new_top);
630 static int get_rank(const type_t *type)
632 assert(!is_typeref(type));
633 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
634 * and esp. footnote 108). However we can't fold constants (yet), so we
635 * can't decide whether unsigned int is possible, while int always works.
636 * (unsigned int would be preferable when possible... for stuff like
637 * struct { enum { ... } bla : 4; } ) */
638 if(type->kind == TYPE_ENUM)
639 return ATOMIC_TYPE_INT;
641 assert(type->kind == TYPE_ATOMIC);
642 const atomic_type_t *atomic_type = &type->atomic;
643 atomic_type_type_t atype = atomic_type->atype;
647 static type_t *promote_integer(type_t *type)
649 if(get_rank(type) < ATOMIC_TYPE_INT)
655 static expression_t *create_cast_expression(expression_t *expression,
658 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
660 cast->unary.value = expression;
661 cast->base.datatype = dest_type;
666 static bool is_null_pointer_constant(const expression_t *expression)
668 /* skip void* cast */
669 if(expression->kind == EXPR_UNARY_CAST
670 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
671 expression = expression->unary.value;
674 /* TODO: not correct yet, should be any constant integer expression
675 * which evaluates to 0 */
676 if (expression->kind != EXPR_CONST)
679 type_t *const type = skip_typeref(expression->base.datatype);
680 if (!is_type_integer(type))
683 return expression->conste.v.int_value == 0;
686 static expression_t *create_implicit_cast(expression_t *expression,
689 type_t *source_type = expression->base.datatype;
691 if(source_type == NULL)
694 source_type = skip_typeref(source_type);
695 dest_type = skip_typeref(dest_type);
697 if(source_type == dest_type)
700 switch (dest_type->kind) {
702 /* TODO warning for implicitly converting to enum */
704 if (source_type->kind != TYPE_ATOMIC &&
705 source_type->kind != TYPE_ENUM) {
706 panic("casting of non-atomic types not implemented yet");
709 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
710 type_error_incompatible("can't cast types",
711 expression->base.source_position, source_type,
716 return create_cast_expression(expression, dest_type);
719 switch (source_type->kind) {
721 if (is_null_pointer_constant(expression)) {
722 return create_cast_expression(expression, dest_type);
727 if (pointers_compatible(source_type, dest_type)) {
728 return create_cast_expression(expression, dest_type);
733 array_type_t *array_type = &source_type->array;
734 pointer_type_t *pointer_type = &dest_type->pointer;
735 if (types_compatible(array_type->element_type,
736 pointer_type->points_to)) {
737 return create_cast_expression(expression, dest_type);
743 panic("casting of non-atomic types not implemented yet");
746 type_error_incompatible("can't implicitly cast types",
747 expression->base.source_position, source_type, dest_type);
751 panic("casting of non-atomic types not implemented yet");
755 /** Implements the rules from § 6.5.16.1 */
756 static void semantic_assign(type_t *orig_type_left, expression_t **right,
759 type_t *orig_type_right = (*right)->base.datatype;
761 if(orig_type_right == NULL)
764 type_t *const type_left = skip_typeref(orig_type_left);
765 type_t *const type_right = skip_typeref(orig_type_right);
767 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
768 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
769 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
770 && is_type_pointer(type_right))) {
771 *right = create_implicit_cast(*right, type_left);
775 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
776 pointer_type_t *pointer_type_left = &type_left->pointer;
777 pointer_type_t *pointer_type_right = &type_right->pointer;
778 type_t *points_to_left = pointer_type_left->points_to;
779 type_t *points_to_right = pointer_type_right->points_to;
781 points_to_left = skip_typeref(points_to_left);
782 points_to_right = skip_typeref(points_to_right);
784 /* the left type has all qualifiers from the right type */
785 unsigned missing_qualifiers
786 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
787 if(missing_qualifiers != 0) {
788 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
792 points_to_left = get_unqualified_type(points_to_left);
793 points_to_right = get_unqualified_type(points_to_right);
795 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
796 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
797 && !types_compatible(points_to_left, points_to_right)) {
798 goto incompatible_assign_types;
801 *right = create_implicit_cast(*right, type_left);
805 if (is_type_compound(type_left)
806 && types_compatible(type_left, type_right)) {
807 *right = create_implicit_cast(*right, type_left);
811 incompatible_assign_types:
812 /* TODO: improve error message */
813 errorf(HERE, "incompatible types in %s", context);
814 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
817 static expression_t *parse_constant_expression(void)
819 /* start parsing at precedence 7 (conditional expression) */
820 expression_t *result = parse_sub_expression(7);
822 if(!is_constant_expression(result)) {
823 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
829 static expression_t *parse_assignment_expression(void)
831 /* start parsing at precedence 2 (assignment expression) */
832 return parse_sub_expression(2);
835 static type_t *make_global_typedef(const char *name, type_t *type)
837 symbol_t *const symbol = symbol_table_insert(name);
839 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
840 declaration->namespc = NAMESPACE_NORMAL;
841 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
842 declaration->type = type;
843 declaration->symbol = symbol;
844 declaration->source_position = builtin_source_position;
846 record_declaration(declaration);
848 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
849 typedef_type->typedeft.declaration = declaration;
854 static const char *parse_string_literals(void)
856 assert(token.type == T_STRING_LITERAL);
857 const char *result = token.v.string;
861 while(token.type == T_STRING_LITERAL) {
862 result = concat_strings(result, token.v.string);
869 static void parse_attributes(void)
873 case T___attribute__: {
881 errorf(HERE, "EOF while parsing attribute");
900 if(token.type != T_STRING_LITERAL) {
901 parse_error_expected("while parsing assembler attribute",
906 parse_string_literals();
911 goto attributes_finished;
920 static designator_t *parse_designation(void)
922 if(token.type != '[' && token.type != '.')
925 designator_t *result = NULL;
926 designator_t *last = NULL;
929 designator_t *designator;
932 designator = allocate_ast_zero(sizeof(designator[0]));
934 designator->array_access = parse_constant_expression();
938 designator = allocate_ast_zero(sizeof(designator[0]));
940 if(token.type != T_IDENTIFIER) {
941 parse_error_expected("while parsing designator",
945 designator->symbol = token.v.symbol;
953 assert(designator != NULL);
955 last->next = designator;
964 static initializer_t *initializer_from_string(array_type_t *type,
967 /* TODO: check len vs. size of array type */
970 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
971 initializer->string.string = string;
976 static initializer_t *initializer_from_wide_string(array_type_t *const type,
977 wide_string_t *const string)
979 /* TODO: check len vs. size of array type */
982 initializer_t *const initializer =
983 allocate_initializer(INITIALIZER_WIDE_STRING);
984 initializer->wide_string.string = *string;
989 static initializer_t *initializer_from_expression(type_t *type,
990 expression_t *expression)
992 /* TODO check that expression is a constant expression */
994 /* § 6.7.8.14/15 char array may be initialized by string literals */
995 type_t *const expr_type = expression->base.datatype;
996 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
997 array_type_t *const array_type = &type->array;
998 type_t *const element_type = skip_typeref(array_type->element_type);
1000 if (element_type->kind == TYPE_ATOMIC) {
1001 switch (expression->kind) {
1002 case EXPR_STRING_LITERAL:
1003 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1004 return initializer_from_string(array_type,
1005 expression->string.value);
1008 case EXPR_WIDE_STRING_LITERAL: {
1009 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1010 if (get_unqualified_type(element_type) == bare_wchar_type) {
1011 return initializer_from_wide_string(array_type,
1012 &expression->wide_string.value);
1021 type_t *expression_type = skip_typeref(expression->base.datatype);
1022 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1023 semantic_assign(type, &expression, "initializer");
1025 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1026 result->value.value = expression;
1034 static initializer_t *parse_sub_initializer(type_t *type,
1035 expression_t *expression,
1036 type_t *expression_type);
1038 static initializer_t *parse_sub_initializer_elem(type_t *type)
1040 if(token.type == '{') {
1041 return parse_sub_initializer(type, NULL, NULL);
1044 expression_t *expression = parse_assignment_expression();
1045 type_t *expression_type = skip_typeref(expression->base.datatype);
1047 return parse_sub_initializer(type, expression, expression_type);
1050 static bool had_initializer_brace_warning;
1052 static initializer_t *parse_sub_initializer(type_t *type,
1053 expression_t *expression,
1054 type_t *expression_type)
1056 if(is_type_scalar(type)) {
1057 /* there might be extra {} hierarchies */
1058 if(token.type == '{') {
1060 if(!had_initializer_brace_warning) {
1061 warningf(HERE, "braces around scalar initializer");
1062 had_initializer_brace_warning = true;
1064 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1065 if(token.type == ',') {
1067 /* TODO: warn about excessive elements */
1073 if(expression == NULL) {
1074 expression = parse_assignment_expression();
1076 return initializer_from_expression(type, expression);
1079 /* does the expression match the currently looked at object to initialize */
1080 if(expression != NULL) {
1081 initializer_t *result = initializer_from_expression(type, expression);
1086 bool read_paren = false;
1087 if(token.type == '{') {
1092 /* descend into subtype */
1093 initializer_t *result = NULL;
1094 initializer_t **elems;
1095 if(is_type_array(type)) {
1096 array_type_t *array_type = &type->array;
1097 type_t *element_type = array_type->element_type;
1098 element_type = skip_typeref(element_type);
1101 had_initializer_brace_warning = false;
1102 if(expression == NULL) {
1103 sub = parse_sub_initializer_elem(element_type);
1105 sub = parse_sub_initializer(element_type, expression,
1109 /* didn't match the subtypes -> try the parent type */
1111 assert(!read_paren);
1115 elems = NEW_ARR_F(initializer_t*, 0);
1116 ARR_APP1(initializer_t*, elems, sub);
1119 if(token.type == '}')
1122 if(token.type == '}')
1125 sub = parse_sub_initializer_elem(element_type);
1127 /* TODO error, do nicer cleanup */
1128 errorf(HERE, "member initializer didn't match");
1132 ARR_APP1(initializer_t*, elems, sub);
1135 assert(is_type_compound(type));
1136 compound_type_t *compound_type = &type->compound;
1137 context_t *context = &compound_type->declaration->context;
1139 declaration_t *first = context->declarations;
1142 type_t *first_type = first->type;
1143 first_type = skip_typeref(first_type);
1146 had_initializer_brace_warning = false;
1147 if(expression == NULL) {
1148 sub = parse_sub_initializer_elem(first_type);
1150 sub = parse_sub_initializer(first_type, expression,expression_type);
1153 /* didn't match the subtypes -> try our parent type */
1155 assert(!read_paren);
1159 elems = NEW_ARR_F(initializer_t*, 0);
1160 ARR_APP1(initializer_t*, elems, sub);
1162 declaration_t *iter = first->next;
1163 for( ; iter != NULL; iter = iter->next) {
1164 if(iter->symbol == NULL)
1166 if(iter->namespc != NAMESPACE_NORMAL)
1169 if(token.type == '}')
1172 if(token.type == '}')
1175 type_t *iter_type = iter->type;
1176 iter_type = skip_typeref(iter_type);
1178 sub = parse_sub_initializer_elem(iter_type);
1180 /* TODO error, do nicer cleanup */
1181 errorf(HERE, "member initializer didn't match");
1185 ARR_APP1(initializer_t*, elems, sub);
1189 int len = ARR_LEN(elems);
1190 size_t elems_size = sizeof(initializer_t*) * len;
1192 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1194 init->initializer.kind = INITIALIZER_LIST;
1196 memcpy(init->initializers, elems, elems_size);
1199 result = (initializer_t*) init;
1202 if(token.type == ',')
1209 static initializer_t *parse_initializer(type_t *type)
1211 initializer_t *result;
1213 type = skip_typeref(type);
1215 if(token.type != '{') {
1216 expression_t *expression = parse_assignment_expression();
1217 initializer_t *initializer = initializer_from_expression(type, expression);
1218 if(initializer == NULL) {
1219 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1224 if(is_type_scalar(type)) {
1228 expression_t *expression = parse_assignment_expression();
1229 result = initializer_from_expression(type, expression);
1231 if(token.type == ',')
1237 result = parse_sub_initializer(type, NULL, NULL);
1245 static declaration_t *parse_compound_type_specifier(bool is_struct)
1253 symbol_t *symbol = NULL;
1254 declaration_t *declaration = NULL;
1256 if (token.type == T___attribute__) {
1261 if(token.type == T_IDENTIFIER) {
1262 symbol = token.v.symbol;
1266 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1268 declaration = get_declaration(symbol, NAMESPACE_UNION);
1270 } else if(token.type != '{') {
1272 parse_error_expected("while parsing struct type specifier",
1273 T_IDENTIFIER, '{', 0);
1275 parse_error_expected("while parsing union type specifier",
1276 T_IDENTIFIER, '{', 0);
1282 if(declaration == NULL) {
1283 declaration = allocate_ast_zero(sizeof(declaration[0]));
1286 declaration->namespc = NAMESPACE_STRUCT;
1288 declaration->namespc = NAMESPACE_UNION;
1290 declaration->source_position = token.source_position;
1291 declaration->symbol = symbol;
1292 record_declaration(declaration);
1295 if(token.type == '{') {
1296 if(declaration->init.is_defined) {
1297 assert(symbol != NULL);
1298 errorf(HERE, "multiple definition of %s %s", is_struct ? "struct" : "union", symbol->string);
1299 declaration->context.declarations = NULL;
1301 declaration->init.is_defined = true;
1303 int top = environment_top();
1304 context_t *last_context = context;
1305 set_context(&declaration->context);
1307 parse_compound_type_entries();
1310 assert(context == &declaration->context);
1311 set_context(last_context);
1312 environment_pop_to(top);
1318 static void parse_enum_entries(enum_type_t *const enum_type)
1322 if(token.type == '}') {
1324 errorf(HERE, "empty enum not allowed");
1329 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1331 if(token.type != T_IDENTIFIER) {
1332 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1336 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1337 entry->type = (type_t*) enum_type;
1338 entry->symbol = token.v.symbol;
1339 entry->source_position = token.source_position;
1342 if(token.type == '=') {
1344 entry->init.enum_value = parse_constant_expression();
1349 record_declaration(entry);
1351 if(token.type != ',')
1354 } while(token.type != '}');
1359 static type_t *parse_enum_specifier(void)
1363 declaration_t *declaration;
1366 if(token.type == T_IDENTIFIER) {
1367 symbol = token.v.symbol;
1370 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1371 } else if(token.type != '{') {
1372 parse_error_expected("while parsing enum type specifier",
1373 T_IDENTIFIER, '{', 0);
1380 if(declaration == NULL) {
1381 declaration = allocate_ast_zero(sizeof(declaration[0]));
1383 declaration->namespc = NAMESPACE_ENUM;
1384 declaration->source_position = token.source_position;
1385 declaration->symbol = symbol;
1388 type_t *const type = allocate_type_zero(TYPE_ENUM);
1389 type->enumt.declaration = declaration;
1391 if(token.type == '{') {
1392 if(declaration->init.is_defined) {
1393 errorf(HERE, "multiple definitions of enum %s", symbol->string);
1395 record_declaration(declaration);
1396 declaration->init.is_defined = 1;
1398 parse_enum_entries(&type->enumt);
1406 * if a symbol is a typedef to another type, return true
1408 static bool is_typedef_symbol(symbol_t *symbol)
1410 const declaration_t *const declaration =
1411 get_declaration(symbol, NAMESPACE_NORMAL);
1413 declaration != NULL &&
1414 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1417 static type_t *parse_typeof(void)
1425 expression_t *expression = NULL;
1428 switch(token.type) {
1429 case T___extension__:
1430 /* this can be a prefix to a typename or an expression */
1431 /* we simply eat it now. */
1434 } while(token.type == T___extension__);
1438 if(is_typedef_symbol(token.v.symbol)) {
1439 type = parse_typename();
1441 expression = parse_expression();
1442 type = expression->base.datatype;
1447 type = parse_typename();
1451 expression = parse_expression();
1452 type = expression->base.datatype;
1458 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1459 typeof_type->typeoft.expression = expression;
1460 typeof_type->typeoft.typeof_type = type;
1466 SPECIFIER_SIGNED = 1 << 0,
1467 SPECIFIER_UNSIGNED = 1 << 1,
1468 SPECIFIER_LONG = 1 << 2,
1469 SPECIFIER_INT = 1 << 3,
1470 SPECIFIER_DOUBLE = 1 << 4,
1471 SPECIFIER_CHAR = 1 << 5,
1472 SPECIFIER_SHORT = 1 << 6,
1473 SPECIFIER_LONG_LONG = 1 << 7,
1474 SPECIFIER_FLOAT = 1 << 8,
1475 SPECIFIER_BOOL = 1 << 9,
1476 SPECIFIER_VOID = 1 << 10,
1477 #ifdef PROVIDE_COMPLEX
1478 SPECIFIER_COMPLEX = 1 << 11,
1479 SPECIFIER_IMAGINARY = 1 << 12,
1483 static type_t *create_builtin_type(symbol_t *const symbol,
1484 type_t *const real_type)
1486 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1487 type->builtin.symbol = symbol;
1488 type->builtin.real_type = real_type;
1490 type_t *result = typehash_insert(type);
1491 if (type != result) {
1498 static type_t *get_typedef_type(symbol_t *symbol)
1500 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1501 if(declaration == NULL
1502 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1505 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1506 type->typedeft.declaration = declaration;
1511 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1513 type_t *type = NULL;
1514 unsigned type_qualifiers = 0;
1515 unsigned type_specifiers = 0;
1518 specifiers->source_position = token.source_position;
1521 switch(token.type) {
1524 #define MATCH_STORAGE_CLASS(token, class) \
1526 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1527 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1529 specifiers->storage_class = class; \
1533 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1534 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1535 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1536 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1537 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1540 switch (specifiers->storage_class) {
1541 case STORAGE_CLASS_NONE:
1542 specifiers->storage_class = STORAGE_CLASS_THREAD;
1545 case STORAGE_CLASS_EXTERN:
1546 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1549 case STORAGE_CLASS_STATIC:
1550 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1554 errorf(HERE, "multiple storage classes in declaration specifiers");
1560 /* type qualifiers */
1561 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1563 type_qualifiers |= qualifier; \
1567 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1568 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1569 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1571 case T___extension__:
1576 /* type specifiers */
1577 #define MATCH_SPECIFIER(token, specifier, name) \
1580 if(type_specifiers & specifier) { \
1581 errorf(HERE, "multiple " name " type specifiers given"); \
1583 type_specifiers |= specifier; \
1587 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1588 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1589 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1590 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1591 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1592 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1593 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1594 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1595 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1596 #ifdef PROVIDE_COMPLEX
1597 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1598 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1601 /* only in microsoft mode */
1602 specifiers->decl_modifiers |= DM_FORCEINLINE;
1606 specifiers->is_inline = true;
1611 if(type_specifiers & SPECIFIER_LONG_LONG) {
1612 errorf(HERE, "multiple type specifiers given");
1613 } else if(type_specifiers & SPECIFIER_LONG) {
1614 type_specifiers |= SPECIFIER_LONG_LONG;
1616 type_specifiers |= SPECIFIER_LONG;
1620 /* TODO: if type != NULL for the following rules should issue
1623 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1625 type->compound.declaration = parse_compound_type_specifier(true);
1629 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1631 type->compound.declaration = parse_compound_type_specifier(false);
1635 type = parse_enum_specifier();
1638 type = parse_typeof();
1640 case T___builtin_va_list:
1641 type = duplicate_type(type_valist);
1645 case T___attribute__:
1650 case T_IDENTIFIER: {
1651 type_t *typedef_type = get_typedef_type(token.v.symbol);
1653 if(typedef_type == NULL)
1654 goto finish_specifiers;
1657 type = typedef_type;
1661 /* function specifier */
1663 goto finish_specifiers;
1670 atomic_type_type_t atomic_type;
1672 /* match valid basic types */
1673 switch(type_specifiers) {
1674 case SPECIFIER_VOID:
1675 atomic_type = ATOMIC_TYPE_VOID;
1677 case SPECIFIER_CHAR:
1678 atomic_type = ATOMIC_TYPE_CHAR;
1680 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1681 atomic_type = ATOMIC_TYPE_SCHAR;
1683 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1684 atomic_type = ATOMIC_TYPE_UCHAR;
1686 case SPECIFIER_SHORT:
1687 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1688 case SPECIFIER_SHORT | SPECIFIER_INT:
1689 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1690 atomic_type = ATOMIC_TYPE_SHORT;
1692 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1693 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1694 atomic_type = ATOMIC_TYPE_USHORT;
1697 case SPECIFIER_SIGNED:
1698 case SPECIFIER_SIGNED | SPECIFIER_INT:
1699 atomic_type = ATOMIC_TYPE_INT;
1701 case SPECIFIER_UNSIGNED:
1702 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1703 atomic_type = ATOMIC_TYPE_UINT;
1705 case SPECIFIER_LONG:
1706 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1707 case SPECIFIER_LONG | SPECIFIER_INT:
1708 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1709 atomic_type = ATOMIC_TYPE_LONG;
1711 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1712 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1713 atomic_type = ATOMIC_TYPE_ULONG;
1715 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1716 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1717 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1718 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1720 atomic_type = ATOMIC_TYPE_LONGLONG;
1722 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1723 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1725 atomic_type = ATOMIC_TYPE_ULONGLONG;
1727 case SPECIFIER_FLOAT:
1728 atomic_type = ATOMIC_TYPE_FLOAT;
1730 case SPECIFIER_DOUBLE:
1731 atomic_type = ATOMIC_TYPE_DOUBLE;
1733 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1734 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1736 case SPECIFIER_BOOL:
1737 atomic_type = ATOMIC_TYPE_BOOL;
1739 #ifdef PROVIDE_COMPLEX
1740 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1741 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1743 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1744 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1746 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1747 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1749 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1750 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1752 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1753 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1755 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1756 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1760 /* invalid specifier combination, give an error message */
1761 if(type_specifiers == 0) {
1763 warningf(HERE, "no type specifiers in declaration, using int");
1764 atomic_type = ATOMIC_TYPE_INT;
1767 errorf(HERE, "no type specifiers given in declaration");
1769 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1770 (type_specifiers & SPECIFIER_UNSIGNED)) {
1771 errorf(HERE, "signed and unsigned specifiers gives");
1772 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1773 errorf(HERE, "only integer types can be signed or unsigned");
1775 errorf(HERE, "multiple datatypes in declaration");
1777 atomic_type = ATOMIC_TYPE_INVALID;
1780 type = allocate_type_zero(TYPE_ATOMIC);
1781 type->atomic.atype = atomic_type;
1784 if(type_specifiers != 0) {
1785 errorf(HERE, "multiple datatypes in declaration");
1789 type->base.qualifiers = type_qualifiers;
1791 type_t *result = typehash_insert(type);
1792 if(newtype && result != type) {
1796 specifiers->type = result;
1799 static type_qualifiers_t parse_type_qualifiers(void)
1801 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1804 switch(token.type) {
1805 /* type qualifiers */
1806 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1807 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1808 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1811 return type_qualifiers;
1816 static declaration_t *parse_identifier_list(void)
1818 declaration_t *declarations = NULL;
1819 declaration_t *last_declaration = NULL;
1821 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1823 declaration->source_position = token.source_position;
1824 declaration->symbol = token.v.symbol;
1827 if(last_declaration != NULL) {
1828 last_declaration->next = declaration;
1830 declarations = declaration;
1832 last_declaration = declaration;
1834 if(token.type != ',')
1837 } while(token.type == T_IDENTIFIER);
1839 return declarations;
1842 static void semantic_parameter(declaration_t *declaration)
1844 /* TODO: improve error messages */
1846 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1847 errorf(HERE, "typedef not allowed in parameter list");
1848 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1849 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1850 errorf(HERE, "parameter may only have none or register storage class");
1853 type_t *orig_type = declaration->type;
1854 if(orig_type == NULL)
1856 type_t *type = skip_typeref(orig_type);
1858 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1859 * into a pointer. § 6.7.5.3 (7) */
1860 if (is_type_array(type)) {
1861 const array_type_t *arr_type = &type->array;
1862 type_t *element_type = arr_type->element_type;
1864 type = make_pointer_type(element_type, type->base.qualifiers);
1866 declaration->type = type;
1869 if(is_type_incomplete(type)) {
1870 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%s'", orig_type, declaration->symbol->string);
1874 static declaration_t *parse_parameter(void)
1876 declaration_specifiers_t specifiers;
1877 memset(&specifiers, 0, sizeof(specifiers));
1879 parse_declaration_specifiers(&specifiers);
1881 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1883 semantic_parameter(declaration);
1888 static declaration_t *parse_parameters(function_type_t *type)
1890 if(token.type == T_IDENTIFIER) {
1891 symbol_t *symbol = token.v.symbol;
1892 if(!is_typedef_symbol(symbol)) {
1893 type->kr_style_parameters = true;
1894 return parse_identifier_list();
1898 if(token.type == ')') {
1899 type->unspecified_parameters = 1;
1902 if(token.type == T_void && look_ahead(1)->type == ')') {
1907 declaration_t *declarations = NULL;
1908 declaration_t *declaration;
1909 declaration_t *last_declaration = NULL;
1910 function_parameter_t *parameter;
1911 function_parameter_t *last_parameter = NULL;
1914 switch(token.type) {
1918 return declarations;
1921 case T___extension__:
1923 declaration = parse_parameter();
1925 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1926 memset(parameter, 0, sizeof(parameter[0]));
1927 parameter->type = declaration->type;
1929 if(last_parameter != NULL) {
1930 last_declaration->next = declaration;
1931 last_parameter->next = parameter;
1933 type->parameters = parameter;
1934 declarations = declaration;
1936 last_parameter = parameter;
1937 last_declaration = declaration;
1941 return declarations;
1943 if(token.type != ',')
1944 return declarations;
1954 } construct_type_type_t;
1956 typedef struct construct_type_t construct_type_t;
1957 struct construct_type_t {
1958 construct_type_type_t type;
1959 construct_type_t *next;
1962 typedef struct parsed_pointer_t parsed_pointer_t;
1963 struct parsed_pointer_t {
1964 construct_type_t construct_type;
1965 type_qualifiers_t type_qualifiers;
1968 typedef struct construct_function_type_t construct_function_type_t;
1969 struct construct_function_type_t {
1970 construct_type_t construct_type;
1971 type_t *function_type;
1974 typedef struct parsed_array_t parsed_array_t;
1975 struct parsed_array_t {
1976 construct_type_t construct_type;
1977 type_qualifiers_t type_qualifiers;
1983 typedef struct construct_base_type_t construct_base_type_t;
1984 struct construct_base_type_t {
1985 construct_type_t construct_type;
1989 static construct_type_t *parse_pointer_declarator(void)
1993 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1994 memset(pointer, 0, sizeof(pointer[0]));
1995 pointer->construct_type.type = CONSTRUCT_POINTER;
1996 pointer->type_qualifiers = parse_type_qualifiers();
1998 return (construct_type_t*) pointer;
2001 static construct_type_t *parse_array_declarator(void)
2005 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2006 memset(array, 0, sizeof(array[0]));
2007 array->construct_type.type = CONSTRUCT_ARRAY;
2009 if(token.type == T_static) {
2010 array->is_static = true;
2014 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2015 if(type_qualifiers != 0) {
2016 if(token.type == T_static) {
2017 array->is_static = true;
2021 array->type_qualifiers = type_qualifiers;
2023 if(token.type == '*' && look_ahead(1)->type == ']') {
2024 array->is_variable = true;
2026 } else if(token.type != ']') {
2027 array->size = parse_assignment_expression();
2032 return (construct_type_t*) array;
2035 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2039 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2041 declaration_t *parameters = parse_parameters(&type->function);
2042 if(declaration != NULL) {
2043 declaration->context.declarations = parameters;
2046 construct_function_type_t *construct_function_type =
2047 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2048 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2049 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2050 construct_function_type->function_type = type;
2054 return (construct_type_t*) construct_function_type;
2057 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2058 bool may_be_abstract)
2060 /* construct a single linked list of construct_type_t's which describe
2061 * how to construct the final declarator type */
2062 construct_type_t *first = NULL;
2063 construct_type_t *last = NULL;
2066 while(token.type == '*') {
2067 construct_type_t *type = parse_pointer_declarator();
2078 /* TODO: find out if this is correct */
2081 construct_type_t *inner_types = NULL;
2083 switch(token.type) {
2085 if(declaration == NULL) {
2086 errorf(HERE, "no identifier expected in typename");
2088 declaration->symbol = token.v.symbol;
2089 declaration->source_position = token.source_position;
2095 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2101 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2102 /* avoid a loop in the outermost scope, because eat_statement doesn't
2104 if(token.type == '}' && current_function == NULL) {
2112 construct_type_t *p = last;
2115 construct_type_t *type;
2116 switch(token.type) {
2118 type = parse_function_declarator(declaration);
2121 type = parse_array_declarator();
2124 goto declarator_finished;
2127 /* insert in the middle of the list (behind p) */
2129 type->next = p->next;
2140 declarator_finished:
2143 /* append inner_types at the end of the list, we don't to set last anymore
2144 * as it's not needed anymore */
2146 assert(first == NULL);
2147 first = inner_types;
2149 last->next = inner_types;
2155 static type_t *construct_declarator_type(construct_type_t *construct_list,
2158 construct_type_t *iter = construct_list;
2159 for( ; iter != NULL; iter = iter->next) {
2160 switch(iter->type) {
2161 case CONSTRUCT_INVALID:
2162 panic("invalid type construction found");
2163 case CONSTRUCT_FUNCTION: {
2164 construct_function_type_t *construct_function_type
2165 = (construct_function_type_t*) iter;
2167 type_t *function_type = construct_function_type->function_type;
2169 function_type->function.return_type = type;
2171 type = function_type;
2175 case CONSTRUCT_POINTER: {
2176 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2177 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2178 pointer_type->pointer.points_to = type;
2179 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2181 type = pointer_type;
2185 case CONSTRUCT_ARRAY: {
2186 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2187 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2189 array_type->base.qualifiers = parsed_array->type_qualifiers;
2190 array_type->array.element_type = type;
2191 array_type->array.is_static = parsed_array->is_static;
2192 array_type->array.is_variable = parsed_array->is_variable;
2193 array_type->array.size = parsed_array->size;
2200 type_t *hashed_type = typehash_insert(type);
2201 if(hashed_type != type) {
2202 /* the function type was constructed earlier freeing it here will
2203 * destroy other types... */
2204 if(iter->type != CONSTRUCT_FUNCTION) {
2214 static declaration_t *parse_declarator(
2215 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2217 type_t *type = specifiers->type;
2218 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2219 declaration->storage_class = specifiers->storage_class;
2220 declaration->decl_modifiers = specifiers->decl_modifiers;
2221 declaration->is_inline = specifiers->is_inline;
2223 construct_type_t *construct_type
2224 = parse_inner_declarator(declaration, may_be_abstract);
2225 declaration->type = construct_declarator_type(construct_type, type);
2227 if(construct_type != NULL) {
2228 obstack_free(&temp_obst, construct_type);
2234 static type_t *parse_abstract_declarator(type_t *base_type)
2236 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2238 type_t *result = construct_declarator_type(construct_type, base_type);
2239 if(construct_type != NULL) {
2240 obstack_free(&temp_obst, construct_type);
2246 static declaration_t *record_declaration(declaration_t *declaration)
2248 assert(declaration->parent_context == NULL);
2249 assert(context != NULL);
2251 symbol_t *symbol = declaration->symbol;
2252 if(symbol != NULL) {
2253 declaration_t *alias = environment_push(declaration);
2254 if(alias != declaration)
2257 declaration->parent_context = context;
2260 if(last_declaration != NULL) {
2261 last_declaration->next = declaration;
2263 context->declarations = declaration;
2265 last_declaration = declaration;
2270 static void parser_error_multiple_definition(declaration_t *declaration,
2271 const source_position_t source_position)
2273 errorf(source_position, "multiple definition of symbol '%s'", declaration->symbol->string);
2274 errorf(declaration->source_position, "this is the location of the previous definition.");
2277 static bool is_declaration_specifier(const token_t *token,
2278 bool only_type_specifiers)
2280 switch(token->type) {
2284 return is_typedef_symbol(token->v.symbol);
2286 case T___extension__:
2289 return !only_type_specifiers;
2296 static void parse_init_declarator_rest(declaration_t *declaration)
2300 type_t *orig_type = declaration->type;
2301 type_t *type = NULL;
2302 if(orig_type != NULL)
2303 type = skip_typeref(orig_type);
2305 if(declaration->init.initializer != NULL) {
2306 parser_error_multiple_definition(declaration, token.source_position);
2309 initializer_t *initializer = parse_initializer(type);
2311 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2312 * the array type size */
2313 if(type != NULL && is_type_array(type) && initializer != NULL) {
2314 array_type_t *array_type = &type->array;
2316 if(array_type->size == NULL) {
2317 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2319 cnst->base.datatype = type_size_t;
2321 switch (initializer->kind) {
2322 case INITIALIZER_LIST: {
2323 initializer_list_t *const list = &initializer->list;
2324 cnst->conste.v.int_value = list->len;
2328 case INITIALIZER_STRING: {
2329 initializer_string_t *const string = &initializer->string;
2330 cnst->conste.v.int_value = strlen(string->string) + 1;
2334 case INITIALIZER_WIDE_STRING: {
2335 initializer_wide_string_t *const string = &initializer->wide_string;
2336 cnst->conste.v.int_value = string->string.size;
2341 panic("invalid initializer type");
2344 array_type->size = cnst;
2348 if(type != NULL && is_type_function(type)) {
2349 errorf(declaration->source_position, "initializers not allowed for function types at declator '%s' (type '%T')", declaration->symbol->string, orig_type);
2351 declaration->init.initializer = initializer;
2355 /* parse rest of a declaration without any declarator */
2356 static void parse_anonymous_declaration_rest(
2357 const declaration_specifiers_t *specifiers,
2358 parsed_declaration_func finished_declaration)
2362 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2364 declaration->type = specifiers->type;
2365 declaration->storage_class = specifiers->storage_class;
2366 declaration->source_position = specifiers->source_position;
2368 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2369 warningf(declaration->source_position, "useless storage class in empty declaration");
2372 type_t *type = declaration->type;
2373 switch (type->kind) {
2374 case TYPE_COMPOUND_STRUCT:
2375 case TYPE_COMPOUND_UNION: {
2376 const compound_type_t *compound_type = &type->compound;
2377 if (compound_type->declaration->symbol == NULL) {
2378 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2387 warningf(declaration->source_position, "empty declaration");
2391 finished_declaration(declaration);
2394 static void parse_declaration_rest(declaration_t *ndeclaration,
2395 const declaration_specifiers_t *specifiers,
2396 parsed_declaration_func finished_declaration)
2399 declaration_t *declaration = finished_declaration(ndeclaration);
2401 type_t *orig_type = declaration->type;
2402 type_t *type = skip_typeref(orig_type);
2404 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2405 warningf(declaration->source_position, "variable '%s' declared 'inline'\n", declaration->symbol->string);
2408 if(token.type == '=') {
2409 parse_init_declarator_rest(declaration);
2412 if(token.type != ',')
2416 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2421 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2423 /* TODO: check that it was actually a parameter that gets a type */
2425 /* we should have a declaration for the parameter in the current
2427 return record_declaration(declaration);
2430 static void parse_declaration(parsed_declaration_func finished_declaration)
2432 declaration_specifiers_t specifiers;
2433 memset(&specifiers, 0, sizeof(specifiers));
2434 parse_declaration_specifiers(&specifiers);
2436 if(token.type == ';') {
2437 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2439 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2440 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2444 static void parse_kr_declaration_list(declaration_t *declaration)
2446 type_t *type = skip_typeref(declaration->type);
2447 if(!is_type_function(type))
2450 if(!type->function.kr_style_parameters)
2453 /* push function parameters */
2454 int top = environment_top();
2455 context_t *last_context = context;
2456 set_context(&declaration->context);
2458 declaration_t *parameter = declaration->context.declarations;
2459 for( ; parameter != NULL; parameter = parameter->next) {
2460 environment_push(parameter);
2463 /* parse declaration list */
2464 while(is_declaration_specifier(&token, false)) {
2465 parse_declaration(finished_kr_declaration);
2468 /* pop function parameters */
2469 assert(context == &declaration->context);
2470 set_context(last_context);
2471 environment_pop_to(top);
2473 /* update function type */
2474 type_t *new_type = duplicate_type(type);
2475 new_type->function.kr_style_parameters = false;
2477 function_parameter_t *parameters = NULL;
2478 function_parameter_t *last_parameter = NULL;
2480 declaration_t *parameter_declaration = declaration->context.declarations;
2481 for( ; parameter_declaration != NULL;
2482 parameter_declaration = parameter_declaration->next) {
2483 type_t *parameter_type = parameter_declaration->type;
2484 if(parameter_type == NULL) {
2486 errorf(HERE, "no type specified for function parameter '%s'", parameter_declaration->symbol->string);
2488 warningf(HERE, "no type specified for function parameter '%s', using int", parameter_declaration->symbol->string);
2489 parameter_type = type_int;
2490 parameter_declaration->type = parameter_type;
2494 semantic_parameter(parameter_declaration);
2495 parameter_type = parameter_declaration->type;
2497 function_parameter_t *function_parameter
2498 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2499 memset(function_parameter, 0, sizeof(function_parameter[0]));
2501 function_parameter->type = parameter_type;
2502 if(last_parameter != NULL) {
2503 last_parameter->next = function_parameter;
2505 parameters = function_parameter;
2507 last_parameter = function_parameter;
2509 new_type->function.parameters = parameters;
2511 type = typehash_insert(new_type);
2512 if(type != new_type) {
2513 obstack_free(type_obst, new_type);
2516 declaration->type = type;
2519 static void parse_external_declaration(void)
2521 /* function-definitions and declarations both start with declaration
2523 declaration_specifiers_t specifiers;
2524 memset(&specifiers, 0, sizeof(specifiers));
2525 parse_declaration_specifiers(&specifiers);
2527 /* must be a declaration */
2528 if(token.type == ';') {
2529 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2533 /* declarator is common to both function-definitions and declarations */
2534 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2536 /* must be a declaration */
2537 if(token.type == ',' || token.type == '=' || token.type == ';') {
2538 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2542 /* must be a function definition */
2543 parse_kr_declaration_list(ndeclaration);
2545 if(token.type != '{') {
2546 parse_error_expected("while parsing function definition", '{', 0);
2551 type_t *type = ndeclaration->type;
2557 /* note that we don't skip typerefs: the standard doesn't allow them here
2558 * (so we can't use is_type_function here) */
2559 if(type->kind != TYPE_FUNCTION) {
2560 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2565 /* § 6.7.5.3 (14) a function definition with () means no
2566 * parameters (and not unspecified parameters) */
2567 if(type->function.unspecified_parameters) {
2568 type_t *duplicate = duplicate_type(type);
2569 duplicate->function.unspecified_parameters = false;
2571 type = typehash_insert(duplicate);
2572 if(type != duplicate) {
2573 obstack_free(type_obst, duplicate);
2575 ndeclaration->type = type;
2578 declaration_t *declaration = record_declaration(ndeclaration);
2579 if(ndeclaration != declaration) {
2580 memcpy(&declaration->context, &ndeclaration->context,
2581 sizeof(declaration->context));
2583 type = skip_typeref(declaration->type);
2585 /* push function parameters and switch context */
2586 int top = environment_top();
2587 context_t *last_context = context;
2588 set_context(&declaration->context);
2590 declaration_t *parameter = declaration->context.declarations;
2591 for( ; parameter != NULL; parameter = parameter->next) {
2592 environment_push(parameter);
2595 if(declaration->init.statement != NULL) {
2596 parser_error_multiple_definition(declaration, token.source_position);
2598 goto end_of_parse_external_declaration;
2600 /* parse function body */
2601 int label_stack_top = label_top();
2602 declaration_t *old_current_function = current_function;
2603 current_function = declaration;
2605 declaration->init.statement = parse_compound_statement();
2607 assert(current_function == declaration);
2608 current_function = old_current_function;
2609 label_pop_to(label_stack_top);
2612 end_of_parse_external_declaration:
2613 assert(context == &declaration->context);
2614 set_context(last_context);
2615 environment_pop_to(top);
2618 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2621 if(token.type == ':') {
2623 parse_constant_expression();
2624 /* TODO (bitfields) */
2626 declaration_t *declaration = parse_declarator(specifiers, /*may_be_abstract=*/true);
2628 /* TODO: check constraints for struct declarations */
2629 /* TODO: check for doubled fields */
2630 record_declaration(declaration);
2632 if(token.type == ':') {
2634 parse_constant_expression();
2635 /* TODO (bitfields) */
2639 if(token.type != ',')
2646 static void parse_compound_type_entries(void)
2650 while(token.type != '}' && token.type != T_EOF) {
2651 declaration_specifiers_t specifiers;
2652 memset(&specifiers, 0, sizeof(specifiers));
2653 parse_declaration_specifiers(&specifiers);
2655 parse_struct_declarators(&specifiers);
2657 if(token.type == T_EOF) {
2658 errorf(HERE, "EOF while parsing struct");
2663 static type_t *parse_typename(void)
2665 declaration_specifiers_t specifiers;
2666 memset(&specifiers, 0, sizeof(specifiers));
2667 parse_declaration_specifiers(&specifiers);
2668 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2669 /* TODO: improve error message, user does probably not know what a
2670 * storage class is...
2672 errorf(HERE, "typename may not have a storage class");
2675 type_t *result = parse_abstract_declarator(specifiers.type);
2683 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2684 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2685 expression_t *left);
2687 typedef struct expression_parser_function_t expression_parser_function_t;
2688 struct expression_parser_function_t {
2689 unsigned precedence;
2690 parse_expression_function parser;
2691 unsigned infix_precedence;
2692 parse_expression_infix_function infix_parser;
2695 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2697 static expression_t *create_invalid_expression(void)
2699 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2700 expression->base.source_position = token.source_position;
2704 static expression_t *expected_expression_error(void)
2706 errorf(HERE, "expected expression, got token '%K'", &token);
2710 return create_invalid_expression();
2713 static expression_t *parse_string_const(void)
2715 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2716 cnst->base.datatype = type_string;
2717 cnst->string.value = parse_string_literals();
2722 static expression_t *parse_wide_string_const(void)
2724 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2725 cnst->base.datatype = type_wchar_t_ptr;
2726 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2731 static expression_t *parse_int_const(void)
2733 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2734 cnst->base.datatype = token.datatype;
2735 cnst->conste.v.int_value = token.v.intvalue;
2742 static expression_t *parse_float_const(void)
2744 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2745 cnst->base.datatype = token.datatype;
2746 cnst->conste.v.float_value = token.v.floatvalue;
2753 static declaration_t *create_implicit_function(symbol_t *symbol,
2754 const source_position_t source_position)
2756 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2757 ntype->function.return_type = type_int;
2758 ntype->function.unspecified_parameters = true;
2760 type_t *type = typehash_insert(ntype);
2765 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2767 declaration->storage_class = STORAGE_CLASS_EXTERN;
2768 declaration->type = type;
2769 declaration->symbol = symbol;
2770 declaration->source_position = source_position;
2772 /* prepend the implicit definition to the global context
2773 * this is safe since the symbol wasn't declared as anything else yet
2775 assert(symbol->declaration == NULL);
2777 context_t *last_context = context;
2778 context = global_context;
2780 environment_push(declaration);
2781 declaration->next = context->declarations;
2782 context->declarations = declaration;
2784 context = last_context;
2789 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2791 function_parameter_t *parameter
2792 = obstack_alloc(type_obst, sizeof(parameter[0]));
2793 memset(parameter, 0, sizeof(parameter[0]));
2794 parameter->type = argument_type;
2796 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2797 type->function.return_type = return_type;
2798 type->function.parameters = parameter;
2800 type_t *result = typehash_insert(type);
2801 if(result != type) {
2808 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2810 switch(symbol->ID) {
2811 case T___builtin_alloca:
2812 return make_function_1_type(type_void_ptr, type_size_t);
2813 case T___builtin_nan:
2814 return make_function_1_type(type_double, type_string);
2815 case T___builtin_nanf:
2816 return make_function_1_type(type_float, type_string);
2817 case T___builtin_nand:
2818 return make_function_1_type(type_long_double, type_string);
2819 case T___builtin_va_end:
2820 return make_function_1_type(type_void, type_valist);
2822 panic("not implemented builtin symbol found");
2827 * performs automatic type cast as described in § 6.3.2.1
2829 static type_t *automatic_type_conversion(type_t *orig_type)
2831 if(orig_type == NULL)
2834 type_t *type = skip_typeref(orig_type);
2835 if(is_type_array(type)) {
2836 array_type_t *array_type = &type->array;
2837 type_t *element_type = array_type->element_type;
2838 unsigned qualifiers = array_type->type.qualifiers;
2840 return make_pointer_type(element_type, qualifiers);
2843 if(is_type_function(type)) {
2844 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
2851 * reverts the automatic casts of array to pointer types and function
2852 * to function-pointer types as defined § 6.3.2.1
2854 type_t *revert_automatic_type_conversion(const expression_t *expression)
2856 if(expression->base.datatype == NULL)
2859 switch(expression->kind) {
2860 case EXPR_REFERENCE: {
2861 const reference_expression_t *ref = &expression->reference;
2862 return ref->declaration->type;
2865 const select_expression_t *select = &expression->select;
2866 return select->compound_entry->type;
2868 case EXPR_UNARY_DEREFERENCE: {
2869 expression_t *value = expression->unary.value;
2870 type_t *type = skip_typeref(value->base.datatype);
2871 pointer_type_t *pointer_type = &type->pointer;
2873 return pointer_type->points_to;
2875 case EXPR_BUILTIN_SYMBOL: {
2876 const builtin_symbol_expression_t *builtin
2877 = &expression->builtin_symbol;
2878 return get_builtin_symbol_type(builtin->symbol);
2880 case EXPR_ARRAY_ACCESS: {
2881 const array_access_expression_t *array_access
2882 = &expression->array_access;
2883 const expression_t *array_ref = array_access->array_ref;
2884 type_t *type_left = skip_typeref(array_ref->base.datatype);
2885 assert(is_type_pointer(type_left));
2886 pointer_type_t *pointer_type = &type_left->pointer;
2887 return pointer_type->points_to;
2894 return expression->base.datatype;
2897 static expression_t *parse_reference(void)
2899 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2901 reference_expression_t *ref = &expression->reference;
2902 ref->symbol = token.v.symbol;
2904 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2906 source_position_t source_position = token.source_position;
2909 if(declaration == NULL) {
2911 /* an implicitly defined function */
2912 if(token.type == '(') {
2913 warningf(HERE, "implicit declaration of function '%s'\n", ref->symbol->string);
2915 declaration = create_implicit_function(ref->symbol,
2920 errorf(HERE, "unknown symbol '%s' found.\n", ref->symbol->string);
2925 type_t *type = declaration->type;
2926 /* we always do the auto-type conversions; the & and sizeof parser contains
2927 * code to revert this! */
2928 type = automatic_type_conversion(type);
2930 ref->declaration = declaration;
2931 ref->expression.datatype = type;
2936 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2940 /* TODO check if explicit cast is allowed and issue warnings/errors */
2943 static expression_t *parse_cast(void)
2945 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
2947 cast->base.source_position = token.source_position;
2949 type_t *type = parse_typename();
2952 expression_t *value = parse_sub_expression(20);
2954 check_cast_allowed(value, type);
2956 cast->base.datatype = type;
2957 cast->unary.value = value;
2962 static expression_t *parse_statement_expression(void)
2964 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
2966 statement_t *statement = parse_compound_statement();
2967 expression->statement.statement = statement;
2968 if(statement == NULL) {
2973 assert(statement->kind == STATEMENT_COMPOUND);
2974 compound_statement_t *compound_statement = &statement->compound;
2976 /* find last statement and use it's type */
2977 const statement_t *last_statement = NULL;
2978 const statement_t *iter = compound_statement->statements;
2979 for( ; iter != NULL; iter = iter->base.next) {
2980 last_statement = iter;
2983 if(last_statement->kind == STATEMENT_EXPRESSION) {
2984 const expression_statement_t *expression_statement
2985 = &last_statement->expression;
2986 expression->base.datatype
2987 = expression_statement->expression->base.datatype;
2989 expression->base.datatype = type_void;
2997 static expression_t *parse_brace_expression(void)
3001 switch(token.type) {
3003 /* gcc extension: a statement expression */
3004 return parse_statement_expression();
3008 return parse_cast();
3010 if(is_typedef_symbol(token.v.symbol)) {
3011 return parse_cast();
3015 expression_t *result = parse_expression();
3021 static expression_t *parse_function_keyword(void)
3026 if (current_function == NULL) {
3027 errorf(HERE, "'__func__' used outside of a function");
3030 string_literal_expression_t *expression
3031 = allocate_ast_zero(sizeof(expression[0]));
3033 expression->expression.kind = EXPR_FUNCTION;
3034 expression->expression.datatype = type_string;
3035 expression->value = current_function->symbol->string;
3037 return (expression_t*) expression;
3040 static expression_t *parse_pretty_function_keyword(void)
3042 eat(T___PRETTY_FUNCTION__);
3045 if (current_function == NULL) {
3046 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3049 string_literal_expression_t *expression
3050 = allocate_ast_zero(sizeof(expression[0]));
3052 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3053 expression->expression.datatype = type_string;
3054 expression->value = current_function->symbol->string;
3056 return (expression_t*) expression;
3059 static designator_t *parse_designator(void)
3061 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3063 if(token.type != T_IDENTIFIER) {
3064 parse_error_expected("while parsing member designator",
3069 result->symbol = token.v.symbol;
3072 designator_t *last_designator = result;
3074 if(token.type == '.') {
3076 if(token.type != T_IDENTIFIER) {
3077 parse_error_expected("while parsing member designator",
3082 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3083 designator->symbol = token.v.symbol;
3086 last_designator->next = designator;
3087 last_designator = designator;
3090 if(token.type == '[') {
3092 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3093 designator->array_access = parse_expression();
3094 if(designator->array_access == NULL) {
3100 last_designator->next = designator;
3101 last_designator = designator;
3110 static expression_t *parse_offsetof(void)
3112 eat(T___builtin_offsetof);
3114 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3115 expression->base.datatype = type_size_t;
3118 expression->offsetofe.type = parse_typename();
3120 expression->offsetofe.designator = parse_designator();
3126 static expression_t *parse_va_start(void)
3128 eat(T___builtin_va_start);
3130 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3133 expression->va_starte.ap = parse_assignment_expression();
3135 expression_t *const expr = parse_assignment_expression();
3136 if (expr->kind == EXPR_REFERENCE) {
3137 declaration_t *const decl = expr->reference.declaration;
3138 if (decl->parent_context == ¤t_function->context &&
3139 decl->next == NULL) {
3140 expression->va_starte.parameter = decl;
3145 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3147 return create_invalid_expression();
3150 static expression_t *parse_va_arg(void)
3152 eat(T___builtin_va_arg);
3154 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3157 expression->va_arge.ap = parse_assignment_expression();
3159 expression->base.datatype = parse_typename();
3165 static expression_t *parse_builtin_symbol(void)
3167 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3169 symbol_t *symbol = token.v.symbol;
3171 expression->builtin_symbol.symbol = symbol;
3174 type_t *type = get_builtin_symbol_type(symbol);
3175 type = automatic_type_conversion(type);
3177 expression->base.datatype = type;
3181 static expression_t *parse_builtin_constant(void)
3183 eat(T___builtin_constant_p);
3185 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3188 expression->builtin_constant.value = parse_expression();
3190 expression->base.datatype = type_int;
3195 static expression_t *parse_compare_builtin(void)
3197 expression_t *expression;
3199 switch(token.type) {
3200 case T___builtin_isgreater:
3201 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3203 case T___builtin_isgreaterequal:
3204 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3206 case T___builtin_isless:
3207 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3209 case T___builtin_islessequal:
3210 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3212 case T___builtin_islessgreater:
3213 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3215 case T___builtin_isunordered:
3216 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3219 panic("invalid compare builtin found");
3225 expression->binary.left = parse_assignment_expression();
3227 expression->binary.right = parse_assignment_expression();
3230 type_t *orig_type_left = expression->binary.left->base.datatype;
3231 type_t *orig_type_right = expression->binary.right->base.datatype;
3232 if(orig_type_left == NULL || orig_type_right == NULL)
3235 type_t *type_left = skip_typeref(orig_type_left);
3236 type_t *type_right = skip_typeref(orig_type_right);
3237 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3238 type_error_incompatible("invalid operands in comparison",
3239 token.source_position, type_left, type_right);
3241 semantic_comparison(&expression->binary);
3247 static expression_t *parse_builtin_expect(void)
3249 eat(T___builtin_expect);
3251 expression_t *expression
3252 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3255 expression->binary.left = parse_assignment_expression();
3257 expression->binary.right = parse_constant_expression();
3260 expression->base.datatype = expression->binary.left->base.datatype;
3265 static expression_t *parse_assume(void) {
3268 expression_t *expression
3269 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3272 expression->unary.value = parse_expression();
3275 expression->base.datatype = type_void;
3279 static expression_t *parse_alignof(void) {
3282 expression_t *expression
3283 = allocate_expression_zero(EXPR_ALIGNOF);
3286 expression->alignofe.type = parse_typename();
3289 expression->base.datatype = type_size_t;
3293 static expression_t *parse_primary_expression(void)
3295 switch(token.type) {
3297 return parse_int_const();
3298 case T_FLOATINGPOINT:
3299 return parse_float_const();
3300 case T_STRING_LITERAL: /* TODO merge */
3301 return parse_string_const();
3302 case T_WIDE_STRING_LITERAL:
3303 return parse_wide_string_const();
3305 return parse_reference();
3306 case T___FUNCTION__:
3308 return parse_function_keyword();
3309 case T___PRETTY_FUNCTION__:
3310 return parse_pretty_function_keyword();
3311 case T___builtin_offsetof:
3312 return parse_offsetof();
3313 case T___builtin_va_start:
3314 return parse_va_start();
3315 case T___builtin_va_arg:
3316 return parse_va_arg();
3317 case T___builtin_expect:
3318 return parse_builtin_expect();
3319 case T___builtin_nanf:
3320 case T___builtin_alloca:
3321 case T___builtin_va_end:
3322 return parse_builtin_symbol();
3323 case T___builtin_isgreater:
3324 case T___builtin_isgreaterequal:
3325 case T___builtin_isless:
3326 case T___builtin_islessequal:
3327 case T___builtin_islessgreater:
3328 case T___builtin_isunordered:
3329 return parse_compare_builtin();
3330 case T___builtin_constant_p:
3331 return parse_builtin_constant();
3333 return parse_alignof();
3335 return parse_assume();
3338 return parse_brace_expression();
3341 errorf(HERE, "unexpected token '%K'", &token);
3344 return create_invalid_expression();
3347 static expression_t *parse_array_expression(unsigned precedence,
3354 expression_t *inside = parse_expression();
3356 array_access_expression_t *array_access
3357 = allocate_ast_zero(sizeof(array_access[0]));
3359 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3361 type_t *type_left = left->base.datatype;
3362 type_t *type_inside = inside->base.datatype;
3363 type_t *return_type = NULL;
3365 if(type_left != NULL && type_inside != NULL) {
3366 type_left = skip_typeref(type_left);
3367 type_inside = skip_typeref(type_inside);
3369 if(is_type_pointer(type_left)) {
3370 pointer_type_t *pointer = &type_left->pointer;
3371 return_type = pointer->points_to;
3372 array_access->array_ref = left;
3373 array_access->index = inside;
3374 } else if(is_type_pointer(type_inside)) {
3375 pointer_type_t *pointer = &type_inside->pointer;
3376 return_type = pointer->points_to;
3377 array_access->array_ref = inside;
3378 array_access->index = left;
3379 array_access->flipped = true;
3381 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3384 array_access->array_ref = left;
3385 array_access->index = inside;
3388 if(token.type != ']') {
3389 parse_error_expected("Problem while parsing array access", ']', 0);
3390 return (expression_t*) array_access;
3394 return_type = automatic_type_conversion(return_type);
3395 array_access->expression.datatype = return_type;
3397 return (expression_t*) array_access;
3400 static expression_t *parse_sizeof(unsigned precedence)
3404 sizeof_expression_t *sizeof_expression
3405 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3406 sizeof_expression->expression.kind = EXPR_SIZEOF;
3407 sizeof_expression->expression.datatype = type_size_t;
3409 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3411 sizeof_expression->type = parse_typename();
3414 expression_t *expression = parse_sub_expression(precedence);
3415 expression->base.datatype = revert_automatic_type_conversion(expression);
3417 sizeof_expression->type = expression->base.datatype;
3418 sizeof_expression->size_expression = expression;
3421 return (expression_t*) sizeof_expression;
3424 static expression_t *parse_select_expression(unsigned precedence,
3425 expression_t *compound)
3428 assert(token.type == '.' || token.type == T_MINUSGREATER);
3430 bool is_pointer = (token.type == T_MINUSGREATER);
3433 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3434 select->select.compound = compound;
3436 if(token.type != T_IDENTIFIER) {
3437 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3440 symbol_t *symbol = token.v.symbol;
3441 select->select.symbol = symbol;
3444 type_t *orig_type = compound->base.datatype;
3445 if(orig_type == NULL)
3446 return create_invalid_expression();
3448 type_t *type = skip_typeref(orig_type);
3450 type_t *type_left = type;
3452 if(type->kind != TYPE_POINTER) {
3453 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3454 return create_invalid_expression();
3456 pointer_type_t *pointer_type = &type->pointer;
3457 type_left = pointer_type->points_to;
3459 type_left = skip_typeref(type_left);
3461 if(type_left->kind != TYPE_COMPOUND_STRUCT
3462 && type_left->kind != TYPE_COMPOUND_UNION) {
3463 errorf(HERE, "request for member '%s' in something not a struct or union, but '%T'", symbol->string, type_left);
3464 return create_invalid_expression();
3467 compound_type_t *compound_type = &type_left->compound;
3468 declaration_t *declaration = compound_type->declaration;
3470 if(!declaration->init.is_defined) {
3471 errorf(HERE, "request for member '%s' of incomplete type '%T'", symbol->string, type_left);
3472 return create_invalid_expression();
3475 declaration_t *iter = declaration->context.declarations;
3476 for( ; iter != NULL; iter = iter->next) {
3477 if(iter->symbol == symbol) {
3482 errorf(HERE, "'%T' has no member names '%s'", type_left, symbol->string);
3483 return create_invalid_expression();
3486 /* we always do the auto-type conversions; the & and sizeof parser contains
3487 * code to revert this! */
3488 type_t *expression_type = automatic_type_conversion(iter->type);
3490 select->select.compound_entry = iter;
3491 select->base.datatype = expression_type;
3495 static expression_t *parse_call_expression(unsigned precedence,
3496 expression_t *expression)
3499 expression_t *result = allocate_expression_zero(EXPR_CALL);
3501 call_expression_t *call = &result->call;
3502 call->function = expression;
3504 function_type_t *function_type = NULL;
3505 type_t *orig_type = expression->base.datatype;
3506 if(orig_type != NULL) {
3507 type_t *type = skip_typeref(orig_type);
3509 if(is_type_pointer(type)) {
3510 pointer_type_t *pointer_type = &type->pointer;
3512 type = skip_typeref(pointer_type->points_to);
3514 if (is_type_function(type)) {
3515 function_type = &type->function;
3516 call->expression.datatype = function_type->return_type;
3519 if(function_type == NULL) {
3520 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3522 function_type = NULL;
3523 call->expression.datatype = NULL;
3527 /* parse arguments */
3530 if(token.type != ')') {
3531 call_argument_t *last_argument = NULL;
3534 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3536 argument->expression = parse_assignment_expression();
3537 if(last_argument == NULL) {
3538 call->arguments = argument;
3540 last_argument->next = argument;
3542 last_argument = argument;
3544 if(token.type != ',')
3551 if(function_type != NULL) {
3552 function_parameter_t *parameter = function_type->parameters;
3553 call_argument_t *argument = call->arguments;
3554 for( ; parameter != NULL && argument != NULL;
3555 parameter = parameter->next, argument = argument->next) {
3556 type_t *expected_type = parameter->type;
3557 /* TODO report context in error messages */
3558 argument->expression = create_implicit_cast(argument->expression,
3561 /* too few parameters */
3562 if(parameter != NULL) {
3563 errorf(HERE, "too few arguments to function '%E'", expression);
3564 } else if(argument != NULL) {
3565 /* too many parameters */
3566 if(!function_type->variadic
3567 && !function_type->unspecified_parameters) {
3568 errorf(HERE, "too many arguments to function '%E'", expression);
3570 /* do default promotion */
3571 for( ; argument != NULL; argument = argument->next) {
3572 type_t *type = argument->expression->base.datatype;
3577 type = skip_typeref(type);
3578 if(is_type_integer(type)) {
3579 type = promote_integer(type);
3580 } else if(type == type_float) {
3584 argument->expression
3585 = create_implicit_cast(argument->expression, type);
3588 check_format(&result->call);
3591 check_format(&result->call);
3598 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3600 static bool same_compound_type(const type_t *type1, const type_t *type2)
3602 if(!is_type_compound(type1))
3604 if(type1->kind != type2->kind)
3607 const compound_type_t *compound1 = &type1->compound;
3608 const compound_type_t *compound2 = &type2->compound;
3610 return compound1->declaration == compound2->declaration;
3613 static expression_t *parse_conditional_expression(unsigned precedence,
3614 expression_t *expression)
3618 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3620 conditional_expression_t *conditional = &result->conditional;
3621 conditional->condition = expression;
3624 type_t *condition_type_orig = expression->base.datatype;
3625 if(condition_type_orig != NULL) {
3626 type_t *condition_type = skip_typeref(condition_type_orig);
3627 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3628 type_error("expected a scalar type in conditional condition",
3629 expression->base.source_position, condition_type_orig);
3633 expression_t *true_expression = parse_expression();
3635 expression_t *false_expression = parse_sub_expression(precedence);
3637 conditional->true_expression = true_expression;
3638 conditional->false_expression = false_expression;
3640 type_t *orig_true_type = true_expression->base.datatype;
3641 type_t *orig_false_type = false_expression->base.datatype;
3642 if(orig_true_type == NULL || orig_false_type == NULL)
3645 type_t *true_type = skip_typeref(orig_true_type);
3646 type_t *false_type = skip_typeref(orig_false_type);
3649 type_t *result_type = NULL;
3650 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3651 result_type = semantic_arithmetic(true_type, false_type);
3653 true_expression = create_implicit_cast(true_expression, result_type);
3654 false_expression = create_implicit_cast(false_expression, result_type);
3656 conditional->true_expression = true_expression;
3657 conditional->false_expression = false_expression;
3658 conditional->expression.datatype = result_type;
3659 } else if (same_compound_type(true_type, false_type)
3660 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3661 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3662 /* just take 1 of the 2 types */
3663 result_type = true_type;
3664 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3665 && pointers_compatible(true_type, false_type)) {
3667 result_type = true_type;
3670 type_error_incompatible("while parsing conditional",
3671 expression->base.source_position, true_type,
3675 conditional->expression.datatype = result_type;
3679 static expression_t *parse_extension(unsigned precedence)
3681 eat(T___extension__);
3683 /* TODO enable extensions */
3685 return parse_sub_expression(precedence);
3688 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3690 eat(T___builtin_classify_type);
3692 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3693 result->base.datatype = type_int;
3696 expression_t *expression = parse_sub_expression(precedence);
3698 result->classify_type.type_expression = expression;
3703 static void semantic_incdec(unary_expression_t *expression)
3705 type_t *orig_type = expression->value->base.datatype;
3706 if(orig_type == NULL)
3709 type_t *type = skip_typeref(orig_type);
3710 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3711 /* TODO: improve error message */
3712 errorf(HERE, "operation needs an arithmetic or pointer type");
3716 expression->expression.datatype = orig_type;
3719 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3721 type_t *orig_type = expression->value->base.datatype;
3722 if(orig_type == NULL)
3725 type_t *type = skip_typeref(orig_type);
3726 if(!is_type_arithmetic(type)) {
3727 /* TODO: improve error message */
3728 errorf(HERE, "operation needs an arithmetic type");
3732 expression->expression.datatype = orig_type;
3735 static void semantic_unexpr_scalar(unary_expression_t *expression)
3737 type_t *orig_type = expression->value->base.datatype;
3738 if(orig_type == NULL)
3741 type_t *type = skip_typeref(orig_type);
3742 if (!is_type_scalar(type)) {
3743 errorf(HERE, "operand of ! must be of scalar type");
3747 expression->expression.datatype = orig_type;
3750 static void semantic_unexpr_integer(unary_expression_t *expression)
3752 type_t *orig_type = expression->value->base.datatype;
3753 if(orig_type == NULL)
3756 type_t *type = skip_typeref(orig_type);
3757 if (!is_type_integer(type)) {
3758 errorf(HERE, "operand of ~ must be of integer type");
3762 expression->expression.datatype = orig_type;
3765 static void semantic_dereference(unary_expression_t *expression)
3767 type_t *orig_type = expression->value->base.datatype;
3768 if(orig_type == NULL)
3771 type_t *type = skip_typeref(orig_type);
3772 if(!is_type_pointer(type)) {
3773 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
3777 pointer_type_t *pointer_type = &type->pointer;
3778 type_t *result_type = pointer_type->points_to;
3780 result_type = automatic_type_conversion(result_type);
3781 expression->expression.datatype = result_type;
3784 static void semantic_take_addr(unary_expression_t *expression)
3786 expression_t *value = expression->value;
3787 value->base.datatype = revert_automatic_type_conversion(value);
3789 type_t *orig_type = value->base.datatype;
3790 if(orig_type == NULL)
3793 if(value->kind == EXPR_REFERENCE) {
3794 reference_expression_t *reference = (reference_expression_t*) value;
3795 declaration_t *declaration = reference->declaration;
3796 if(declaration != NULL) {
3797 declaration->address_taken = 1;
3801 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3804 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3805 static expression_t *parse_##unexpression_type(unsigned precedence) \
3809 expression_t *unary_expression \
3810 = allocate_expression_zero(unexpression_type); \
3811 unary_expression->unary.value = parse_sub_expression(precedence); \
3813 sfunc(&unary_expression->unary); \
3815 return unary_expression; \
3818 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
3819 semantic_unexpr_arithmetic)
3820 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
3821 semantic_unexpr_arithmetic)
3822 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
3823 semantic_unexpr_scalar)
3824 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
3825 semantic_dereference)
3826 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
3828 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
3829 semantic_unexpr_integer)
3830 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
3832 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
3835 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3837 static expression_t *parse_##unexpression_type(unsigned precedence, \
3838 expression_t *left) \
3840 (void) precedence; \
3843 expression_t *unary_expression \
3844 = allocate_expression_zero(unexpression_type); \
3845 unary_expression->unary.value = left; \
3847 sfunc(&unary_expression->unary); \
3849 return unary_expression; \
3852 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
3853 EXPR_UNARY_POSTFIX_INCREMENT,
3855 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
3856 EXPR_UNARY_POSTFIX_DECREMENT,
3859 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3861 /* TODO: handle complex + imaginary types */
3863 /* § 6.3.1.8 Usual arithmetic conversions */
3864 if(type_left == type_long_double || type_right == type_long_double) {
3865 return type_long_double;
3866 } else if(type_left == type_double || type_right == type_double) {
3868 } else if(type_left == type_float || type_right == type_float) {
3872 type_right = promote_integer(type_right);
3873 type_left = promote_integer(type_left);
3875 if(type_left == type_right)
3878 bool signed_left = is_type_signed(type_left);
3879 bool signed_right = is_type_signed(type_right);
3880 int rank_left = get_rank(type_left);
3881 int rank_right = get_rank(type_right);
3882 if(rank_left < rank_right) {
3883 if(signed_left == signed_right || !signed_right) {
3889 if(signed_left == signed_right || !signed_left) {
3897 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3899 expression_t *left = expression->left;
3900 expression_t *right = expression->right;
3901 type_t *orig_type_left = left->base.datatype;
3902 type_t *orig_type_right = right->base.datatype;
3904 if(orig_type_left == NULL || orig_type_right == NULL)
3907 type_t *type_left = skip_typeref(orig_type_left);
3908 type_t *type_right = skip_typeref(orig_type_right);
3910 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3911 /* TODO: improve error message */
3912 errorf(HERE, "operation needs arithmetic types");
3916 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3917 expression->left = create_implicit_cast(left, arithmetic_type);
3918 expression->right = create_implicit_cast(right, arithmetic_type);
3919 expression->expression.datatype = arithmetic_type;
3922 static void semantic_shift_op(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_integer(type_left) || !is_type_integer(type_right)) {
3936 /* TODO: improve error message */
3937 errorf(HERE, "operation needs integer types");
3941 type_left = promote_integer(type_left);
3942 type_right = promote_integer(type_right);
3944 expression->left = create_implicit_cast(left, type_left);
3945 expression->right = create_implicit_cast(right, type_right);
3946 expression->expression.datatype = type_left;
3949 static void semantic_add(binary_expression_t *expression)
3951 expression_t *left = expression->left;
3952 expression_t *right = expression->right;
3953 type_t *orig_type_left = left->base.datatype;
3954 type_t *orig_type_right = right->base.datatype;
3956 if(orig_type_left == NULL || orig_type_right == NULL)
3959 type_t *type_left = skip_typeref(orig_type_left);
3960 type_t *type_right = skip_typeref(orig_type_right);
3963 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3964 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3965 expression->left = create_implicit_cast(left, arithmetic_type);
3966 expression->right = create_implicit_cast(right, arithmetic_type);
3967 expression->expression.datatype = arithmetic_type;
3969 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3970 expression->expression.datatype = type_left;
3971 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3972 expression->expression.datatype = type_right;
3974 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
3978 static void semantic_sub(binary_expression_t *expression)
3980 expression_t *left = expression->left;
3981 expression_t *right = expression->right;
3982 type_t *orig_type_left = left->base.datatype;
3983 type_t *orig_type_right = right->base.datatype;
3985 if(orig_type_left == NULL || orig_type_right == NULL)
3988 type_t *type_left = skip_typeref(orig_type_left);
3989 type_t *type_right = skip_typeref(orig_type_right);
3992 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3993 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3994 expression->left = create_implicit_cast(left, arithmetic_type);
3995 expression->right = create_implicit_cast(right, arithmetic_type);
3996 expression->expression.datatype = arithmetic_type;
3998 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3999 expression->expression.datatype = type_left;
4000 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4001 if(!pointers_compatible(type_left, type_right)) {
4002 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4004 expression->expression.datatype = type_ptrdiff_t;
4007 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4011 static void semantic_comparison(binary_expression_t *expression)
4013 expression_t *left = expression->left;
4014 expression_t *right = expression->right;
4015 type_t *orig_type_left = left->base.datatype;
4016 type_t *orig_type_right = right->base.datatype;
4018 if(orig_type_left == NULL || orig_type_right == NULL)
4021 type_t *type_left = skip_typeref(orig_type_left);
4022 type_t *type_right = skip_typeref(orig_type_right);
4024 /* TODO non-arithmetic types */
4025 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4026 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4027 expression->left = create_implicit_cast(left, arithmetic_type);
4028 expression->right = create_implicit_cast(right, arithmetic_type);
4029 expression->expression.datatype = arithmetic_type;
4030 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4031 /* TODO check compatibility */
4032 } else if (is_type_pointer(type_left)) {
4033 expression->right = create_implicit_cast(right, type_left);
4034 } else if (is_type_pointer(type_right)) {
4035 expression->left = create_implicit_cast(left, type_right);
4037 type_error_incompatible("invalid operands in comparison",
4038 token.source_position, type_left, type_right);
4040 expression->expression.datatype = type_int;
4043 static void semantic_arithmetic_assign(binary_expression_t *expression)
4045 expression_t *left = expression->left;
4046 expression_t *right = expression->right;
4047 type_t *orig_type_left = left->base.datatype;
4048 type_t *orig_type_right = right->base.datatype;
4050 if(orig_type_left == NULL || orig_type_right == NULL)
4053 type_t *type_left = skip_typeref(orig_type_left);
4054 type_t *type_right = skip_typeref(orig_type_right);
4056 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4057 /* TODO: improve error message */
4058 errorf(HERE, "operation needs arithmetic types");
4062 /* combined instructions are tricky. We can't create an implicit cast on
4063 * the left side, because we need the uncasted form for the store.
4064 * The ast2firm pass has to know that left_type must be right_type
4065 * for the arithmeitc operation and create a cast by itself */
4066 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4067 expression->right = create_implicit_cast(right, arithmetic_type);
4068 expression->expression.datatype = type_left;
4071 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4073 expression_t *left = expression->left;
4074 expression_t *right = expression->right;
4075 type_t *orig_type_left = left->base.datatype;
4076 type_t *orig_type_right = right->base.datatype;
4078 if(orig_type_left == NULL || orig_type_right == NULL)
4081 type_t *type_left = skip_typeref(orig_type_left);
4082 type_t *type_right = skip_typeref(orig_type_right);
4084 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4085 /* combined instructions are tricky. We can't create an implicit cast on
4086 * the left side, because we need the uncasted form for the store.
4087 * The ast2firm pass has to know that left_type must be right_type
4088 * for the arithmeitc operation and create a cast by itself */
4089 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4090 expression->right = create_implicit_cast(right, arithmetic_type);
4091 expression->expression.datatype = type_left;
4092 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4093 expression->expression.datatype = type_left;
4095 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4100 static void semantic_logical_op(binary_expression_t *expression)
4102 expression_t *left = expression->left;
4103 expression_t *right = expression->right;
4104 type_t *orig_type_left = left->base.datatype;
4105 type_t *orig_type_right = right->base.datatype;
4107 if(orig_type_left == NULL || orig_type_right == NULL)
4110 type_t *type_left = skip_typeref(orig_type_left);
4111 type_t *type_right = skip_typeref(orig_type_right);
4113 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4114 /* TODO: improve error message */
4115 errorf(HERE, "operation needs scalar types");
4119 expression->expression.datatype = type_int;
4122 static bool has_const_fields(type_t *type)
4129 static void semantic_binexpr_assign(binary_expression_t *expression)
4131 expression_t *left = expression->left;
4132 type_t *orig_type_left = left->base.datatype;
4134 if(orig_type_left == NULL)
4137 type_t *type_left = revert_automatic_type_conversion(left);
4138 type_left = skip_typeref(orig_type_left);
4140 /* must be a modifiable lvalue */
4141 if (is_type_array(type_left)) {
4142 errorf(HERE, "cannot assign to arrays ('%E')", left);
4145 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4146 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left, orig_type_left);
4149 if(is_type_incomplete(type_left)) {
4150 errorf(HERE, "left-hand side of assignment '%E' has incomplete type '%T'", left, orig_type_left);
4153 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4154 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields", left, orig_type_left);
4158 semantic_assign(orig_type_left, &expression->right, "assignment");
4160 expression->expression.datatype = orig_type_left;
4163 static void semantic_comma(binary_expression_t *expression)
4165 expression->expression.datatype = expression->right->base.datatype;
4168 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4169 static expression_t *parse_##binexpression_type(unsigned precedence, \
4170 expression_t *left) \
4174 expression_t *right = parse_sub_expression(precedence + lr); \
4176 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4177 binexpr->binary.left = left; \
4178 binexpr->binary.right = right; \
4179 sfunc(&binexpr->binary); \
4184 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4185 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4186 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4187 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4188 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4189 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4190 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4191 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4192 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4194 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4195 semantic_comparison, 1)
4196 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4197 semantic_comparison, 1)
4198 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4199 semantic_comparison, 1)
4200 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4201 semantic_comparison, 1)
4203 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4204 semantic_binexpr_arithmetic, 1)
4205 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4206 semantic_binexpr_arithmetic, 1)
4207 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4208 semantic_binexpr_arithmetic, 1)
4209 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4210 semantic_logical_op, 1)
4211 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4212 semantic_logical_op, 1)
4213 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4214 semantic_shift_op, 1)
4215 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4216 semantic_shift_op, 1)
4217 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4218 semantic_arithmetic_addsubb_assign, 0)
4219 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4220 semantic_arithmetic_addsubb_assign, 0)
4221 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4222 semantic_arithmetic_assign, 0)
4223 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4224 semantic_arithmetic_assign, 0)
4225 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4226 semantic_arithmetic_assign, 0)
4227 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4228 semantic_arithmetic_assign, 0)
4229 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4230 semantic_arithmetic_assign, 0)
4231 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4232 semantic_arithmetic_assign, 0)
4233 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4234 semantic_arithmetic_assign, 0)
4235 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4236 semantic_arithmetic_assign, 0)
4238 static expression_t *parse_sub_expression(unsigned precedence)
4240 if(token.type < 0) {
4241 return expected_expression_error();
4244 expression_parser_function_t *parser
4245 = &expression_parsers[token.type];
4246 source_position_t source_position = token.source_position;
4249 if(parser->parser != NULL) {
4250 left = parser->parser(parser->precedence);
4252 left = parse_primary_expression();
4254 assert(left != NULL);
4255 left->base.source_position = source_position;
4258 if(token.type < 0) {
4259 return expected_expression_error();
4262 parser = &expression_parsers[token.type];
4263 if(parser->infix_parser == NULL)
4265 if(parser->infix_precedence < precedence)
4268 left = parser->infix_parser(parser->infix_precedence, left);
4270 assert(left != NULL);
4271 assert(left->kind != EXPR_UNKNOWN);
4272 left->base.source_position = source_position;
4278 static expression_t *parse_expression(void)
4280 return parse_sub_expression(1);
4285 static void register_expression_parser(parse_expression_function parser,
4286 int token_type, unsigned precedence)
4288 expression_parser_function_t *entry = &expression_parsers[token_type];
4290 if(entry->parser != NULL) {
4291 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4292 panic("trying to register multiple expression parsers for a token");
4294 entry->parser = parser;
4295 entry->precedence = precedence;
4298 static void register_infix_parser(parse_expression_infix_function parser,
4299 int token_type, unsigned precedence)
4301 expression_parser_function_t *entry = &expression_parsers[token_type];
4303 if(entry->infix_parser != NULL) {
4304 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4305 panic("trying to register multiple infix expression parsers for a "
4308 entry->infix_parser = parser;
4309 entry->infix_precedence = precedence;
4312 static void init_expression_parsers(void)
4314 memset(&expression_parsers, 0, sizeof(expression_parsers));
4316 register_infix_parser(parse_array_expression, '[', 30);
4317 register_infix_parser(parse_call_expression, '(', 30);
4318 register_infix_parser(parse_select_expression, '.', 30);
4319 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4320 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4322 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4325 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4326 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4327 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4328 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4329 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4330 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4331 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4332 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4333 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4334 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4335 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4336 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4337 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4338 T_EXCLAMATIONMARKEQUAL, 13);
4339 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4340 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4341 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4342 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4343 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4344 register_infix_parser(parse_conditional_expression, '?', 7);
4345 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4346 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4347 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4348 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4349 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4350 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4351 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4352 T_LESSLESSEQUAL, 2);
4353 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4354 T_GREATERGREATEREQUAL, 2);
4355 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4357 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4359 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4362 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4364 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4365 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4366 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4367 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4368 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4369 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4370 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4372 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4374 register_expression_parser(parse_sizeof, T_sizeof, 25);
4375 register_expression_parser(parse_extension, T___extension__, 25);
4376 register_expression_parser(parse_builtin_classify_type,
4377 T___builtin_classify_type, 25);
4380 static asm_constraint_t *parse_asm_constraints(void)
4382 asm_constraint_t *result = NULL;
4383 asm_constraint_t *last = NULL;
4385 while(token.type == T_STRING_LITERAL || token.type == '[') {
4386 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4387 memset(constraint, 0, sizeof(constraint[0]));
4389 if(token.type == '[') {
4391 if(token.type != T_IDENTIFIER) {
4392 parse_error_expected("while parsing asm constraint",
4396 constraint->symbol = token.v.symbol;
4401 constraint->constraints = parse_string_literals();
4403 constraint->expression = parse_expression();
4407 last->next = constraint;
4409 result = constraint;
4413 if(token.type != ',')
4421 static asm_clobber_t *parse_asm_clobbers(void)
4423 asm_clobber_t *result = NULL;
4424 asm_clobber_t *last = NULL;
4426 while(token.type == T_STRING_LITERAL) {
4427 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4428 clobber->clobber = parse_string_literals();
4431 last->next = clobber;
4437 if(token.type != ',')
4445 static statement_t *parse_asm_statement(void)
4449 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4450 statement->base.source_position = token.source_position;
4452 asm_statement_t *asm_statement = &statement->asms;
4454 if(token.type == T_volatile) {
4456 asm_statement->is_volatile = true;
4460 asm_statement->asm_text = parse_string_literals();
4462 if(token.type != ':')
4466 asm_statement->inputs = parse_asm_constraints();
4467 if(token.type != ':')
4471 asm_statement->outputs = parse_asm_constraints();
4472 if(token.type != ':')
4476 asm_statement->clobbers = parse_asm_clobbers();
4484 static statement_t *parse_case_statement(void)
4488 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4490 statement->base.source_position = token.source_position;
4491 statement->case_label.expression = parse_expression();
4494 statement->case_label.label_statement = parse_statement();
4499 static statement_t *parse_default_statement(void)
4503 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4505 statement->base.source_position = token.source_position;
4508 statement->label.label_statement = parse_statement();
4513 static declaration_t *get_label(symbol_t *symbol)
4515 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4516 assert(current_function != NULL);
4517 /* if we found a label in the same function, then we already created the
4519 if(candidate != NULL
4520 && candidate->parent_context == ¤t_function->context) {
4524 /* otherwise we need to create a new one */
4525 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4526 declaration->namespc = NAMESPACE_LABEL;
4527 declaration->symbol = symbol;
4529 label_push(declaration);
4534 static statement_t *parse_label_statement(void)
4536 assert(token.type == T_IDENTIFIER);
4537 symbol_t *symbol = token.v.symbol;
4540 declaration_t *label = get_label(symbol);
4542 /* if source position is already set then the label is defined twice,
4543 * otherwise it was just mentioned in a goto so far */
4544 if(label->source_position.input_name != NULL) {
4545 errorf(HERE, "duplicate label '%s'\n", symbol->string);
4546 errorf(label->source_position, "previous definition of '%s' was here\n", symbol->string);
4548 label->source_position = token.source_position;
4551 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4553 label_statement->statement.kind = STATEMENT_LABEL;
4554 label_statement->statement.source_position = token.source_position;
4555 label_statement->label = label;
4559 if(token.type == '}') {
4560 /* TODO only warn? */
4561 errorf(HERE, "label at end of compound statement");
4562 return (statement_t*) label_statement;
4564 label_statement->label_statement = parse_statement();
4567 return (statement_t*) label_statement;
4570 static statement_t *parse_if(void)
4574 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4575 statement->statement.kind = STATEMENT_IF;
4576 statement->statement.source_position = token.source_position;
4579 statement->condition = parse_expression();
4582 statement->true_statement = parse_statement();
4583 if(token.type == T_else) {
4585 statement->false_statement = parse_statement();
4588 return (statement_t*) statement;
4591 static statement_t *parse_switch(void)
4595 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4596 statement->statement.kind = STATEMENT_SWITCH;
4597 statement->statement.source_position = token.source_position;
4600 statement->expression = parse_expression();
4602 statement->body = parse_statement();
4604 return (statement_t*) statement;
4607 static statement_t *parse_while(void)
4611 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4612 statement->statement.kind = STATEMENT_WHILE;
4613 statement->statement.source_position = token.source_position;
4616 statement->condition = parse_expression();
4618 statement->body = parse_statement();
4620 return (statement_t*) statement;
4623 static statement_t *parse_do(void)
4627 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4628 statement->statement.kind = STATEMENT_DO_WHILE;
4629 statement->statement.source_position = token.source_position;
4631 statement->body = parse_statement();
4634 statement->condition = parse_expression();
4638 return (statement_t*) statement;
4641 static statement_t *parse_for(void)
4645 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4646 statement->statement.kind = STATEMENT_FOR;
4647 statement->statement.source_position = token.source_position;
4651 int top = environment_top();
4652 context_t *last_context = context;
4653 set_context(&statement->context);
4655 if(token.type != ';') {
4656 if(is_declaration_specifier(&token, false)) {
4657 parse_declaration(record_declaration);
4659 statement->initialisation = parse_expression();
4666 if(token.type != ';') {
4667 statement->condition = parse_expression();
4670 if(token.type != ')') {
4671 statement->step = parse_expression();
4674 statement->body = parse_statement();
4676 assert(context == &statement->context);
4677 set_context(last_context);
4678 environment_pop_to(top);
4680 return (statement_t*) statement;
4683 static statement_t *parse_goto(void)
4687 if(token.type != T_IDENTIFIER) {
4688 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4692 symbol_t *symbol = token.v.symbol;
4695 declaration_t *label = get_label(symbol);
4697 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4699 statement->statement.kind = STATEMENT_GOTO;
4700 statement->statement.source_position = token.source_position;
4702 statement->label = label;
4706 return (statement_t*) statement;
4709 static statement_t *parse_continue(void)
4714 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4715 statement->kind = STATEMENT_CONTINUE;
4716 statement->base.source_position = token.source_position;
4721 static statement_t *parse_break(void)
4726 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4727 statement->kind = STATEMENT_BREAK;
4728 statement->base.source_position = token.source_position;
4733 static statement_t *parse_return(void)
4737 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4739 statement->statement.kind = STATEMENT_RETURN;
4740 statement->statement.source_position = token.source_position;
4742 assert(is_type_function(current_function->type));
4743 function_type_t *function_type = ¤t_function->type->function;
4744 type_t *return_type = function_type->return_type;
4746 expression_t *return_value = NULL;
4747 if(token.type != ';') {
4748 return_value = parse_expression();
4752 if(return_type == NULL)
4753 return (statement_t*) statement;
4754 if(return_value != NULL && return_value->base.datatype == NULL)
4755 return (statement_t*) statement;
4757 return_type = skip_typeref(return_type);
4759 if(return_value != NULL) {
4760 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4762 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4763 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4764 warningf(HERE, "'return' with a value, in function returning void");
4765 return_value = NULL;
4767 if(return_type != NULL) {
4768 semantic_assign(return_type, &return_value, "'return'");
4772 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4773 warningf(HERE, "'return' without value, in function returning non-void");
4776 statement->return_value = return_value;
4778 return (statement_t*) statement;
4781 static statement_t *parse_declaration_statement(void)
4783 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4785 statement->base.source_position = token.source_position;
4787 declaration_t *before = last_declaration;
4788 parse_declaration(record_declaration);
4790 if(before == NULL) {
4791 statement->declaration.declarations_begin = context->declarations;
4793 statement->declaration.declarations_begin = before->next;
4795 statement->declaration.declarations_end = last_declaration;
4800 static statement_t *parse_expression_statement(void)
4802 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4804 statement->base.source_position = token.source_position;
4805 statement->expression.expression = parse_expression();
4812 static statement_t *parse_statement(void)
4814 statement_t *statement = NULL;
4816 /* declaration or statement */
4817 switch(token.type) {
4819 statement = parse_asm_statement();
4823 statement = parse_case_statement();
4827 statement = parse_default_statement();
4831 statement = parse_compound_statement();
4835 statement = parse_if();
4839 statement = parse_switch();
4843 statement = parse_while();
4847 statement = parse_do();
4851 statement = parse_for();
4855 statement = parse_goto();
4859 statement = parse_continue();
4863 statement = parse_break();
4867 statement = parse_return();
4876 if(look_ahead(1)->type == ':') {
4877 statement = parse_label_statement();
4881 if(is_typedef_symbol(token.v.symbol)) {
4882 statement = parse_declaration_statement();
4886 statement = parse_expression_statement();
4889 case T___extension__:
4890 /* this can be a prefix to a declaration or an expression statement */
4891 /* we simply eat it now and parse the rest with tail recursion */
4894 } while(token.type == T___extension__);
4895 statement = parse_statement();
4899 statement = parse_declaration_statement();
4903 statement = parse_expression_statement();
4907 assert(statement == NULL
4908 || statement->base.source_position.input_name != NULL);
4913 static statement_t *parse_compound_statement(void)
4915 compound_statement_t *compound_statement
4916 = allocate_ast_zero(sizeof(compound_statement[0]));
4917 compound_statement->statement.kind = STATEMENT_COMPOUND;
4918 compound_statement->statement.source_position = token.source_position;
4922 int top = environment_top();
4923 context_t *last_context = context;
4924 set_context(&compound_statement->context);
4926 statement_t *last_statement = NULL;
4928 while(token.type != '}' && token.type != T_EOF) {
4929 statement_t *statement = parse_statement();
4930 if(statement == NULL)
4933 if(last_statement != NULL) {
4934 last_statement->base.next = statement;
4936 compound_statement->statements = statement;
4939 while(statement->base.next != NULL)
4940 statement = statement->base.next;
4942 last_statement = statement;
4945 if(token.type != '}') {
4946 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
4950 assert(context == &compound_statement->context);
4951 set_context(last_context);
4952 environment_pop_to(top);
4954 return (statement_t*) compound_statement;
4957 static void initialize_builtins(void)
4959 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
4960 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
4961 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
4962 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
4963 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
4964 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
4965 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
4966 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
4968 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
4969 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
4970 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
4971 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
4974 static translation_unit_t *parse_translation_unit(void)
4976 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4978 assert(global_context == NULL);
4979 global_context = &unit->context;
4981 assert(context == NULL);
4982 set_context(&unit->context);
4984 initialize_builtins();
4986 while(token.type != T_EOF) {
4987 parse_external_declaration();
4990 assert(context == &unit->context);
4992 last_declaration = NULL;
4994 assert(global_context == &unit->context);
4995 global_context = NULL;
5000 translation_unit_t *parse(void)
5002 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5003 label_stack = NEW_ARR_F(stack_entry_t, 0);
5004 found_error = false;
5006 type_set_output(stderr);
5007 ast_set_output(stderr);
5009 lookahead_bufpos = 0;
5010 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5013 translation_unit_t *unit = parse_translation_unit();
5015 DEL_ARR_F(environment_stack);
5016 DEL_ARR_F(label_stack);
5024 void init_parser(void)
5026 init_expression_parsers();
5027 obstack_init(&temp_obst);
5029 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5030 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5033 void exit_parser(void)
5035 obstack_free(&temp_obst, NULL);