7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
23 #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 scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(void);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(*allocate_declaration_zero()));
144 declaration->type = type_error_type;
149 * Returns the size of a statement node.
151 * @param kind the statement kind
153 static size_t get_statement_struct_size(statement_kind_t kind)
155 static const size_t sizes[] = {
156 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
157 [STATEMENT_RETURN] = sizeof(return_statement_t),
158 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
159 [STATEMENT_IF] = sizeof(if_statement_t),
160 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
161 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
162 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
163 [STATEMENT_BREAK] = sizeof(statement_base_t),
164 [STATEMENT_GOTO] = sizeof(goto_statement_t),
165 [STATEMENT_LABEL] = sizeof(label_statement_t),
166 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
167 [STATEMENT_WHILE] = sizeof(while_statement_t),
168 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
169 [STATEMENT_FOR] = sizeof(for_statement_t),
170 [STATEMENT_ASM] = sizeof(asm_statement_t)
172 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
173 assert(sizes[kind] != 0);
178 * Allocate a statement node of given kind and initialize all
181 static statement_t *allocate_statement_zero(statement_kind_t kind)
183 size_t size = get_statement_struct_size(kind);
184 statement_t *res = allocate_ast_zero(size);
186 res->base.kind = kind;
191 * Returns the size of an expression node.
193 * @param kind the expression kind
195 static size_t get_expression_struct_size(expression_kind_t kind)
197 static const size_t sizes[] = {
198 [EXPR_INVALID] = sizeof(expression_base_t),
199 [EXPR_REFERENCE] = sizeof(reference_expression_t),
200 [EXPR_CONST] = sizeof(const_expression_t),
201 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
202 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
203 [EXPR_CALL] = sizeof(call_expression_t),
204 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
205 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
206 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
207 [EXPR_SELECT] = sizeof(select_expression_t),
208 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
209 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
210 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
211 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
212 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
213 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
214 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
215 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
216 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
217 [EXPR_VA_START] = sizeof(va_start_expression_t),
218 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
219 [EXPR_STATEMENT] = sizeof(statement_expression_t),
221 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
222 return sizes[EXPR_UNARY_FIRST];
224 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
225 return sizes[EXPR_BINARY_FIRST];
227 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
228 assert(sizes[kind] != 0);
233 * Allocate an expression node of given kind and initialize all
236 static expression_t *allocate_expression_zero(expression_kind_t kind)
238 size_t size = get_expression_struct_size(kind);
239 expression_t *res = allocate_ast_zero(size);
241 res->base.kind = kind;
242 res->base.datatype = type_error_type;
247 * Returns the size of a type node.
249 * @param kind the type kind
251 static size_t get_type_struct_size(type_kind_t kind)
253 static const size_t sizes[] = {
254 [TYPE_ATOMIC] = sizeof(atomic_type_t),
255 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
256 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
257 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
258 [TYPE_ENUM] = sizeof(enum_type_t),
259 [TYPE_FUNCTION] = sizeof(function_type_t),
260 [TYPE_POINTER] = sizeof(pointer_type_t),
261 [TYPE_ARRAY] = sizeof(array_type_t),
262 [TYPE_BUILTIN] = sizeof(builtin_type_t),
263 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
264 [TYPE_TYPEOF] = sizeof(typeof_type_t),
266 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
267 assert(kind <= TYPE_TYPEOF);
268 assert(sizes[kind] != 0);
273 * Allocate a type node of given kind and initialize all
276 static type_t *allocate_type_zero(type_kind_t kind)
278 size_t size = get_type_struct_size(kind);
279 type_t *res = obstack_alloc(type_obst, size);
280 memset(res, 0, size);
282 res->base.kind = kind;
287 * Returns the size of an initializer node.
289 * @param kind the initializer kind
291 static size_t get_initializer_size(initializer_kind_t kind)
293 static const size_t sizes[] = {
294 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
295 [INITIALIZER_STRING] = sizeof(initializer_string_t),
296 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
297 [INITIALIZER_LIST] = sizeof(initializer_list_t)
299 assert(kind < sizeof(sizes) / sizeof(*sizes));
300 assert(sizes[kind] != 0);
305 * Allocate an initializer node of given kind and initialize all
308 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
310 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
317 * Free a type from the type obstack.
319 static void free_type(void *type)
321 obstack_free(type_obst, type);
325 * Returns the index of the top element of the environment stack.
327 static size_t environment_top(void)
329 return ARR_LEN(environment_stack);
333 * Returns the index of the top element of the label stack.
335 static size_t label_top(void)
337 return ARR_LEN(label_stack);
342 * Return the next token.
344 static inline void next_token(void)
346 token = lookahead_buffer[lookahead_bufpos];
347 lookahead_buffer[lookahead_bufpos] = lexer_token;
350 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
353 print_token(stderr, &token);
354 fprintf(stderr, "\n");
359 * Return the next token with a given lookahead.
361 static inline const token_t *look_ahead(int num)
363 assert(num > 0 && num <= MAX_LOOKAHEAD);
364 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
365 return &lookahead_buffer[pos];
368 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
371 * Report a parse error because an expected token was not found.
373 static void parse_error_expected(const char *message, ...)
375 if(message != NULL) {
376 errorf(HERE, "%s", message);
379 va_start(ap, message);
380 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
385 * Report a type error.
387 static void type_error(const char *msg, const source_position_t source_position,
390 errorf(source_position, "%s, but found type '%T'", msg, type);
394 * Report an incompatible type.
396 static void type_error_incompatible(const char *msg,
397 const source_position_t source_position, type_t *type1, type_t *type2)
399 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
403 * Eat an complete block, ie. '{ ... }'.
405 static void eat_block(void)
407 if(token.type == '{')
410 while(token.type != '}') {
411 if(token.type == T_EOF)
413 if(token.type == '{') {
423 * Eat a statement until an ';' token.
425 static void eat_statement(void)
427 while(token.type != ';') {
428 if(token.type == T_EOF)
430 if(token.type == '}')
432 if(token.type == '{') {
442 * Eat a parenthesed term, ie. '( ... )'.
444 static void eat_paren(void)
446 if(token.type == '(')
449 while(token.type != ')') {
450 if(token.type == T_EOF)
452 if(token.type == ')' || token.type == ';' || token.type == '}') {
455 if(token.type == '(') {
459 if(token.type == '{') {
468 #define expect(expected) \
469 if(UNLIKELY(token.type != (expected))) { \
470 parse_error_expected(NULL, (expected), 0); \
476 #define expect_block(expected) \
477 if(UNLIKELY(token.type != (expected))) { \
478 parse_error_expected(NULL, (expected), 0); \
484 #define expect_void(expected) \
485 if(UNLIKELY(token.type != (expected))) { \
486 parse_error_expected(NULL, (expected), 0); \
492 static void set_scope(scope_t *new_scope)
496 last_declaration = new_scope->declarations;
497 if(last_declaration != NULL) {
498 while(last_declaration->next != NULL) {
499 last_declaration = last_declaration->next;
505 * Search a symbol in a given namespace and returns its declaration or
506 * NULL if this symbol was not found.
508 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
510 declaration_t *declaration = symbol->declaration;
511 for( ; declaration != NULL; declaration = declaration->symbol_next) {
512 if(declaration->namespc == namespc)
520 * pushs an environment_entry on the environment stack and links the
521 * corresponding symbol to the new entry
523 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
525 symbol_t *symbol = declaration->symbol;
526 namespace_t namespc = (namespace_t)declaration->namespc;
528 /* remember old declaration */
530 entry.symbol = symbol;
531 entry.old_declaration = symbol->declaration;
532 entry.namespc = (unsigned short) namespc;
533 ARR_APP1(stack_entry_t, *stack_ptr, entry);
535 /* replace/add declaration into declaration list of the symbol */
536 if(symbol->declaration == NULL) {
537 symbol->declaration = declaration;
539 declaration_t *iter_last = NULL;
540 declaration_t *iter = symbol->declaration;
541 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
542 /* replace an entry? */
543 if(iter->namespc == namespc) {
544 if(iter_last == NULL) {
545 symbol->declaration = declaration;
547 iter_last->symbol_next = declaration;
549 declaration->symbol_next = iter->symbol_next;
554 assert(iter_last->symbol_next == NULL);
555 iter_last->symbol_next = declaration;
560 static void environment_push(declaration_t *declaration)
562 assert(declaration->source_position.input_name != NULL);
563 assert(declaration->parent_scope != NULL);
564 stack_push(&environment_stack, declaration);
567 static void label_push(declaration_t *declaration)
569 declaration->parent_scope = ¤t_function->scope;
570 stack_push(&label_stack, declaration);
574 * pops symbols from the environment stack until @p new_top is the top element
576 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
578 stack_entry_t *stack = *stack_ptr;
579 size_t top = ARR_LEN(stack);
582 assert(new_top <= top);
586 for(i = top; i > new_top; --i) {
587 stack_entry_t *entry = &stack[i - 1];
589 declaration_t *old_declaration = entry->old_declaration;
590 symbol_t *symbol = entry->symbol;
591 namespace_t namespc = (namespace_t)entry->namespc;
593 /* replace/remove declaration */
594 declaration_t *declaration = symbol->declaration;
595 assert(declaration != NULL);
596 if(declaration->namespc == namespc) {
597 if(old_declaration == NULL) {
598 symbol->declaration = declaration->symbol_next;
600 symbol->declaration = old_declaration;
603 declaration_t *iter_last = declaration;
604 declaration_t *iter = declaration->symbol_next;
605 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
606 /* replace an entry? */
607 if(iter->namespc == namespc) {
608 assert(iter_last != NULL);
609 iter_last->symbol_next = old_declaration;
610 old_declaration->symbol_next = iter->symbol_next;
614 assert(iter != NULL);
618 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
621 static void environment_pop_to(size_t new_top)
623 stack_pop_to(&environment_stack, new_top);
626 static void label_pop_to(size_t new_top)
628 stack_pop_to(&label_stack, new_top);
632 static int get_rank(const type_t *type)
634 assert(!is_typeref(type));
635 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
636 * and esp. footnote 108). However we can't fold constants (yet), so we
637 * can't decide whether unsigned int is possible, while int always works.
638 * (unsigned int would be preferable when possible... for stuff like
639 * struct { enum { ... } bla : 4; } ) */
640 if(type->kind == TYPE_ENUM)
641 return ATOMIC_TYPE_INT;
643 assert(type->kind == TYPE_ATOMIC);
644 return type->atomic.akind;
647 static type_t *promote_integer(type_t *type)
649 if(type->kind == TYPE_BITFIELD)
650 type = type->bitfield.base;
652 if(get_rank(type) < ATOMIC_TYPE_INT)
659 * Create a cast expression.
661 * @param expression the expression to cast
662 * @param dest_type the destination type
664 static expression_t *create_cast_expression(expression_t *expression,
667 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
669 cast->unary.value = expression;
670 cast->base.datatype = dest_type;
676 * Check if a given expression represents the 0 pointer constant.
678 static bool is_null_pointer_constant(const expression_t *expression)
680 /* skip void* cast */
681 if(expression->kind == EXPR_UNARY_CAST
682 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
683 expression = expression->unary.value;
686 /* TODO: not correct yet, should be any constant integer expression
687 * which evaluates to 0 */
688 if (expression->kind != EXPR_CONST)
691 type_t *const type = skip_typeref(expression->base.datatype);
692 if (!is_type_integer(type))
695 return expression->conste.v.int_value == 0;
699 * Create an implicit cast expression.
701 * @param expression the expression to cast
702 * @param dest_type the destination type
704 static expression_t *create_implicit_cast(expression_t *expression,
707 type_t *const source_type = expression->base.datatype;
709 if (source_type == dest_type)
712 return create_cast_expression(expression, dest_type);
715 /** Implements the rules from § 6.5.16.1 */
716 static type_t *semantic_assign(type_t *orig_type_left,
717 const expression_t *const right,
720 type_t *const orig_type_right = right->base.datatype;
721 type_t *const type_left = skip_typeref(orig_type_left);
722 type_t *const type_right = skip_typeref(orig_type_right);
724 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
725 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
726 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
727 && is_type_pointer(type_right))) {
728 return orig_type_left;
731 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
732 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
733 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
735 /* the left type has all qualifiers from the right type */
736 unsigned missing_qualifiers
737 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
738 if(missing_qualifiers != 0) {
739 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
740 return orig_type_left;
743 points_to_left = get_unqualified_type(points_to_left);
744 points_to_right = get_unqualified_type(points_to_right);
746 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
747 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
748 && !types_compatible(points_to_left, points_to_right)) {
752 return orig_type_left;
755 if (is_type_compound(type_left) && is_type_compound(type_right)) {
756 type_t *const unqual_type_left = get_unqualified_type(type_left);
757 type_t *const unqual_type_right = get_unqualified_type(type_right);
758 if (types_compatible(unqual_type_left, unqual_type_right)) {
759 return orig_type_left;
763 if (!is_type_valid(type_left))
766 if (!is_type_valid(type_right))
767 return orig_type_right;
772 static expression_t *parse_constant_expression(void)
774 /* start parsing at precedence 7 (conditional expression) */
775 expression_t *result = parse_sub_expression(7);
777 if(!is_constant_expression(result)) {
778 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
784 static expression_t *parse_assignment_expression(void)
786 /* start parsing at precedence 2 (assignment expression) */
787 return parse_sub_expression(2);
790 static type_t *make_global_typedef(const char *name, type_t *type)
792 symbol_t *const symbol = symbol_table_insert(name);
794 declaration_t *const declaration = allocate_declaration_zero();
795 declaration->namespc = NAMESPACE_NORMAL;
796 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
797 declaration->type = type;
798 declaration->symbol = symbol;
799 declaration->source_position = builtin_source_position;
801 record_declaration(declaration);
803 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
804 typedef_type->typedeft.declaration = declaration;
809 static string_t parse_string_literals(void)
811 assert(token.type == T_STRING_LITERAL);
812 string_t result = token.v.string;
816 while (token.type == T_STRING_LITERAL) {
817 result = concat_strings(&result, &token.v.string);
824 static void parse_attributes(void)
828 case T___attribute__: {
836 errorf(HERE, "EOF while parsing attribute");
855 if(token.type != T_STRING_LITERAL) {
856 parse_error_expected("while parsing assembler attribute",
861 parse_string_literals();
866 goto attributes_finished;
875 static designator_t *parse_designation(void)
877 if(token.type != '[' && token.type != '.')
880 designator_t *result = NULL;
881 designator_t *last = NULL;
884 designator_t *designator;
887 designator = allocate_ast_zero(sizeof(designator[0]));
889 designator->array_access = parse_constant_expression();
893 designator = allocate_ast_zero(sizeof(designator[0]));
895 if(token.type != T_IDENTIFIER) {
896 parse_error_expected("while parsing designator",
900 designator->symbol = token.v.symbol;
908 assert(designator != NULL);
910 last->next = designator;
919 static initializer_t *initializer_from_string(array_type_t *type,
920 const string_t *const string)
922 /* TODO: check len vs. size of array type */
925 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
926 initializer->string.string = *string;
931 static initializer_t *initializer_from_wide_string(array_type_t *const type,
932 wide_string_t *const string)
934 /* TODO: check len vs. size of array type */
937 initializer_t *const initializer =
938 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
939 initializer->wide_string.string = *string;
944 static initializer_t *initializer_from_expression(type_t *type,
945 expression_t *expression)
947 /* TODO check that expression is a constant expression */
949 /* § 6.7.8.14/15 char array may be initialized by string literals */
950 type_t *const expr_type = expression->base.datatype;
951 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
952 array_type_t *const array_type = &type->array;
953 type_t *const element_type = skip_typeref(array_type->element_type);
955 if (element_type->kind == TYPE_ATOMIC) {
956 switch (expression->kind) {
957 case EXPR_STRING_LITERAL:
958 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
959 return initializer_from_string(array_type,
960 &expression->string.value);
963 case EXPR_WIDE_STRING_LITERAL: {
964 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
965 if (get_unqualified_type(element_type) == bare_wchar_type) {
966 return initializer_from_wide_string(array_type,
967 &expression->wide_string.value);
977 type_t *const res_type = semantic_assign(type, expression, "initializer");
978 if (res_type == NULL)
981 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
982 result->value.value = create_implicit_cast(expression, res_type);
987 static initializer_t *parse_sub_initializer(type_t *type,
988 expression_t *expression);
990 static initializer_t *parse_sub_initializer_elem(type_t *type)
992 if(token.type == '{') {
993 return parse_sub_initializer(type, NULL);
996 expression_t *expression = parse_assignment_expression();
997 return parse_sub_initializer(type, expression);
1000 static bool had_initializer_brace_warning;
1002 static void skip_designator(void)
1005 if(token.type == '.') {
1007 if(token.type == T_IDENTIFIER)
1009 } else if(token.type == '[') {
1011 parse_constant_expression();
1012 if(token.type == ']')
1020 static initializer_t *parse_sub_initializer(type_t *type,
1021 expression_t *expression)
1023 if(is_type_scalar(type)) {
1024 /* there might be extra {} hierarchies */
1025 if(token.type == '{') {
1027 if(!had_initializer_brace_warning) {
1028 warningf(HERE, "braces around scalar initializer");
1029 had_initializer_brace_warning = true;
1031 initializer_t *result = parse_sub_initializer(type, NULL);
1032 if(token.type == ',') {
1034 /* TODO: warn about excessive elements */
1040 if(expression == NULL) {
1041 expression = parse_assignment_expression();
1043 return initializer_from_expression(type, expression);
1046 /* does the expression match the currently looked at object to initialize */
1047 if(expression != NULL) {
1048 initializer_t *result = initializer_from_expression(type, expression);
1053 bool read_paren = false;
1054 if(token.type == '{') {
1059 /* descend into subtype */
1060 initializer_t *result = NULL;
1061 initializer_t **elems;
1062 if(is_type_array(type)) {
1063 if(token.type == '.') {
1065 "compound designator in initializer for array type '%T'",
1070 type_t *const element_type = skip_typeref(type->array.element_type);
1073 had_initializer_brace_warning = false;
1074 if(expression == NULL) {
1075 sub = parse_sub_initializer_elem(element_type);
1077 sub = parse_sub_initializer(element_type, expression);
1080 /* didn't match the subtypes -> try the parent type */
1082 assert(!read_paren);
1086 elems = NEW_ARR_F(initializer_t*, 0);
1087 ARR_APP1(initializer_t*, elems, sub);
1090 if(token.type == '}')
1093 if(token.type == '}')
1096 sub = parse_sub_initializer_elem(element_type);
1098 /* TODO error, do nicer cleanup */
1099 errorf(HERE, "member initializer didn't match");
1103 ARR_APP1(initializer_t*, elems, sub);
1106 assert(is_type_compound(type));
1107 scope_t *const scope = &type->compound.declaration->scope;
1109 if(token.type == '[') {
1111 "array designator in initializer for compound type '%T'",
1116 declaration_t *first = scope->declarations;
1119 type_t *first_type = first->type;
1120 first_type = skip_typeref(first_type);
1123 had_initializer_brace_warning = false;
1124 if(expression == NULL) {
1125 sub = parse_sub_initializer_elem(first_type);
1127 sub = parse_sub_initializer(first_type, expression);
1130 /* didn't match the subtypes -> try our parent type */
1132 assert(!read_paren);
1136 elems = NEW_ARR_F(initializer_t*, 0);
1137 ARR_APP1(initializer_t*, elems, sub);
1139 declaration_t *iter = first->next;
1140 for( ; iter != NULL; iter = iter->next) {
1141 if(iter->symbol == NULL)
1143 if(iter->namespc != NAMESPACE_NORMAL)
1146 if(token.type == '}')
1149 if(token.type == '}')
1152 type_t *iter_type = iter->type;
1153 iter_type = skip_typeref(iter_type);
1155 sub = parse_sub_initializer_elem(iter_type);
1157 /* TODO error, do nicer cleanup */
1158 errorf(HERE, "member initializer didn't match");
1162 ARR_APP1(initializer_t*, elems, sub);
1166 int len = ARR_LEN(elems);
1167 size_t elems_size = sizeof(initializer_t*) * len;
1169 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1171 init->initializer.kind = INITIALIZER_LIST;
1173 memcpy(init->initializers, elems, elems_size);
1176 result = (initializer_t*) init;
1179 if(token.type == ',')
1186 static initializer_t *parse_initializer(type_t *const orig_type)
1188 initializer_t *result;
1190 type_t *const type = skip_typeref(orig_type);
1192 if(token.type != '{') {
1193 expression_t *expression = parse_assignment_expression();
1194 initializer_t *initializer = initializer_from_expression(type, expression);
1195 if(initializer == NULL) {
1197 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1198 expression, expression->base.datatype, orig_type);
1203 if(is_type_scalar(type)) {
1207 expression_t *expression = parse_assignment_expression();
1208 result = initializer_from_expression(type, expression);
1210 if(token.type == ',')
1216 result = parse_sub_initializer(type, NULL);
1222 static declaration_t *append_declaration(declaration_t *declaration);
1224 static declaration_t *parse_compound_type_specifier(bool is_struct)
1232 symbol_t *symbol = NULL;
1233 declaration_t *declaration = NULL;
1235 if (token.type == T___attribute__) {
1240 if(token.type == T_IDENTIFIER) {
1241 symbol = token.v.symbol;
1245 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1247 declaration = get_declaration(symbol, NAMESPACE_UNION);
1249 } else if(token.type != '{') {
1251 parse_error_expected("while parsing struct type specifier",
1252 T_IDENTIFIER, '{', 0);
1254 parse_error_expected("while parsing union type specifier",
1255 T_IDENTIFIER, '{', 0);
1261 if(declaration == NULL) {
1262 declaration = allocate_declaration_zero();
1263 declaration->namespc =
1264 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1265 declaration->source_position = token.source_position;
1266 declaration->symbol = symbol;
1267 declaration->parent_scope = scope;
1268 if (symbol != NULL) {
1269 environment_push(declaration);
1271 append_declaration(declaration);
1274 if(token.type == '{') {
1275 if(declaration->init.is_defined) {
1276 assert(symbol != NULL);
1277 errorf(HERE, "multiple definition of '%s %Y'",
1278 is_struct ? "struct" : "union", symbol);
1279 declaration->scope.declarations = NULL;
1281 declaration->init.is_defined = true;
1283 int top = environment_top();
1284 scope_t *last_scope = scope;
1285 set_scope(&declaration->scope);
1287 parse_compound_type_entries();
1290 assert(scope == &declaration->scope);
1291 set_scope(last_scope);
1292 environment_pop_to(top);
1298 static void parse_enum_entries(type_t *const enum_type)
1302 if(token.type == '}') {
1304 errorf(HERE, "empty enum not allowed");
1309 if(token.type != T_IDENTIFIER) {
1310 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1315 declaration_t *const entry = allocate_declaration_zero();
1316 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1317 entry->type = enum_type;
1318 entry->symbol = token.v.symbol;
1319 entry->source_position = token.source_position;
1322 if(token.type == '=') {
1324 entry->init.enum_value = parse_constant_expression();
1329 record_declaration(entry);
1331 if(token.type != ',')
1334 } while(token.type != '}');
1339 static type_t *parse_enum_specifier(void)
1343 declaration_t *declaration;
1346 if(token.type == T_IDENTIFIER) {
1347 symbol = token.v.symbol;
1350 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1351 } else if(token.type != '{') {
1352 parse_error_expected("while parsing enum type specifier",
1353 T_IDENTIFIER, '{', 0);
1360 if(declaration == NULL) {
1361 declaration = allocate_declaration_zero();
1362 declaration->namespc = NAMESPACE_ENUM;
1363 declaration->source_position = token.source_position;
1364 declaration->symbol = symbol;
1365 declaration->parent_scope = scope;
1368 type_t *const type = allocate_type_zero(TYPE_ENUM);
1369 type->enumt.declaration = declaration;
1371 if(token.type == '{') {
1372 if(declaration->init.is_defined) {
1373 errorf(HERE, "multiple definitions of enum %Y", symbol);
1375 if (symbol != NULL) {
1376 environment_push(declaration);
1378 append_declaration(declaration);
1379 declaration->init.is_defined = 1;
1381 parse_enum_entries(type);
1389 * if a symbol is a typedef to another type, return true
1391 static bool is_typedef_symbol(symbol_t *symbol)
1393 const declaration_t *const declaration =
1394 get_declaration(symbol, NAMESPACE_NORMAL);
1396 declaration != NULL &&
1397 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1400 static type_t *parse_typeof(void)
1408 expression_t *expression = NULL;
1411 switch(token.type) {
1412 case T___extension__:
1413 /* this can be a prefix to a typename or an expression */
1414 /* we simply eat it now. */
1417 } while(token.type == T___extension__);
1421 if(is_typedef_symbol(token.v.symbol)) {
1422 type = parse_typename();
1424 expression = parse_expression();
1425 type = expression->base.datatype;
1430 type = parse_typename();
1434 expression = parse_expression();
1435 type = expression->base.datatype;
1441 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1442 typeof_type->typeoft.expression = expression;
1443 typeof_type->typeoft.typeof_type = type;
1449 SPECIFIER_SIGNED = 1 << 0,
1450 SPECIFIER_UNSIGNED = 1 << 1,
1451 SPECIFIER_LONG = 1 << 2,
1452 SPECIFIER_INT = 1 << 3,
1453 SPECIFIER_DOUBLE = 1 << 4,
1454 SPECIFIER_CHAR = 1 << 5,
1455 SPECIFIER_SHORT = 1 << 6,
1456 SPECIFIER_LONG_LONG = 1 << 7,
1457 SPECIFIER_FLOAT = 1 << 8,
1458 SPECIFIER_BOOL = 1 << 9,
1459 SPECIFIER_VOID = 1 << 10,
1460 #ifdef PROVIDE_COMPLEX
1461 SPECIFIER_COMPLEX = 1 << 11,
1462 SPECIFIER_IMAGINARY = 1 << 12,
1466 static type_t *create_builtin_type(symbol_t *const symbol,
1467 type_t *const real_type)
1469 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1470 type->builtin.symbol = symbol;
1471 type->builtin.real_type = real_type;
1473 type_t *result = typehash_insert(type);
1474 if (type != result) {
1481 static type_t *get_typedef_type(symbol_t *symbol)
1483 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1484 if(declaration == NULL
1485 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1488 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1489 type->typedeft.declaration = declaration;
1494 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1496 type_t *type = NULL;
1497 unsigned type_qualifiers = 0;
1498 unsigned type_specifiers = 0;
1501 specifiers->source_position = token.source_position;
1504 switch(token.type) {
1507 #define MATCH_STORAGE_CLASS(token, class) \
1509 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1510 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1512 specifiers->storage_class = class; \
1516 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1517 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1518 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1519 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1520 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1523 switch (specifiers->storage_class) {
1524 case STORAGE_CLASS_NONE:
1525 specifiers->storage_class = STORAGE_CLASS_THREAD;
1528 case STORAGE_CLASS_EXTERN:
1529 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1532 case STORAGE_CLASS_STATIC:
1533 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1537 errorf(HERE, "multiple storage classes in declaration specifiers");
1543 /* type qualifiers */
1544 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1546 type_qualifiers |= qualifier; \
1550 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1551 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1552 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1554 case T___extension__:
1559 /* type specifiers */
1560 #define MATCH_SPECIFIER(token, specifier, name) \
1563 if(type_specifiers & specifier) { \
1564 errorf(HERE, "multiple " name " type specifiers given"); \
1566 type_specifiers |= specifier; \
1570 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1571 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1572 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1573 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1574 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1575 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1576 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1577 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1578 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1579 #ifdef PROVIDE_COMPLEX
1580 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1581 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1584 /* only in microsoft mode */
1585 specifiers->decl_modifiers |= DM_FORCEINLINE;
1589 specifiers->is_inline = true;
1594 if(type_specifiers & SPECIFIER_LONG_LONG) {
1595 errorf(HERE, "multiple type specifiers given");
1596 } else if(type_specifiers & SPECIFIER_LONG) {
1597 type_specifiers |= SPECIFIER_LONG_LONG;
1599 type_specifiers |= SPECIFIER_LONG;
1603 /* TODO: if is_type_valid(type) for the following rules should issue
1606 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1608 type->compound.declaration = parse_compound_type_specifier(true);
1612 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1614 type->compound.declaration = parse_compound_type_specifier(false);
1618 type = parse_enum_specifier();
1621 type = parse_typeof();
1623 case T___builtin_va_list:
1624 type = duplicate_type(type_valist);
1628 case T___attribute__:
1633 case T_IDENTIFIER: {
1634 type_t *typedef_type = get_typedef_type(token.v.symbol);
1636 if(typedef_type == NULL)
1637 goto finish_specifiers;
1640 type = typedef_type;
1644 /* function specifier */
1646 goto finish_specifiers;
1653 atomic_type_kind_t atomic_type;
1655 /* match valid basic types */
1656 switch(type_specifiers) {
1657 case SPECIFIER_VOID:
1658 atomic_type = ATOMIC_TYPE_VOID;
1660 case SPECIFIER_CHAR:
1661 atomic_type = ATOMIC_TYPE_CHAR;
1663 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1664 atomic_type = ATOMIC_TYPE_SCHAR;
1666 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1667 atomic_type = ATOMIC_TYPE_UCHAR;
1669 case SPECIFIER_SHORT:
1670 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1671 case SPECIFIER_SHORT | SPECIFIER_INT:
1672 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1673 atomic_type = ATOMIC_TYPE_SHORT;
1675 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1676 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1677 atomic_type = ATOMIC_TYPE_USHORT;
1680 case SPECIFIER_SIGNED:
1681 case SPECIFIER_SIGNED | SPECIFIER_INT:
1682 atomic_type = ATOMIC_TYPE_INT;
1684 case SPECIFIER_UNSIGNED:
1685 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1686 atomic_type = ATOMIC_TYPE_UINT;
1688 case SPECIFIER_LONG:
1689 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1690 case SPECIFIER_LONG | SPECIFIER_INT:
1691 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1692 atomic_type = ATOMIC_TYPE_LONG;
1694 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1695 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1696 atomic_type = ATOMIC_TYPE_ULONG;
1698 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1699 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1700 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1701 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1703 atomic_type = ATOMIC_TYPE_LONGLONG;
1705 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1706 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1708 atomic_type = ATOMIC_TYPE_ULONGLONG;
1710 case SPECIFIER_FLOAT:
1711 atomic_type = ATOMIC_TYPE_FLOAT;
1713 case SPECIFIER_DOUBLE:
1714 atomic_type = ATOMIC_TYPE_DOUBLE;
1716 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1717 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1719 case SPECIFIER_BOOL:
1720 atomic_type = ATOMIC_TYPE_BOOL;
1722 #ifdef PROVIDE_COMPLEX
1723 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1724 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1726 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1727 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1729 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1730 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1732 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1733 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1735 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1736 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1738 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1739 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1743 /* invalid specifier combination, give an error message */
1744 if(type_specifiers == 0) {
1745 if (! strict_mode) {
1746 if (warning.implicit_int) {
1747 warningf(HERE, "no type specifiers in declaration, using 'int'");
1749 atomic_type = ATOMIC_TYPE_INT;
1752 errorf(HERE, "no type specifiers given in declaration");
1754 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1755 (type_specifiers & SPECIFIER_UNSIGNED)) {
1756 errorf(HERE, "signed and unsigned specifiers gives");
1757 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1758 errorf(HERE, "only integer types can be signed or unsigned");
1760 errorf(HERE, "multiple datatypes in declaration");
1762 atomic_type = ATOMIC_TYPE_INVALID;
1765 type = allocate_type_zero(TYPE_ATOMIC);
1766 type->atomic.akind = atomic_type;
1769 if(type_specifiers != 0) {
1770 errorf(HERE, "multiple datatypes in declaration");
1774 type->base.qualifiers = type_qualifiers;
1776 type_t *result = typehash_insert(type);
1777 if(newtype && result != type) {
1781 specifiers->type = result;
1784 static type_qualifiers_t parse_type_qualifiers(void)
1786 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1789 switch(token.type) {
1790 /* type qualifiers */
1791 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1792 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1793 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1796 return type_qualifiers;
1801 static declaration_t *parse_identifier_list(void)
1803 declaration_t *declarations = NULL;
1804 declaration_t *last_declaration = NULL;
1806 declaration_t *const declaration = allocate_declaration_zero();
1807 declaration->source_position = token.source_position;
1808 declaration->symbol = token.v.symbol;
1811 if(last_declaration != NULL) {
1812 last_declaration->next = declaration;
1814 declarations = declaration;
1816 last_declaration = declaration;
1818 if(token.type != ',')
1821 } while(token.type == T_IDENTIFIER);
1823 return declarations;
1826 static void semantic_parameter(declaration_t *declaration)
1828 /* TODO: improve error messages */
1830 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1831 errorf(HERE, "typedef not allowed in parameter list");
1832 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1833 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1834 errorf(HERE, "parameter may only have none or register storage class");
1837 type_t *const orig_type = declaration->type;
1838 type_t * type = skip_typeref(orig_type);
1840 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1841 * into a pointer. § 6.7.5.3 (7) */
1842 if (is_type_array(type)) {
1843 type_t *const element_type = type->array.element_type;
1845 type = make_pointer_type(element_type, type->base.qualifiers);
1847 declaration->type = type;
1850 if(is_type_incomplete(type)) {
1851 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1852 orig_type, declaration->symbol);
1856 static declaration_t *parse_parameter(void)
1858 declaration_specifiers_t specifiers;
1859 memset(&specifiers, 0, sizeof(specifiers));
1861 parse_declaration_specifiers(&specifiers);
1863 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1865 semantic_parameter(declaration);
1870 static declaration_t *parse_parameters(function_type_t *type)
1872 if(token.type == T_IDENTIFIER) {
1873 symbol_t *symbol = token.v.symbol;
1874 if(!is_typedef_symbol(symbol)) {
1875 type->kr_style_parameters = true;
1876 return parse_identifier_list();
1880 if(token.type == ')') {
1881 type->unspecified_parameters = 1;
1884 if(token.type == T_void && look_ahead(1)->type == ')') {
1889 declaration_t *declarations = NULL;
1890 declaration_t *declaration;
1891 declaration_t *last_declaration = NULL;
1892 function_parameter_t *parameter;
1893 function_parameter_t *last_parameter = NULL;
1896 switch(token.type) {
1900 return declarations;
1903 case T___extension__:
1905 declaration = parse_parameter();
1907 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1908 memset(parameter, 0, sizeof(parameter[0]));
1909 parameter->type = declaration->type;
1911 if(last_parameter != NULL) {
1912 last_declaration->next = declaration;
1913 last_parameter->next = parameter;
1915 type->parameters = parameter;
1916 declarations = declaration;
1918 last_parameter = parameter;
1919 last_declaration = declaration;
1923 return declarations;
1925 if(token.type != ',')
1926 return declarations;
1936 } construct_type_type_t;
1938 typedef struct construct_type_t construct_type_t;
1939 struct construct_type_t {
1940 construct_type_type_t type;
1941 construct_type_t *next;
1944 typedef struct parsed_pointer_t parsed_pointer_t;
1945 struct parsed_pointer_t {
1946 construct_type_t construct_type;
1947 type_qualifiers_t type_qualifiers;
1950 typedef struct construct_function_type_t construct_function_type_t;
1951 struct construct_function_type_t {
1952 construct_type_t construct_type;
1953 type_t *function_type;
1956 typedef struct parsed_array_t parsed_array_t;
1957 struct parsed_array_t {
1958 construct_type_t construct_type;
1959 type_qualifiers_t type_qualifiers;
1965 typedef struct construct_base_type_t construct_base_type_t;
1966 struct construct_base_type_t {
1967 construct_type_t construct_type;
1971 static construct_type_t *parse_pointer_declarator(void)
1975 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1976 memset(pointer, 0, sizeof(pointer[0]));
1977 pointer->construct_type.type = CONSTRUCT_POINTER;
1978 pointer->type_qualifiers = parse_type_qualifiers();
1980 return (construct_type_t*) pointer;
1983 static construct_type_t *parse_array_declarator(void)
1987 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1988 memset(array, 0, sizeof(array[0]));
1989 array->construct_type.type = CONSTRUCT_ARRAY;
1991 if(token.type == T_static) {
1992 array->is_static = true;
1996 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1997 if(type_qualifiers != 0) {
1998 if(token.type == T_static) {
1999 array->is_static = true;
2003 array->type_qualifiers = type_qualifiers;
2005 if(token.type == '*' && look_ahead(1)->type == ']') {
2006 array->is_variable = true;
2008 } else if(token.type != ']') {
2009 array->size = parse_assignment_expression();
2014 return (construct_type_t*) array;
2017 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2021 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2023 declaration_t *parameters = parse_parameters(&type->function);
2024 if(declaration != NULL) {
2025 declaration->scope.declarations = parameters;
2028 construct_function_type_t *construct_function_type =
2029 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2030 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2031 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2032 construct_function_type->function_type = type;
2036 return (construct_type_t*) construct_function_type;
2039 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2040 bool may_be_abstract)
2042 /* construct a single linked list of construct_type_t's which describe
2043 * how to construct the final declarator type */
2044 construct_type_t *first = NULL;
2045 construct_type_t *last = NULL;
2048 while(token.type == '*') {
2049 construct_type_t *type = parse_pointer_declarator();
2060 /* TODO: find out if this is correct */
2063 construct_type_t *inner_types = NULL;
2065 switch(token.type) {
2067 if(declaration == NULL) {
2068 errorf(HERE, "no identifier expected in typename");
2070 declaration->symbol = token.v.symbol;
2071 declaration->source_position = token.source_position;
2077 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2083 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2084 /* avoid a loop in the outermost scope, because eat_statement doesn't
2086 if(token.type == '}' && current_function == NULL) {
2094 construct_type_t *p = last;
2097 construct_type_t *type;
2098 switch(token.type) {
2100 type = parse_function_declarator(declaration);
2103 type = parse_array_declarator();
2106 goto declarator_finished;
2109 /* insert in the middle of the list (behind p) */
2111 type->next = p->next;
2122 declarator_finished:
2125 /* append inner_types at the end of the list, we don't to set last anymore
2126 * as it's not needed anymore */
2128 assert(first == NULL);
2129 first = inner_types;
2131 last->next = inner_types;
2137 static type_t *construct_declarator_type(construct_type_t *construct_list,
2140 construct_type_t *iter = construct_list;
2141 for( ; iter != NULL; iter = iter->next) {
2142 switch(iter->type) {
2143 case CONSTRUCT_INVALID:
2144 panic("invalid type construction found");
2145 case CONSTRUCT_FUNCTION: {
2146 construct_function_type_t *construct_function_type
2147 = (construct_function_type_t*) iter;
2149 type_t *function_type = construct_function_type->function_type;
2151 function_type->function.return_type = type;
2153 type_t *skipped_return_type = skip_typeref(type);
2154 if (is_type_function(skipped_return_type)) {
2155 errorf(HERE, "function returning function is not allowed");
2156 type = type_error_type;
2157 } else if (is_type_array(skipped_return_type)) {
2158 errorf(HERE, "function returning array is not allowed");
2159 type = type_error_type;
2161 type = function_type;
2166 case CONSTRUCT_POINTER: {
2167 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2168 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2169 pointer_type->pointer.points_to = type;
2170 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2172 type = pointer_type;
2176 case CONSTRUCT_ARRAY: {
2177 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2178 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2180 array_type->base.qualifiers = parsed_array->type_qualifiers;
2181 array_type->array.element_type = type;
2182 array_type->array.is_static = parsed_array->is_static;
2183 array_type->array.is_variable = parsed_array->is_variable;
2184 array_type->array.size = parsed_array->size;
2186 type_t *skipped_type = skip_typeref(type);
2187 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2188 errorf(HERE, "array of void is not allowed");
2189 type = type_error_type;
2197 type_t *hashed_type = typehash_insert(type);
2198 if(hashed_type != type) {
2199 /* the function type was constructed earlier freeing it here will
2200 * destroy other types... */
2201 if(iter->type != CONSTRUCT_FUNCTION) {
2211 static declaration_t *parse_declarator(
2212 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2214 declaration_t *const declaration = allocate_declaration_zero();
2215 declaration->storage_class = specifiers->storage_class;
2216 declaration->modifiers = specifiers->decl_modifiers;
2217 declaration->is_inline = specifiers->is_inline;
2219 construct_type_t *construct_type
2220 = parse_inner_declarator(declaration, may_be_abstract);
2221 type_t *const type = specifiers->type;
2222 declaration->type = construct_declarator_type(construct_type, type);
2224 if(construct_type != NULL) {
2225 obstack_free(&temp_obst, construct_type);
2231 static type_t *parse_abstract_declarator(type_t *base_type)
2233 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2235 type_t *result = construct_declarator_type(construct_type, base_type);
2236 if(construct_type != NULL) {
2237 obstack_free(&temp_obst, construct_type);
2243 static declaration_t *append_declaration(declaration_t* const declaration)
2245 if (last_declaration != NULL) {
2246 last_declaration->next = declaration;
2248 scope->declarations = declaration;
2250 last_declaration = declaration;
2255 * Check if the declaration of main is suspicious. main should be a
2256 * function with external linkage, returning int, taking either zero
2257 * arguments, two, or three arguments of appropriate types, ie.
2259 * int main([ int argc, char **argv [, char **env ] ]).
2261 * @param decl the declaration to check
2262 * @param type the function type of the declaration
2264 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2266 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2267 warningf(decl->source_position, "'main' is normally a non-static function");
2269 if (skip_typeref(func_type->return_type) != type_int) {
2270 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2272 const function_parameter_t *parm = func_type->parameters;
2274 type_t *const first_type = parm->type;
2275 if (!types_compatible(skip_typeref(first_type), type_int)) {
2276 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2280 type_t *const second_type = parm->type;
2281 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2282 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2286 type_t *const third_type = parm->type;
2287 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2288 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2292 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2296 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2302 * Check if a symbol is the equal to "main".
2304 static bool is_sym_main(const symbol_t *const sym)
2306 return strcmp(sym->string, "main") == 0;
2309 static declaration_t *internal_record_declaration(
2310 declaration_t *const declaration,
2311 const bool is_function_definition)
2313 const symbol_t *const symbol = declaration->symbol;
2314 const namespace_t namespc = (namespace_t)declaration->namespc;
2316 type_t *const orig_type = declaration->type;
2317 const type_t *const type = skip_typeref(orig_type);
2318 if (is_type_function(type) &&
2319 type->function.unspecified_parameters &&
2320 warning.strict_prototypes) {
2321 warningf(declaration->source_position,
2322 "function declaration '%#T' is not a prototype",
2323 orig_type, declaration->symbol);
2326 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2327 check_type_of_main(declaration, &type->function);
2330 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2331 assert(declaration != previous_declaration);
2332 if (previous_declaration != NULL) {
2333 if (previous_declaration->parent_scope == scope) {
2334 /* can happen for K&R style declarations */
2335 if(previous_declaration->type == NULL) {
2336 previous_declaration->type = declaration->type;
2339 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2340 if (!types_compatible(type, prev_type)) {
2341 errorf(declaration->source_position,
2342 "declaration '%#T' is incompatible with previous declaration '%#T'",
2343 orig_type, symbol, previous_declaration->type, symbol);
2344 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2346 unsigned old_storage_class = previous_declaration->storage_class;
2347 unsigned new_storage_class = declaration->storage_class;
2349 /* pretend no storage class means extern for function declarations
2350 * (except if the previous declaration is neither none nor extern) */
2351 if (is_type_function(type)) {
2352 switch (old_storage_class) {
2353 case STORAGE_CLASS_NONE:
2354 old_storage_class = STORAGE_CLASS_EXTERN;
2356 case STORAGE_CLASS_EXTERN:
2357 if (is_function_definition) {
2358 if (warning.missing_prototypes &&
2359 prev_type->function.unspecified_parameters &&
2360 !is_sym_main(symbol)) {
2361 warningf(declaration->source_position, "no previous prototype for '%#T'", orig_type, symbol);
2363 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2364 new_storage_class = STORAGE_CLASS_EXTERN;
2372 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2373 new_storage_class == STORAGE_CLASS_EXTERN) {
2374 warn_redundant_declaration:
2375 if (warning.redundant_decls) {
2376 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2377 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2379 } else if (current_function == NULL) {
2380 if (old_storage_class != STORAGE_CLASS_STATIC &&
2381 new_storage_class == STORAGE_CLASS_STATIC) {
2382 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2383 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2385 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2386 goto warn_redundant_declaration;
2388 if (new_storage_class == STORAGE_CLASS_NONE) {
2389 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2393 if (old_storage_class == new_storage_class) {
2394 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2396 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2398 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2401 return previous_declaration;
2403 } else if (is_function_definition) {
2404 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2405 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2406 warningf(declaration->source_position, "no previous prototype for '%#T'", orig_type, symbol);
2407 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2408 warningf(declaration->source_position, "no previous declaration for '%#T'", orig_type, symbol);
2411 } else if (warning.missing_declarations &&
2412 scope == global_scope &&
2413 !is_type_function(type) && (
2414 declaration->storage_class == STORAGE_CLASS_NONE ||
2415 declaration->storage_class == STORAGE_CLASS_THREAD
2417 warningf(declaration->source_position, "no previous declaration for '%#T'", orig_type, symbol);
2420 assert(declaration->parent_scope == NULL);
2421 assert(declaration->symbol != NULL);
2422 assert(scope != NULL);
2424 declaration->parent_scope = scope;
2426 environment_push(declaration);
2427 return append_declaration(declaration);
2430 static declaration_t *record_declaration(declaration_t *declaration)
2432 return internal_record_declaration(declaration, false);
2435 static declaration_t *record_function_definition(declaration_t *declaration)
2437 return internal_record_declaration(declaration, true);
2440 static void parser_error_multiple_definition(declaration_t *declaration,
2441 const source_position_t source_position)
2443 errorf(source_position, "multiple definition of symbol '%Y'",
2444 declaration->symbol);
2445 errorf(declaration->source_position,
2446 "this is the location of the previous definition.");
2449 static bool is_declaration_specifier(const token_t *token,
2450 bool only_type_specifiers)
2452 switch(token->type) {
2456 return is_typedef_symbol(token->v.symbol);
2458 case T___extension__:
2461 return !only_type_specifiers;
2468 static void parse_init_declarator_rest(declaration_t *declaration)
2472 type_t *orig_type = declaration->type;
2473 type_t *type = type = skip_typeref(orig_type);
2475 if(declaration->init.initializer != NULL) {
2476 parser_error_multiple_definition(declaration, token.source_position);
2479 initializer_t *initializer = parse_initializer(type);
2481 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2482 * the array type size */
2483 if(is_type_array(type) && initializer != NULL) {
2484 array_type_t *array_type = &type->array;
2486 if(array_type->size == NULL) {
2487 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2489 cnst->base.datatype = type_size_t;
2491 switch (initializer->kind) {
2492 case INITIALIZER_LIST: {
2493 cnst->conste.v.int_value = initializer->list.len;
2497 case INITIALIZER_STRING: {
2498 cnst->conste.v.int_value = initializer->string.string.size;
2502 case INITIALIZER_WIDE_STRING: {
2503 cnst->conste.v.int_value = initializer->wide_string.string.size;
2508 panic("invalid initializer type");
2511 array_type->size = cnst;
2515 if(is_type_function(type)) {
2516 errorf(declaration->source_position,
2517 "initializers not allowed for function types at declator '%Y' (type '%T')",
2518 declaration->symbol, orig_type);
2520 declaration->init.initializer = initializer;
2524 /* parse rest of a declaration without any declarator */
2525 static void parse_anonymous_declaration_rest(
2526 const declaration_specifiers_t *specifiers,
2527 parsed_declaration_func finished_declaration)
2531 declaration_t *const declaration = allocate_declaration_zero();
2532 declaration->type = specifiers->type;
2533 declaration->storage_class = specifiers->storage_class;
2534 declaration->source_position = specifiers->source_position;
2536 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2537 warningf(declaration->source_position, "useless storage class in empty declaration");
2540 type_t *type = declaration->type;
2541 switch (type->kind) {
2542 case TYPE_COMPOUND_STRUCT:
2543 case TYPE_COMPOUND_UNION: {
2544 if (type->compound.declaration->symbol == NULL) {
2545 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2554 warningf(declaration->source_position, "empty declaration");
2558 finished_declaration(declaration);
2561 static void parse_declaration_rest(declaration_t *ndeclaration,
2562 const declaration_specifiers_t *specifiers,
2563 parsed_declaration_func finished_declaration)
2566 declaration_t *declaration = finished_declaration(ndeclaration);
2568 type_t *orig_type = declaration->type;
2569 type_t *type = skip_typeref(orig_type);
2571 if (type->kind != TYPE_FUNCTION &&
2572 declaration->is_inline &&
2573 is_type_valid(type)) {
2574 warningf(declaration->source_position,
2575 "variable '%Y' declared 'inline'\n", declaration->symbol);
2578 if(token.type == '=') {
2579 parse_init_declarator_rest(declaration);
2582 if(token.type != ',')
2586 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2591 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2593 symbol_t *symbol = declaration->symbol;
2594 if(symbol == NULL) {
2595 errorf(HERE, "anonymous declaration not valid as function parameter");
2598 namespace_t namespc = (namespace_t) declaration->namespc;
2599 if(namespc != NAMESPACE_NORMAL) {
2600 return record_declaration(declaration);
2603 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2604 if(previous_declaration == NULL ||
2605 previous_declaration->parent_scope != scope) {
2606 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2611 if(previous_declaration->type == NULL) {
2612 previous_declaration->type = declaration->type;
2613 previous_declaration->storage_class = declaration->storage_class;
2614 previous_declaration->parent_scope = scope;
2615 return previous_declaration;
2617 return record_declaration(declaration);
2621 static void parse_declaration(parsed_declaration_func finished_declaration)
2623 declaration_specifiers_t specifiers;
2624 memset(&specifiers, 0, sizeof(specifiers));
2625 parse_declaration_specifiers(&specifiers);
2627 if(token.type == ';') {
2628 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2630 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2631 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2635 static void parse_kr_declaration_list(declaration_t *declaration)
2637 type_t *type = skip_typeref(declaration->type);
2638 if(!is_type_function(type))
2641 if(!type->function.kr_style_parameters)
2644 /* push function parameters */
2645 int top = environment_top();
2646 scope_t *last_scope = scope;
2647 set_scope(&declaration->scope);
2649 declaration_t *parameter = declaration->scope.declarations;
2650 for( ; parameter != NULL; parameter = parameter->next) {
2651 assert(parameter->parent_scope == NULL);
2652 parameter->parent_scope = scope;
2653 environment_push(parameter);
2656 /* parse declaration list */
2657 while(is_declaration_specifier(&token, false)) {
2658 parse_declaration(finished_kr_declaration);
2661 /* pop function parameters */
2662 assert(scope == &declaration->scope);
2663 set_scope(last_scope);
2664 environment_pop_to(top);
2666 /* update function type */
2667 type_t *new_type = duplicate_type(type);
2668 new_type->function.kr_style_parameters = false;
2670 function_parameter_t *parameters = NULL;
2671 function_parameter_t *last_parameter = NULL;
2673 declaration_t *parameter_declaration = declaration->scope.declarations;
2674 for( ; parameter_declaration != NULL;
2675 parameter_declaration = parameter_declaration->next) {
2676 type_t *parameter_type = parameter_declaration->type;
2677 if(parameter_type == NULL) {
2679 errorf(HERE, "no type specified for function parameter '%Y'",
2680 parameter_declaration->symbol);
2682 if (warning.implicit_int) {
2683 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2684 parameter_declaration->symbol);
2686 parameter_type = type_int;
2687 parameter_declaration->type = parameter_type;
2691 semantic_parameter(parameter_declaration);
2692 parameter_type = parameter_declaration->type;
2694 function_parameter_t *function_parameter
2695 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2696 memset(function_parameter, 0, sizeof(function_parameter[0]));
2698 function_parameter->type = parameter_type;
2699 if(last_parameter != NULL) {
2700 last_parameter->next = function_parameter;
2702 parameters = function_parameter;
2704 last_parameter = function_parameter;
2706 new_type->function.parameters = parameters;
2708 type = typehash_insert(new_type);
2709 if(type != new_type) {
2710 obstack_free(type_obst, new_type);
2713 declaration->type = type;
2716 static bool first_err = true;
2719 * When called with first_err set, prints the name of the current function,
2722 static void print_in_function(void) {
2725 diagnosticf("%s: In function '%Y':\n",
2726 current_function->source_position.input_name,
2727 current_function->symbol);
2732 * Check if all labels are defined in the current function.
2733 * Check if all labels are used in the current function.
2735 static void check_labels(void)
2737 for (const goto_statement_t *goto_statement = goto_first;
2738 goto_statement != NULL;
2739 goto_statement = goto_statement->next) {
2740 declaration_t *label = goto_statement->label;
2743 if (label->source_position.input_name == NULL) {
2744 print_in_function();
2745 errorf(goto_statement->statement.source_position,
2746 "label '%Y' used but not defined", label->symbol);
2749 goto_first = goto_last = NULL;
2751 if (warning.unused_label) {
2752 for (const label_statement_t *label_statement = label_first;
2753 label_statement != NULL;
2754 label_statement = label_statement->next) {
2755 const declaration_t *label = label_statement->label;
2757 if (! label->used) {
2758 print_in_function();
2759 warningf(label_statement->statement.source_position,
2760 "label '%Y' defined but not used", label->symbol);
2764 label_first = label_last = NULL;
2768 * Check declarations of current_function for unused entities.
2770 static void check_declarations(void)
2772 if (warning.unused_parameter) {
2773 const scope_t *scope = ¤t_function->scope;
2775 const declaration_t *parameter = scope->declarations;
2776 for (; parameter != NULL; parameter = parameter->next) {
2777 if (! parameter->used) {
2778 print_in_function();
2779 warningf(parameter->source_position,
2780 "unused parameter '%Y'", parameter->symbol);
2784 if (warning.unused_variable) {
2788 static void parse_external_declaration(void)
2790 /* function-definitions and declarations both start with declaration
2792 declaration_specifiers_t specifiers;
2793 memset(&specifiers, 0, sizeof(specifiers));
2794 parse_declaration_specifiers(&specifiers);
2796 /* must be a declaration */
2797 if(token.type == ';') {
2798 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2802 /* declarator is common to both function-definitions and declarations */
2803 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2805 /* must be a declaration */
2806 if(token.type == ',' || token.type == '=' || token.type == ';') {
2807 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2811 /* must be a function definition */
2812 parse_kr_declaration_list(ndeclaration);
2814 if(token.type != '{') {
2815 parse_error_expected("while parsing function definition", '{', 0);
2820 type_t *type = ndeclaration->type;
2822 /* note that we don't skip typerefs: the standard doesn't allow them here
2823 * (so we can't use is_type_function here) */
2824 if(type->kind != TYPE_FUNCTION) {
2825 if (is_type_valid(type)) {
2826 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2827 type, ndeclaration->symbol);
2833 /* § 6.7.5.3 (14) a function definition with () means no
2834 * parameters (and not unspecified parameters) */
2835 if(type->function.unspecified_parameters) {
2836 type_t *duplicate = duplicate_type(type);
2837 duplicate->function.unspecified_parameters = false;
2839 type = typehash_insert(duplicate);
2840 if(type != duplicate) {
2841 obstack_free(type_obst, duplicate);
2843 ndeclaration->type = type;
2846 declaration_t *const declaration = record_function_definition(ndeclaration);
2847 if(ndeclaration != declaration) {
2848 declaration->scope = ndeclaration->scope;
2850 type = skip_typeref(declaration->type);
2852 /* push function parameters and switch scope */
2853 int top = environment_top();
2854 scope_t *last_scope = scope;
2855 set_scope(&declaration->scope);
2857 declaration_t *parameter = declaration->scope.declarations;
2858 for( ; parameter != NULL; parameter = parameter->next) {
2859 if(parameter->parent_scope == &ndeclaration->scope) {
2860 parameter->parent_scope = scope;
2862 assert(parameter->parent_scope == NULL
2863 || parameter->parent_scope == scope);
2864 parameter->parent_scope = scope;
2865 environment_push(parameter);
2868 if(declaration->init.statement != NULL) {
2869 parser_error_multiple_definition(declaration, token.source_position);
2871 goto end_of_parse_external_declaration;
2873 /* parse function body */
2874 int label_stack_top = label_top();
2875 declaration_t *old_current_function = current_function;
2876 current_function = declaration;
2878 declaration->init.statement = parse_compound_statement();
2881 check_declarations();
2883 assert(current_function == declaration);
2884 current_function = old_current_function;
2885 label_pop_to(label_stack_top);
2888 end_of_parse_external_declaration:
2889 assert(scope == &declaration->scope);
2890 set_scope(last_scope);
2891 environment_pop_to(top);
2894 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2896 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2897 type->bitfield.base = base;
2898 type->bitfield.size = size;
2903 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2905 /* TODO: check constraints for struct declarations (in specifiers) */
2907 declaration_t *declaration;
2909 if(token.type == ':') {
2912 type_t *base_type = specifiers->type;
2913 expression_t *size = parse_constant_expression();
2915 type_t *type = make_bitfield_type(base_type, size);
2917 declaration = allocate_declaration_zero();
2918 declaration->namespc = NAMESPACE_NORMAL;
2919 declaration->storage_class = STORAGE_CLASS_NONE;
2920 declaration->source_position = token.source_position;
2921 declaration->modifiers = specifiers->decl_modifiers;
2922 declaration->type = type;
2924 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2926 if(token.type == ':') {
2928 expression_t *size = parse_constant_expression();
2930 type_t *type = make_bitfield_type(declaration->type, size);
2931 declaration->type = type;
2934 record_declaration(declaration);
2936 if(token.type != ',')
2943 static void parse_compound_type_entries(void)
2947 while(token.type != '}' && token.type != T_EOF) {
2948 declaration_specifiers_t specifiers;
2949 memset(&specifiers, 0, sizeof(specifiers));
2950 parse_declaration_specifiers(&specifiers);
2952 parse_struct_declarators(&specifiers);
2954 if(token.type == T_EOF) {
2955 errorf(HERE, "EOF while parsing struct");
2960 static type_t *parse_typename(void)
2962 declaration_specifiers_t specifiers;
2963 memset(&specifiers, 0, sizeof(specifiers));
2964 parse_declaration_specifiers(&specifiers);
2965 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2966 /* TODO: improve error message, user does probably not know what a
2967 * storage class is...
2969 errorf(HERE, "typename may not have a storage class");
2972 type_t *result = parse_abstract_declarator(specifiers.type);
2980 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2981 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2982 expression_t *left);
2984 typedef struct expression_parser_function_t expression_parser_function_t;
2985 struct expression_parser_function_t {
2986 unsigned precedence;
2987 parse_expression_function parser;
2988 unsigned infix_precedence;
2989 parse_expression_infix_function infix_parser;
2992 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2995 * Creates a new invalid expression.
2997 static expression_t *create_invalid_expression(void)
2999 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3000 expression->base.source_position = token.source_position;
3005 * Prints an error message if an expression was expected but not read
3007 static expression_t *expected_expression_error(void)
3009 /* skip the error message if the error token was read */
3010 if (token.type != T_ERROR) {
3011 errorf(HERE, "expected expression, got token '%K'", &token);
3015 return create_invalid_expression();
3019 * Parse a string constant.
3021 static expression_t *parse_string_const(void)
3023 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3024 cnst->base.datatype = type_string;
3025 cnst->string.value = parse_string_literals();
3031 * Parse a wide string constant.
3033 static expression_t *parse_wide_string_const(void)
3035 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3036 cnst->base.datatype = type_wchar_t_ptr;
3037 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3043 * Parse an integer constant.
3045 static expression_t *parse_int_const(void)
3047 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3048 cnst->base.datatype = token.datatype;
3049 cnst->conste.v.int_value = token.v.intvalue;
3057 * Parse a float constant.
3059 static expression_t *parse_float_const(void)
3061 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3062 cnst->base.datatype = token.datatype;
3063 cnst->conste.v.float_value = token.v.floatvalue;
3070 static declaration_t *create_implicit_function(symbol_t *symbol,
3071 const source_position_t source_position)
3073 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3074 ntype->function.return_type = type_int;
3075 ntype->function.unspecified_parameters = true;
3077 type_t *type = typehash_insert(ntype);
3082 declaration_t *const declaration = allocate_declaration_zero();
3083 declaration->storage_class = STORAGE_CLASS_EXTERN;
3084 declaration->type = type;
3085 declaration->symbol = symbol;
3086 declaration->source_position = source_position;
3087 declaration->parent_scope = global_scope;
3089 scope_t *old_scope = scope;
3090 set_scope(global_scope);
3092 environment_push(declaration);
3093 /* prepends the declaration to the global declarations list */
3094 declaration->next = scope->declarations;
3095 scope->declarations = declaration;
3097 assert(scope == global_scope);
3098 set_scope(old_scope);
3104 * Creates a return_type (func)(argument_type) function type if not
3107 * @param return_type the return type
3108 * @param argument_type the argument type
3110 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3112 function_parameter_t *parameter
3113 = obstack_alloc(type_obst, sizeof(parameter[0]));
3114 memset(parameter, 0, sizeof(parameter[0]));
3115 parameter->type = argument_type;
3117 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3118 type->function.return_type = return_type;
3119 type->function.parameters = parameter;
3121 type_t *result = typehash_insert(type);
3122 if(result != type) {
3130 * Creates a function type for some function like builtins.
3132 * @param symbol the symbol describing the builtin
3134 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3136 switch(symbol->ID) {
3137 case T___builtin_alloca:
3138 return make_function_1_type(type_void_ptr, type_size_t);
3139 case T___builtin_nan:
3140 return make_function_1_type(type_double, type_string);
3141 case T___builtin_nanf:
3142 return make_function_1_type(type_float, type_string);
3143 case T___builtin_nand:
3144 return make_function_1_type(type_long_double, type_string);
3145 case T___builtin_va_end:
3146 return make_function_1_type(type_void, type_valist);
3148 panic("not implemented builtin symbol found");
3153 * Performs automatic type cast as described in § 6.3.2.1.
3155 * @param orig_type the original type
3157 static type_t *automatic_type_conversion(type_t *orig_type)
3159 type_t *type = skip_typeref(orig_type);
3160 if(is_type_array(type)) {
3161 array_type_t *array_type = &type->array;
3162 type_t *element_type = array_type->element_type;
3163 unsigned qualifiers = array_type->type.qualifiers;
3165 return make_pointer_type(element_type, qualifiers);
3168 if(is_type_function(type)) {
3169 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3176 * reverts the automatic casts of array to pointer types and function
3177 * to function-pointer types as defined § 6.3.2.1
3179 type_t *revert_automatic_type_conversion(const expression_t *expression)
3181 switch (expression->kind) {
3182 case EXPR_REFERENCE: return expression->reference.declaration->type;
3183 case EXPR_SELECT: return expression->select.compound_entry->type;
3185 case EXPR_UNARY_DEREFERENCE: {
3186 const expression_t *const value = expression->unary.value;
3187 type_t *const type = skip_typeref(value->base.datatype);
3188 assert(is_type_pointer(type));
3189 return type->pointer.points_to;
3192 case EXPR_BUILTIN_SYMBOL:
3193 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3195 case EXPR_ARRAY_ACCESS: {
3196 const expression_t *const array_ref = expression->array_access.array_ref;
3197 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3198 if (!is_type_valid(type_left))
3200 assert(is_type_pointer(type_left));
3201 return type_left->pointer.points_to;
3207 return expression->base.datatype;
3210 static expression_t *parse_reference(void)
3212 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3214 reference_expression_t *ref = &expression->reference;
3215 ref->symbol = token.v.symbol;
3217 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3219 source_position_t source_position = token.source_position;
3222 if(declaration == NULL) {
3223 if (! strict_mode && token.type == '(') {
3224 /* an implicitly defined function */
3225 if (warning.implicit_function_declaration) {
3226 warningf(HERE, "implicit declaration of function '%Y'",
3230 declaration = create_implicit_function(ref->symbol,
3233 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3238 type_t *type = declaration->type;
3240 /* we always do the auto-type conversions; the & and sizeof parser contains
3241 * code to revert this! */
3242 type = automatic_type_conversion(type);
3244 ref->declaration = declaration;
3245 ref->expression.datatype = type;
3247 /* this declaration is used */
3248 declaration->used = true;
3253 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3257 /* TODO check if explicit cast is allowed and issue warnings/errors */
3260 static expression_t *parse_cast(void)
3262 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3264 cast->base.source_position = token.source_position;
3266 type_t *type = parse_typename();
3269 expression_t *value = parse_sub_expression(20);
3271 check_cast_allowed(value, type);
3273 cast->base.datatype = type;
3274 cast->unary.value = value;
3279 static expression_t *parse_statement_expression(void)
3281 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3283 statement_t *statement = parse_compound_statement();
3284 expression->statement.statement = statement;
3285 expression->base.source_position = statement->base.source_position;
3287 /* find last statement and use its type */
3288 type_t *type = type_void;
3289 const statement_t *stmt = statement->compound.statements;
3291 while (stmt->base.next != NULL)
3292 stmt = stmt->base.next;
3294 if (stmt->kind == STATEMENT_EXPRESSION) {
3295 type = stmt->expression.expression->base.datatype;
3298 warningf(expression->base.source_position, "empty statement expression ({})");
3300 expression->base.datatype = type;
3307 static expression_t *parse_brace_expression(void)
3311 switch(token.type) {
3313 /* gcc extension: a statement expression */
3314 return parse_statement_expression();
3318 return parse_cast();
3320 if(is_typedef_symbol(token.v.symbol)) {
3321 return parse_cast();
3325 expression_t *result = parse_expression();
3331 static expression_t *parse_function_keyword(void)
3336 if (current_function == NULL) {
3337 errorf(HERE, "'__func__' used outside of a function");
3340 string_literal_expression_t *expression
3341 = allocate_ast_zero(sizeof(expression[0]));
3343 expression->expression.kind = EXPR_FUNCTION;
3344 expression->expression.datatype = type_string;
3346 return (expression_t*) expression;
3349 static expression_t *parse_pretty_function_keyword(void)
3351 eat(T___PRETTY_FUNCTION__);
3354 if (current_function == NULL) {
3355 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3358 string_literal_expression_t *expression
3359 = allocate_ast_zero(sizeof(expression[0]));
3361 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3362 expression->expression.datatype = type_string;
3364 return (expression_t*) expression;
3367 static designator_t *parse_designator(void)
3369 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3371 if(token.type != T_IDENTIFIER) {
3372 parse_error_expected("while parsing member designator",
3377 result->symbol = token.v.symbol;
3380 designator_t *last_designator = result;
3382 if(token.type == '.') {
3384 if(token.type != T_IDENTIFIER) {
3385 parse_error_expected("while parsing member designator",
3390 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3391 designator->symbol = token.v.symbol;
3394 last_designator->next = designator;
3395 last_designator = designator;
3398 if(token.type == '[') {
3400 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3401 designator->array_access = parse_expression();
3402 if(designator->array_access == NULL) {
3408 last_designator->next = designator;
3409 last_designator = designator;
3418 static expression_t *parse_offsetof(void)
3420 eat(T___builtin_offsetof);
3422 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3423 expression->base.datatype = type_size_t;
3426 expression->offsetofe.type = parse_typename();
3428 expression->offsetofe.designator = parse_designator();
3434 static expression_t *parse_va_start(void)
3436 eat(T___builtin_va_start);
3438 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3441 expression->va_starte.ap = parse_assignment_expression();
3443 expression_t *const expr = parse_assignment_expression();
3444 if (expr->kind == EXPR_REFERENCE) {
3445 declaration_t *const decl = expr->reference.declaration;
3446 if (decl->parent_scope == ¤t_function->scope &&
3447 decl->next == NULL) {
3448 expression->va_starte.parameter = decl;
3453 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3455 return create_invalid_expression();
3458 static expression_t *parse_va_arg(void)
3460 eat(T___builtin_va_arg);
3462 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3465 expression->va_arge.ap = parse_assignment_expression();
3467 expression->base.datatype = parse_typename();
3473 static expression_t *parse_builtin_symbol(void)
3475 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3477 symbol_t *symbol = token.v.symbol;
3479 expression->builtin_symbol.symbol = symbol;
3482 type_t *type = get_builtin_symbol_type(symbol);
3483 type = automatic_type_conversion(type);
3485 expression->base.datatype = type;
3489 static expression_t *parse_builtin_constant(void)
3491 eat(T___builtin_constant_p);
3493 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3496 expression->builtin_constant.value = parse_assignment_expression();
3498 expression->base.datatype = type_int;
3503 static expression_t *parse_builtin_prefetch(void)
3505 eat(T___builtin_prefetch);
3507 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3510 expression->builtin_prefetch.adr = parse_assignment_expression();
3511 if (token.type == ',') {
3513 expression->builtin_prefetch.rw = parse_assignment_expression();
3515 if (token.type == ',') {
3517 expression->builtin_prefetch.locality = parse_assignment_expression();
3520 expression->base.datatype = type_void;
3525 static expression_t *parse_compare_builtin(void)
3527 expression_t *expression;
3529 switch(token.type) {
3530 case T___builtin_isgreater:
3531 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3533 case T___builtin_isgreaterequal:
3534 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3536 case T___builtin_isless:
3537 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3539 case T___builtin_islessequal:
3540 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3542 case T___builtin_islessgreater:
3543 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3545 case T___builtin_isunordered:
3546 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3549 panic("invalid compare builtin found");
3552 expression->base.source_position = HERE;
3556 expression->binary.left = parse_assignment_expression();
3558 expression->binary.right = parse_assignment_expression();
3561 type_t *const orig_type_left = expression->binary.left->base.datatype;
3562 type_t *const orig_type_right = expression->binary.right->base.datatype;
3564 type_t *const type_left = skip_typeref(orig_type_left);
3565 type_t *const type_right = skip_typeref(orig_type_right);
3566 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3567 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3568 type_error_incompatible("invalid operands in comparison",
3569 expression->base.source_position, orig_type_left, orig_type_right);
3572 semantic_comparison(&expression->binary);
3578 static expression_t *parse_builtin_expect(void)
3580 eat(T___builtin_expect);
3582 expression_t *expression
3583 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3586 expression->binary.left = parse_assignment_expression();
3588 expression->binary.right = parse_constant_expression();
3591 expression->base.datatype = expression->binary.left->base.datatype;
3596 static expression_t *parse_assume(void) {
3599 expression_t *expression
3600 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3603 expression->unary.value = parse_assignment_expression();
3606 expression->base.datatype = type_void;
3610 static expression_t *parse_alignof(void) {
3613 expression_t *expression
3614 = allocate_expression_zero(EXPR_ALIGNOF);
3617 expression->alignofe.type = parse_typename();
3620 expression->base.datatype = type_size_t;
3624 static expression_t *parse_primary_expression(void)
3626 switch(token.type) {
3628 return parse_int_const();
3629 case T_FLOATINGPOINT:
3630 return parse_float_const();
3631 case T_STRING_LITERAL:
3632 return parse_string_const();
3633 case T_WIDE_STRING_LITERAL:
3634 return parse_wide_string_const();
3636 return parse_reference();
3637 case T___FUNCTION__:
3639 return parse_function_keyword();
3640 case T___PRETTY_FUNCTION__:
3641 return parse_pretty_function_keyword();
3642 case T___builtin_offsetof:
3643 return parse_offsetof();
3644 case T___builtin_va_start:
3645 return parse_va_start();
3646 case T___builtin_va_arg:
3647 return parse_va_arg();
3648 case T___builtin_expect:
3649 return parse_builtin_expect();
3650 case T___builtin_nanf:
3651 case T___builtin_alloca:
3652 case T___builtin_va_end:
3653 return parse_builtin_symbol();
3654 case T___builtin_isgreater:
3655 case T___builtin_isgreaterequal:
3656 case T___builtin_isless:
3657 case T___builtin_islessequal:
3658 case T___builtin_islessgreater:
3659 case T___builtin_isunordered:
3660 return parse_compare_builtin();
3661 case T___builtin_constant_p:
3662 return parse_builtin_constant();
3663 case T___builtin_prefetch:
3664 return parse_builtin_prefetch();
3666 return parse_alignof();
3668 return parse_assume();
3671 return parse_brace_expression();
3674 errorf(HERE, "unexpected token '%K'", &token);
3677 return create_invalid_expression();
3681 * Check if the expression has the character type and issue a warning then.
3683 static void check_for_char_index_type(const expression_t *expression) {
3684 type_t *const type = expression->base.datatype;
3685 const type_t *const base_type = skip_typeref(type);
3687 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3688 warning.char_subscripts) {
3689 warningf(expression->base.source_position,
3690 "array subscript has type '%T'", type);
3694 static expression_t *parse_array_expression(unsigned precedence,
3701 expression_t *inside = parse_expression();
3703 array_access_expression_t *array_access
3704 = allocate_ast_zero(sizeof(array_access[0]));
3706 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3708 type_t *const orig_type_left = left->base.datatype;
3709 type_t *const orig_type_inside = inside->base.datatype;
3711 type_t *const type_left = skip_typeref(orig_type_left);
3712 type_t *const type_inside = skip_typeref(orig_type_inside);
3714 type_t *return_type;
3715 if (is_type_pointer(type_left)) {
3716 return_type = type_left->pointer.points_to;
3717 array_access->array_ref = left;
3718 array_access->index = inside;
3719 check_for_char_index_type(inside);
3720 } else if (is_type_pointer(type_inside)) {
3721 return_type = type_inside->pointer.points_to;
3722 array_access->array_ref = inside;
3723 array_access->index = left;
3724 array_access->flipped = true;
3725 check_for_char_index_type(left);
3727 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3729 "array access on object with non-pointer types '%T', '%T'",
3730 orig_type_left, orig_type_inside);
3732 return_type = type_error_type;
3733 array_access->array_ref = create_invalid_expression();
3736 if(token.type != ']') {
3737 parse_error_expected("Problem while parsing array access", ']', 0);
3738 return (expression_t*) array_access;
3742 return_type = automatic_type_conversion(return_type);
3743 array_access->expression.datatype = return_type;
3745 return (expression_t*) array_access;
3748 static expression_t *parse_sizeof(unsigned precedence)
3752 sizeof_expression_t *sizeof_expression
3753 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3754 sizeof_expression->expression.kind = EXPR_SIZEOF;
3755 sizeof_expression->expression.datatype = type_size_t;
3757 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3759 sizeof_expression->type = parse_typename();
3762 expression_t *expression = parse_sub_expression(precedence);
3763 expression->base.datatype = revert_automatic_type_conversion(expression);
3765 sizeof_expression->type = expression->base.datatype;
3766 sizeof_expression->size_expression = expression;
3769 return (expression_t*) sizeof_expression;
3772 static expression_t *parse_select_expression(unsigned precedence,
3773 expression_t *compound)
3776 assert(token.type == '.' || token.type == T_MINUSGREATER);
3778 bool is_pointer = (token.type == T_MINUSGREATER);
3781 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3782 select->select.compound = compound;
3784 if(token.type != T_IDENTIFIER) {
3785 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3788 symbol_t *symbol = token.v.symbol;
3789 select->select.symbol = symbol;
3792 type_t *const orig_type = compound->base.datatype;
3793 type_t *const type = skip_typeref(orig_type);
3795 type_t *type_left = type;
3797 if (!is_type_pointer(type)) {
3798 if (is_type_valid(type)) {
3799 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3801 return create_invalid_expression();
3803 type_left = type->pointer.points_to;
3805 type_left = skip_typeref(type_left);
3807 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3808 type_left->kind != TYPE_COMPOUND_UNION) {
3809 if (is_type_valid(type_left)) {
3810 errorf(HERE, "request for member '%Y' in something not a struct or "
3811 "union, but '%T'", symbol, type_left);
3813 return create_invalid_expression();
3816 declaration_t *const declaration = type_left->compound.declaration;
3818 if(!declaration->init.is_defined) {
3819 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3821 return create_invalid_expression();
3824 declaration_t *iter = declaration->scope.declarations;
3825 for( ; iter != NULL; iter = iter->next) {
3826 if(iter->symbol == symbol) {
3831 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3832 return create_invalid_expression();
3835 /* we always do the auto-type conversions; the & and sizeof parser contains
3836 * code to revert this! */
3837 type_t *expression_type = automatic_type_conversion(iter->type);
3839 select->select.compound_entry = iter;
3840 select->base.datatype = expression_type;
3842 if(expression_type->kind == TYPE_BITFIELD) {
3843 expression_t *extract
3844 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3845 extract->unary.value = select;
3846 extract->base.datatype = expression_type->bitfield.base;
3855 * Parse a call expression, ie. expression '( ... )'.
3857 * @param expression the function address
3859 static expression_t *parse_call_expression(unsigned precedence,
3860 expression_t *expression)
3863 expression_t *result = allocate_expression_zero(EXPR_CALL);
3865 call_expression_t *call = &result->call;
3866 call->function = expression;
3868 type_t *const orig_type = expression->base.datatype;
3869 type_t *const type = skip_typeref(orig_type);
3871 function_type_t *function_type = NULL;
3872 if (is_type_pointer(type)) {
3873 type_t *const to_type = skip_typeref(type->pointer.points_to);
3875 if (is_type_function(to_type)) {
3876 function_type = &to_type->function;
3877 call->expression.datatype = function_type->return_type;
3881 if (function_type == NULL && is_type_valid(type)) {
3882 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3885 /* parse arguments */
3888 if(token.type != ')') {
3889 call_argument_t *last_argument = NULL;
3892 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3894 argument->expression = parse_assignment_expression();
3895 if(last_argument == NULL) {
3896 call->arguments = argument;
3898 last_argument->next = argument;
3900 last_argument = argument;
3902 if(token.type != ',')
3909 if(function_type != NULL) {
3910 function_parameter_t *parameter = function_type->parameters;
3911 call_argument_t *argument = call->arguments;
3912 for( ; parameter != NULL && argument != NULL;
3913 parameter = parameter->next, argument = argument->next) {
3914 type_t *expected_type = parameter->type;
3915 /* TODO report scope in error messages */
3916 expression_t *const arg_expr = argument->expression;
3917 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3918 if (res_type == NULL) {
3919 /* TODO improve error message */
3920 errorf(arg_expr->base.source_position,
3921 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3922 arg_expr, arg_expr->base.datatype, expected_type);
3924 argument->expression = create_implicit_cast(argument->expression, expected_type);
3927 /* too few parameters */
3928 if(parameter != NULL) {
3929 errorf(HERE, "too few arguments to function '%E'", expression);
3930 } else if(argument != NULL) {
3931 /* too many parameters */
3932 if(!function_type->variadic
3933 && !function_type->unspecified_parameters) {
3934 errorf(HERE, "too many arguments to function '%E'", expression);
3936 /* do default promotion */
3937 for( ; argument != NULL; argument = argument->next) {
3938 type_t *type = argument->expression->base.datatype;
3940 type = skip_typeref(type);
3941 if(is_type_integer(type)) {
3942 type = promote_integer(type);
3943 } else if(type == type_float) {
3947 argument->expression
3948 = create_implicit_cast(argument->expression, type);
3951 check_format(&result->call);
3954 check_format(&result->call);
3961 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3963 static bool same_compound_type(const type_t *type1, const type_t *type2)
3966 is_type_compound(type1) &&
3967 type1->kind == type2->kind &&
3968 type1->compound.declaration == type2->compound.declaration;
3972 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3974 * @param expression the conditional expression
3976 static expression_t *parse_conditional_expression(unsigned precedence,
3977 expression_t *expression)
3981 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3983 conditional_expression_t *conditional = &result->conditional;
3984 conditional->condition = expression;
3987 type_t *const condition_type_orig = expression->base.datatype;
3988 type_t *const condition_type = skip_typeref(condition_type_orig);
3989 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3990 type_error("expected a scalar type in conditional condition",
3991 expression->base.source_position, condition_type_orig);
3994 expression_t *true_expression = parse_expression();
3996 expression_t *false_expression = parse_sub_expression(precedence);
3998 conditional->true_expression = true_expression;
3999 conditional->false_expression = false_expression;
4001 type_t *const orig_true_type = true_expression->base.datatype;
4002 type_t *const orig_false_type = false_expression->base.datatype;
4003 type_t *const true_type = skip_typeref(orig_true_type);
4004 type_t *const false_type = skip_typeref(orig_false_type);
4007 type_t *result_type;
4008 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4009 result_type = semantic_arithmetic(true_type, false_type);
4011 true_expression = create_implicit_cast(true_expression, result_type);
4012 false_expression = create_implicit_cast(false_expression, result_type);
4014 conditional->true_expression = true_expression;
4015 conditional->false_expression = false_expression;
4016 conditional->expression.datatype = result_type;
4017 } else if (same_compound_type(true_type, false_type) || (
4018 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4019 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4021 /* just take 1 of the 2 types */
4022 result_type = true_type;
4023 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4024 && pointers_compatible(true_type, false_type)) {
4026 result_type = true_type;
4029 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4030 type_error_incompatible("while parsing conditional",
4031 expression->base.source_position, true_type,
4034 result_type = type_error_type;
4037 conditional->expression.datatype = result_type;
4042 * Parse an extension expression.
4044 static expression_t *parse_extension(unsigned precedence)
4046 eat(T___extension__);
4048 /* TODO enable extensions */
4049 expression_t *expression = parse_sub_expression(precedence);
4050 /* TODO disable extensions */
4054 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4056 eat(T___builtin_classify_type);
4058 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4059 result->base.datatype = type_int;
4062 expression_t *expression = parse_sub_expression(precedence);
4064 result->classify_type.type_expression = expression;
4069 static void semantic_incdec(unary_expression_t *expression)
4071 type_t *const orig_type = expression->value->base.datatype;
4072 type_t *const type = skip_typeref(orig_type);
4073 /* TODO !is_type_real && !is_type_pointer */
4074 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4075 if (is_type_valid(type)) {
4076 /* TODO: improve error message */
4077 errorf(HERE, "operation needs an arithmetic or pointer type");
4082 expression->expression.datatype = orig_type;
4085 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4087 type_t *const orig_type = expression->value->base.datatype;
4088 type_t *const type = skip_typeref(orig_type);
4089 if(!is_type_arithmetic(type)) {
4090 if (is_type_valid(type)) {
4091 /* TODO: improve error message */
4092 errorf(HERE, "operation needs an arithmetic type");
4097 expression->expression.datatype = orig_type;
4100 static void semantic_unexpr_scalar(unary_expression_t *expression)
4102 type_t *const orig_type = expression->value->base.datatype;
4103 type_t *const type = skip_typeref(orig_type);
4104 if (!is_type_scalar(type)) {
4105 if (is_type_valid(type)) {
4106 errorf(HERE, "operand of ! must be of scalar type");
4111 expression->expression.datatype = orig_type;
4114 static void semantic_unexpr_integer(unary_expression_t *expression)
4116 type_t *const orig_type = expression->value->base.datatype;
4117 type_t *const type = skip_typeref(orig_type);
4118 if (!is_type_integer(type)) {
4119 if (is_type_valid(type)) {
4120 errorf(HERE, "operand of ~ must be of integer type");
4125 expression->expression.datatype = orig_type;
4128 static void semantic_dereference(unary_expression_t *expression)
4130 type_t *const orig_type = expression->value->base.datatype;
4131 type_t *const type = skip_typeref(orig_type);
4132 if(!is_type_pointer(type)) {
4133 if (is_type_valid(type)) {
4134 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4139 type_t *result_type = type->pointer.points_to;
4140 result_type = automatic_type_conversion(result_type);
4141 expression->expression.datatype = result_type;
4145 * Check the semantic of the address taken expression.
4147 static void semantic_take_addr(unary_expression_t *expression)
4149 expression_t *value = expression->value;
4150 value->base.datatype = revert_automatic_type_conversion(value);
4152 type_t *orig_type = value->base.datatype;
4153 if(!is_type_valid(orig_type))
4156 if(value->kind == EXPR_REFERENCE) {
4157 declaration_t *const declaration = value->reference.declaration;
4158 if(declaration != NULL) {
4159 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4160 errorf(expression->expression.source_position,
4161 "address of register variable '%Y' requested",
4162 declaration->symbol);
4164 declaration->address_taken = 1;
4168 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4171 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4172 static expression_t *parse_##unexpression_type(unsigned precedence) \
4176 expression_t *unary_expression \
4177 = allocate_expression_zero(unexpression_type); \
4178 unary_expression->base.source_position = HERE; \
4179 unary_expression->unary.value = parse_sub_expression(precedence); \
4181 sfunc(&unary_expression->unary); \
4183 return unary_expression; \
4186 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4187 semantic_unexpr_arithmetic)
4188 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4189 semantic_unexpr_arithmetic)
4190 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4191 semantic_unexpr_scalar)
4192 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4193 semantic_dereference)
4194 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4196 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4197 semantic_unexpr_integer)
4198 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4200 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4203 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4205 static expression_t *parse_##unexpression_type(unsigned precedence, \
4206 expression_t *left) \
4208 (void) precedence; \
4211 expression_t *unary_expression \
4212 = allocate_expression_zero(unexpression_type); \
4213 unary_expression->unary.value = left; \
4215 sfunc(&unary_expression->unary); \
4217 return unary_expression; \
4220 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4221 EXPR_UNARY_POSTFIX_INCREMENT,
4223 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4224 EXPR_UNARY_POSTFIX_DECREMENT,
4227 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4229 /* TODO: handle complex + imaginary types */
4231 /* § 6.3.1.8 Usual arithmetic conversions */
4232 if(type_left == type_long_double || type_right == type_long_double) {
4233 return type_long_double;
4234 } else if(type_left == type_double || type_right == type_double) {
4236 } else if(type_left == type_float || type_right == type_float) {
4240 type_right = promote_integer(type_right);
4241 type_left = promote_integer(type_left);
4243 if(type_left == type_right)
4246 bool signed_left = is_type_signed(type_left);
4247 bool signed_right = is_type_signed(type_right);
4248 int rank_left = get_rank(type_left);
4249 int rank_right = get_rank(type_right);
4250 if(rank_left < rank_right) {
4251 if(signed_left == signed_right || !signed_right) {
4257 if(signed_left == signed_right || !signed_left) {
4266 * Check the semantic restrictions for a binary expression.
4268 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4270 expression_t *const left = expression->left;
4271 expression_t *const right = expression->right;
4272 type_t *const orig_type_left = left->base.datatype;
4273 type_t *const orig_type_right = right->base.datatype;
4274 type_t *const type_left = skip_typeref(orig_type_left);
4275 type_t *const type_right = skip_typeref(orig_type_right);
4277 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4278 /* TODO: improve error message */
4279 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4280 errorf(HERE, "operation needs arithmetic types");
4285 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4286 expression->left = create_implicit_cast(left, arithmetic_type);
4287 expression->right = create_implicit_cast(right, arithmetic_type);
4288 expression->expression.datatype = arithmetic_type;
4291 static void semantic_shift_op(binary_expression_t *expression)
4293 expression_t *const left = expression->left;
4294 expression_t *const right = expression->right;
4295 type_t *const orig_type_left = left->base.datatype;
4296 type_t *const orig_type_right = right->base.datatype;
4297 type_t * type_left = skip_typeref(orig_type_left);
4298 type_t * type_right = skip_typeref(orig_type_right);
4300 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4301 /* TODO: improve error message */
4302 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4303 errorf(HERE, "operation needs integer types");
4308 type_left = promote_integer(type_left);
4309 type_right = promote_integer(type_right);
4311 expression->left = create_implicit_cast(left, type_left);
4312 expression->right = create_implicit_cast(right, type_right);
4313 expression->expression.datatype = type_left;
4316 static void semantic_add(binary_expression_t *expression)
4318 expression_t *const left = expression->left;
4319 expression_t *const right = expression->right;
4320 type_t *const orig_type_left = left->base.datatype;
4321 type_t *const orig_type_right = right->base.datatype;
4322 type_t *const type_left = skip_typeref(orig_type_left);
4323 type_t *const type_right = skip_typeref(orig_type_right);
4326 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4327 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4328 expression->left = create_implicit_cast(left, arithmetic_type);
4329 expression->right = create_implicit_cast(right, arithmetic_type);
4330 expression->expression.datatype = arithmetic_type;
4332 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4333 expression->expression.datatype = type_left;
4334 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4335 expression->expression.datatype = type_right;
4336 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4337 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4341 static void semantic_sub(binary_expression_t *expression)
4343 expression_t *const left = expression->left;
4344 expression_t *const right = expression->right;
4345 type_t *const orig_type_left = left->base.datatype;
4346 type_t *const orig_type_right = right->base.datatype;
4347 type_t *const type_left = skip_typeref(orig_type_left);
4348 type_t *const type_right = skip_typeref(orig_type_right);
4351 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4352 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4353 expression->left = create_implicit_cast(left, arithmetic_type);
4354 expression->right = create_implicit_cast(right, arithmetic_type);
4355 expression->expression.datatype = arithmetic_type;
4357 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4358 expression->expression.datatype = type_left;
4359 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4360 if(!pointers_compatible(type_left, type_right)) {
4361 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4363 expression->expression.datatype = type_ptrdiff_t;
4365 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4366 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4371 * Check the semantics of comparison expressions.
4373 * @param expression The expression to check.
4375 static void semantic_comparison(binary_expression_t *expression)
4377 expression_t *left = expression->left;
4378 expression_t *right = expression->right;
4379 type_t *orig_type_left = left->base.datatype;
4380 type_t *orig_type_right = right->base.datatype;
4382 type_t *type_left = skip_typeref(orig_type_left);
4383 type_t *type_right = skip_typeref(orig_type_right);
4385 /* TODO non-arithmetic types */
4386 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4387 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4388 expression->left = create_implicit_cast(left, arithmetic_type);
4389 expression->right = create_implicit_cast(right, arithmetic_type);
4390 expression->expression.datatype = arithmetic_type;
4391 if (warning.float_equal &&
4392 (expression->expression.kind == EXPR_BINARY_EQUAL ||
4393 expression->expression.kind == EXPR_BINARY_NOTEQUAL) &&
4394 is_type_floating(arithmetic_type)) {
4395 warningf(expression->expression.source_position,
4396 "comparing floating point with == or != is unsafe");
4398 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4399 /* TODO check compatibility */
4400 } else if (is_type_pointer(type_left)) {
4401 expression->right = create_implicit_cast(right, type_left);
4402 } else if (is_type_pointer(type_right)) {
4403 expression->left = create_implicit_cast(left, type_right);
4404 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4405 type_error_incompatible("invalid operands in comparison",
4406 expression->expression.source_position,
4407 type_left, type_right);
4409 expression->expression.datatype = type_int;
4412 static void semantic_arithmetic_assign(binary_expression_t *expression)
4414 expression_t *left = expression->left;
4415 expression_t *right = expression->right;
4416 type_t *orig_type_left = left->base.datatype;
4417 type_t *orig_type_right = right->base.datatype;
4419 type_t *type_left = skip_typeref(orig_type_left);
4420 type_t *type_right = skip_typeref(orig_type_right);
4422 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4423 /* TODO: improve error message */
4424 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4425 errorf(HERE, "operation needs arithmetic types");
4430 /* combined instructions are tricky. We can't create an implicit cast on
4431 * the left side, because we need the uncasted form for the store.
4432 * The ast2firm pass has to know that left_type must be right_type
4433 * for the arithmetic operation and create a cast by itself */
4434 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4435 expression->right = create_implicit_cast(right, arithmetic_type);
4436 expression->expression.datatype = type_left;
4439 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4441 expression_t *const left = expression->left;
4442 expression_t *const right = expression->right;
4443 type_t *const orig_type_left = left->base.datatype;
4444 type_t *const orig_type_right = right->base.datatype;
4445 type_t *const type_left = skip_typeref(orig_type_left);
4446 type_t *const type_right = skip_typeref(orig_type_right);
4448 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4449 /* combined instructions are tricky. We can't create an implicit cast on
4450 * the left side, because we need the uncasted form for the store.
4451 * The ast2firm pass has to know that left_type must be right_type
4452 * for the arithmetic operation and create a cast by itself */
4453 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4454 expression->right = create_implicit_cast(right, arithmetic_type);
4455 expression->expression.datatype = type_left;
4456 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4457 expression->expression.datatype = type_left;
4458 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4459 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4464 * Check the semantic restrictions of a logical expression.
4466 static void semantic_logical_op(binary_expression_t *expression)
4468 expression_t *const left = expression->left;
4469 expression_t *const right = expression->right;
4470 type_t *const orig_type_left = left->base.datatype;
4471 type_t *const orig_type_right = right->base.datatype;
4472 type_t *const type_left = skip_typeref(orig_type_left);
4473 type_t *const type_right = skip_typeref(orig_type_right);
4475 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4476 /* TODO: improve error message */
4477 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4478 errorf(HERE, "operation needs scalar types");
4483 expression->expression.datatype = type_int;
4487 * Checks if a compound type has constant fields.
4489 static bool has_const_fields(const compound_type_t *type)
4491 const scope_t *scope = &type->declaration->scope;
4492 const declaration_t *declaration = scope->declarations;
4494 for (; declaration != NULL; declaration = declaration->next) {
4495 if (declaration->namespc != NAMESPACE_NORMAL)
4498 const type_t *decl_type = skip_typeref(declaration->type);
4499 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4507 * Check the semantic restrictions of a binary assign expression.
4509 static void semantic_binexpr_assign(binary_expression_t *expression)
4511 expression_t *left = expression->left;
4512 type_t *orig_type_left = left->base.datatype;
4514 type_t *type_left = revert_automatic_type_conversion(left);
4515 type_left = skip_typeref(orig_type_left);
4517 /* must be a modifiable lvalue */
4518 if (is_type_array(type_left)) {
4519 errorf(HERE, "cannot assign to arrays ('%E')", left);
4522 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4523 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4527 if(is_type_incomplete(type_left)) {
4529 "left-hand side of assignment '%E' has incomplete type '%T'",
4530 left, orig_type_left);
4533 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4534 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4535 left, orig_type_left);
4539 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4541 if (res_type == NULL) {
4542 errorf(expression->expression.source_position,
4543 "cannot assign to '%T' from '%T'",
4544 orig_type_left, expression->right->base.datatype);
4546 expression->right = create_implicit_cast(expression->right, res_type);
4549 expression->expression.datatype = orig_type_left;
4552 static bool expression_has_effect(const expression_t *const expr)
4554 switch (expr->kind) {
4555 case EXPR_UNKNOWN: break;
4556 case EXPR_INVALID: break;
4557 case EXPR_REFERENCE: return false;
4558 case EXPR_CONST: return false;
4559 case EXPR_STRING_LITERAL: return false;
4560 case EXPR_WIDE_STRING_LITERAL: return false;
4562 const call_expression_t *const call = &expr->call;
4563 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4566 switch (call->function->builtin_symbol.symbol->ID) {
4567 case T___builtin_va_end: return true;
4568 default: return false;
4571 case EXPR_CONDITIONAL: {
4572 const conditional_expression_t *const cond = &expr->conditional;
4574 expression_has_effect(cond->true_expression) &&
4575 expression_has_effect(cond->false_expression);
4577 case EXPR_SELECT: return false;
4578 case EXPR_ARRAY_ACCESS: return false;
4579 case EXPR_SIZEOF: return false;
4580 case EXPR_CLASSIFY_TYPE: return false;
4581 case EXPR_ALIGNOF: return false;
4583 case EXPR_FUNCTION: return false;
4584 case EXPR_PRETTY_FUNCTION: return false;
4585 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4586 case EXPR_BUILTIN_CONSTANT_P: return false;
4587 case EXPR_BUILTIN_PREFETCH: return true;
4588 case EXPR_OFFSETOF: return false;
4589 case EXPR_VA_START: return true;
4590 case EXPR_VA_ARG: return true;
4591 case EXPR_STATEMENT: return true; // TODO
4593 case EXPR_UNARY_NEGATE: return false;
4594 case EXPR_UNARY_PLUS: return false;
4595 case EXPR_UNARY_BITWISE_NEGATE: return false;
4596 case EXPR_UNARY_NOT: return false;
4597 case EXPR_UNARY_DEREFERENCE: return false;
4598 case EXPR_UNARY_TAKE_ADDRESS: return false;
4599 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4600 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4601 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4602 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4603 case EXPR_UNARY_CAST:
4604 return is_type_atomic(expr->base.datatype, ATOMIC_TYPE_VOID);
4605 case EXPR_UNARY_CAST_IMPLICIT: return true;
4606 case EXPR_UNARY_ASSUME: return true;
4607 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4609 case EXPR_BINARY_ADD: return false;
4610 case EXPR_BINARY_SUB: return false;
4611 case EXPR_BINARY_MUL: return false;
4612 case EXPR_BINARY_DIV: return false;
4613 case EXPR_BINARY_MOD: return false;
4614 case EXPR_BINARY_EQUAL: return false;
4615 case EXPR_BINARY_NOTEQUAL: return false;
4616 case EXPR_BINARY_LESS: return false;
4617 case EXPR_BINARY_LESSEQUAL: return false;
4618 case EXPR_BINARY_GREATER: return false;
4619 case EXPR_BINARY_GREATEREQUAL: return false;
4620 case EXPR_BINARY_BITWISE_AND: return false;
4621 case EXPR_BINARY_BITWISE_OR: return false;
4622 case EXPR_BINARY_BITWISE_XOR: return false;
4623 case EXPR_BINARY_SHIFTLEFT: return false;
4624 case EXPR_BINARY_SHIFTRIGHT: return false;
4625 case EXPR_BINARY_ASSIGN: return true;
4626 case EXPR_BINARY_MUL_ASSIGN: return true;
4627 case EXPR_BINARY_DIV_ASSIGN: return true;
4628 case EXPR_BINARY_MOD_ASSIGN: return true;
4629 case EXPR_BINARY_ADD_ASSIGN: return true;
4630 case EXPR_BINARY_SUB_ASSIGN: return true;
4631 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4632 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4633 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4634 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4635 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4636 case EXPR_BINARY_LOGICAL_AND:
4637 case EXPR_BINARY_LOGICAL_OR:
4638 case EXPR_BINARY_COMMA:
4639 return expression_has_effect(expr->binary.right);
4641 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4642 case EXPR_BINARY_ISGREATER: return false;
4643 case EXPR_BINARY_ISGREATEREQUAL: return false;
4644 case EXPR_BINARY_ISLESS: return false;
4645 case EXPR_BINARY_ISLESSEQUAL: return false;
4646 case EXPR_BINARY_ISLESSGREATER: return false;
4647 case EXPR_BINARY_ISUNORDERED: return false;
4650 panic("unexpected statement");
4653 static void semantic_comma(binary_expression_t *expression)
4655 if (warning.unused_value) {
4656 const expression_t *const left = expression->left;
4657 if (!expression_has_effect(left)) {
4658 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4661 expression->expression.datatype = expression->right->base.datatype;
4664 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4665 static expression_t *parse_##binexpression_type(unsigned precedence, \
4666 expression_t *left) \
4669 source_position_t pos = HERE; \
4671 expression_t *right = parse_sub_expression(precedence + lr); \
4673 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4674 binexpr->base.source_position = pos; \
4675 binexpr->binary.left = left; \
4676 binexpr->binary.right = right; \
4677 sfunc(&binexpr->binary); \
4682 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4683 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4684 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4685 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4686 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4687 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4688 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4689 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4690 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4692 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4693 semantic_comparison, 1)
4694 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4695 semantic_comparison, 1)
4696 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4697 semantic_comparison, 1)
4698 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4699 semantic_comparison, 1)
4701 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4702 semantic_binexpr_arithmetic, 1)
4703 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4704 semantic_binexpr_arithmetic, 1)
4705 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4706 semantic_binexpr_arithmetic, 1)
4707 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4708 semantic_logical_op, 1)
4709 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4710 semantic_logical_op, 1)
4711 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4712 semantic_shift_op, 1)
4713 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4714 semantic_shift_op, 1)
4715 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4716 semantic_arithmetic_addsubb_assign, 0)
4717 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4718 semantic_arithmetic_addsubb_assign, 0)
4719 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4720 semantic_arithmetic_assign, 0)
4721 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4722 semantic_arithmetic_assign, 0)
4723 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4724 semantic_arithmetic_assign, 0)
4725 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4726 semantic_arithmetic_assign, 0)
4727 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4728 semantic_arithmetic_assign, 0)
4729 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4730 semantic_arithmetic_assign, 0)
4731 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4732 semantic_arithmetic_assign, 0)
4733 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4734 semantic_arithmetic_assign, 0)
4736 static expression_t *parse_sub_expression(unsigned precedence)
4738 if(token.type < 0) {
4739 return expected_expression_error();
4742 expression_parser_function_t *parser
4743 = &expression_parsers[token.type];
4744 source_position_t source_position = token.source_position;
4747 if(parser->parser != NULL) {
4748 left = parser->parser(parser->precedence);
4750 left = parse_primary_expression();
4752 assert(left != NULL);
4753 left->base.source_position = source_position;
4756 if(token.type < 0) {
4757 return expected_expression_error();
4760 parser = &expression_parsers[token.type];
4761 if(parser->infix_parser == NULL)
4763 if(parser->infix_precedence < precedence)
4766 left = parser->infix_parser(parser->infix_precedence, left);
4768 assert(left != NULL);
4769 assert(left->kind != EXPR_UNKNOWN);
4770 left->base.source_position = source_position;
4777 * Parse an expression.
4779 static expression_t *parse_expression(void)
4781 return parse_sub_expression(1);
4785 * Register a parser for a prefix-like operator with given precedence.
4787 * @param parser the parser function
4788 * @param token_type the token type of the prefix token
4789 * @param precedence the precedence of the operator
4791 static void register_expression_parser(parse_expression_function parser,
4792 int token_type, unsigned precedence)
4794 expression_parser_function_t *entry = &expression_parsers[token_type];
4796 if(entry->parser != NULL) {
4797 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4798 panic("trying to register multiple expression parsers for a token");
4800 entry->parser = parser;
4801 entry->precedence = precedence;
4805 * Register a parser for an infix operator with given precedence.
4807 * @param parser the parser function
4808 * @param token_type the token type of the infix operator
4809 * @param precedence the precedence of the operator
4811 static void register_infix_parser(parse_expression_infix_function parser,
4812 int token_type, unsigned precedence)
4814 expression_parser_function_t *entry = &expression_parsers[token_type];
4816 if(entry->infix_parser != NULL) {
4817 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4818 panic("trying to register multiple infix expression parsers for a "
4821 entry->infix_parser = parser;
4822 entry->infix_precedence = precedence;
4826 * Initialize the expression parsers.
4828 static void init_expression_parsers(void)
4830 memset(&expression_parsers, 0, sizeof(expression_parsers));
4832 register_infix_parser(parse_array_expression, '[', 30);
4833 register_infix_parser(parse_call_expression, '(', 30);
4834 register_infix_parser(parse_select_expression, '.', 30);
4835 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4836 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4838 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4841 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4842 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4843 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4844 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4845 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4846 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4847 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4848 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4849 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4850 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4851 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4852 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4853 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4854 T_EXCLAMATIONMARKEQUAL, 13);
4855 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4856 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4857 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4858 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4859 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4860 register_infix_parser(parse_conditional_expression, '?', 7);
4861 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4862 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4863 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4864 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4865 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4866 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4867 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4868 T_LESSLESSEQUAL, 2);
4869 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4870 T_GREATERGREATEREQUAL, 2);
4871 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4873 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4875 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4878 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4880 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4881 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4882 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4883 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4884 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4885 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4886 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4888 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4890 register_expression_parser(parse_sizeof, T_sizeof, 25);
4891 register_expression_parser(parse_extension, T___extension__, 25);
4892 register_expression_parser(parse_builtin_classify_type,
4893 T___builtin_classify_type, 25);
4897 * Parse a asm statement constraints specification.
4899 static asm_constraint_t *parse_asm_constraints(void)
4901 asm_constraint_t *result = NULL;
4902 asm_constraint_t *last = NULL;
4904 while(token.type == T_STRING_LITERAL || token.type == '[') {
4905 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4906 memset(constraint, 0, sizeof(constraint[0]));
4908 if(token.type == '[') {
4910 if(token.type != T_IDENTIFIER) {
4911 parse_error_expected("while parsing asm constraint",
4915 constraint->symbol = token.v.symbol;
4920 constraint->constraints = parse_string_literals();
4922 constraint->expression = parse_expression();
4926 last->next = constraint;
4928 result = constraint;
4932 if(token.type != ',')
4941 * Parse a asm statement clobber specification.
4943 static asm_clobber_t *parse_asm_clobbers(void)
4945 asm_clobber_t *result = NULL;
4946 asm_clobber_t *last = NULL;
4948 while(token.type == T_STRING_LITERAL) {
4949 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4950 clobber->clobber = parse_string_literals();
4953 last->next = clobber;
4959 if(token.type != ',')
4968 * Parse an asm statement.
4970 static statement_t *parse_asm_statement(void)
4974 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4975 statement->base.source_position = token.source_position;
4977 asm_statement_t *asm_statement = &statement->asms;
4979 if(token.type == T_volatile) {
4981 asm_statement->is_volatile = true;
4985 asm_statement->asm_text = parse_string_literals();
4987 if(token.type != ':')
4991 asm_statement->inputs = parse_asm_constraints();
4992 if(token.type != ':')
4996 asm_statement->outputs = parse_asm_constraints();
4997 if(token.type != ':')
5001 asm_statement->clobbers = parse_asm_clobbers();
5010 * Parse a case statement.
5012 static statement_t *parse_case_statement(void)
5016 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5018 statement->base.source_position = token.source_position;
5019 statement->case_label.expression = parse_expression();
5023 if (! is_constant_expression(statement->case_label.expression)) {
5024 errorf(statement->base.source_position,
5025 "case label does not reduce to an integer constant");
5027 /* TODO: check if the case label is already known */
5028 if (current_switch != NULL) {
5029 /* link all cases into the switch statement */
5030 if (current_switch->last_case == NULL) {
5031 current_switch->first_case =
5032 current_switch->last_case = &statement->case_label;
5034 current_switch->last_case->next = &statement->case_label;
5037 errorf(statement->base.source_position,
5038 "case label not within a switch statement");
5041 statement->case_label.label_statement = parse_statement();
5047 * Finds an existing default label of a switch statement.
5049 static case_label_statement_t *
5050 find_default_label(const switch_statement_t *statement)
5052 for (case_label_statement_t *label = statement->first_case;
5054 label = label->next) {
5055 if (label->expression == NULL)
5062 * Parse a default statement.
5064 static statement_t *parse_default_statement(void)
5068 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5070 statement->base.source_position = token.source_position;
5073 if (current_switch != NULL) {
5074 const case_label_statement_t *def_label = find_default_label(current_switch);
5075 if (def_label != NULL) {
5076 errorf(HERE, "multiple default labels in one switch");
5077 errorf(def_label->statement.source_position,
5078 "this is the first default label");
5080 /* link all cases into the switch statement */
5081 if (current_switch->last_case == NULL) {
5082 current_switch->first_case =
5083 current_switch->last_case = &statement->case_label;
5085 current_switch->last_case->next = &statement->case_label;
5089 errorf(statement->base.source_position,
5090 "'default' label not within a switch statement");
5092 statement->label.label_statement = parse_statement();
5098 * Return the declaration for a given label symbol or create a new one.
5100 static declaration_t *get_label(symbol_t *symbol)
5102 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5103 assert(current_function != NULL);
5104 /* if we found a label in the same function, then we already created the
5106 if(candidate != NULL
5107 && candidate->parent_scope == ¤t_function->scope) {
5111 /* otherwise we need to create a new one */
5112 declaration_t *const declaration = allocate_declaration_zero();
5113 declaration->namespc = NAMESPACE_LABEL;
5114 declaration->symbol = symbol;
5116 label_push(declaration);
5122 * Parse a label statement.
5124 static statement_t *parse_label_statement(void)
5126 assert(token.type == T_IDENTIFIER);
5127 symbol_t *symbol = token.v.symbol;
5130 declaration_t *label = get_label(symbol);
5132 /* if source position is already set then the label is defined twice,
5133 * otherwise it was just mentioned in a goto so far */
5134 if(label->source_position.input_name != NULL) {
5135 errorf(HERE, "duplicate label '%Y'", symbol);
5136 errorf(label->source_position, "previous definition of '%Y' was here",
5139 label->source_position = token.source_position;
5142 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5144 label_statement->statement.kind = STATEMENT_LABEL;
5145 label_statement->statement.source_position = token.source_position;
5146 label_statement->label = label;
5150 if(token.type == '}') {
5151 /* TODO only warn? */
5152 errorf(HERE, "label at end of compound statement");
5153 return (statement_t*) label_statement;
5155 if (token.type == ';') {
5156 /* eat an empty statement here, to avoid the warning about an empty
5157 * after a label. label:; is commonly used to have a label before
5161 label_statement->label_statement = parse_statement();
5165 /* remember the labels's in a list for later checking */
5166 if (label_last == NULL) {
5167 label_first = label_last = label_statement;
5169 label_last->next = label_statement;
5172 return (statement_t*) label_statement;
5176 * Parse an if statement.
5178 static statement_t *parse_if(void)
5182 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5183 statement->statement.kind = STATEMENT_IF;
5184 statement->statement.source_position = token.source_position;
5187 statement->condition = parse_expression();
5190 statement->true_statement = parse_statement();
5191 if(token.type == T_else) {
5193 statement->false_statement = parse_statement();
5196 return (statement_t*) statement;
5200 * Parse a switch statement.
5202 static statement_t *parse_switch(void)
5206 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5207 statement->statement.kind = STATEMENT_SWITCH;
5208 statement->statement.source_position = token.source_position;
5211 expression_t *const expr = parse_expression();
5212 type_t * type = skip_typeref(expr->base.datatype);
5213 if (is_type_integer(type)) {
5214 type = promote_integer(type);
5215 } else if (is_type_valid(type)) {
5216 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
5217 type = type_error_type;
5219 statement->expression = create_implicit_cast(expr, type);
5222 switch_statement_t *rem = current_switch;
5223 current_switch = statement;
5224 statement->body = parse_statement();
5225 current_switch = rem;
5227 if (warning.switch_default && find_default_label(statement) == NULL) {
5228 warningf(statement->statement.source_position, "switch has no default case");
5231 return (statement_t*) statement;
5234 static statement_t *parse_loop_body(statement_t *const loop)
5236 statement_t *const rem = current_loop;
5237 current_loop = loop;
5238 statement_t *const body = parse_statement();
5244 * Parse a while statement.
5246 static statement_t *parse_while(void)
5250 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5251 statement->statement.kind = STATEMENT_WHILE;
5252 statement->statement.source_position = token.source_position;
5255 statement->condition = parse_expression();
5258 statement->body = parse_loop_body((statement_t*)statement);
5260 return (statement_t*) statement;
5264 * Parse a do statement.
5266 static statement_t *parse_do(void)
5270 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5271 statement->statement.kind = STATEMENT_DO_WHILE;
5272 statement->statement.source_position = token.source_position;
5274 statement->body = parse_loop_body((statement_t*)statement);
5277 statement->condition = parse_expression();
5281 return (statement_t*) statement;
5285 * Parse a for statement.
5287 static statement_t *parse_for(void)
5291 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5292 statement->statement.kind = STATEMENT_FOR;
5293 statement->statement.source_position = token.source_position;
5297 int top = environment_top();
5298 scope_t *last_scope = scope;
5299 set_scope(&statement->scope);
5301 if(token.type != ';') {
5302 if(is_declaration_specifier(&token, false)) {
5303 parse_declaration(record_declaration);
5305 statement->initialisation = parse_expression();
5312 if(token.type != ';') {
5313 statement->condition = parse_expression();
5316 if(token.type != ')') {
5317 statement->step = parse_expression();
5320 statement->body = parse_loop_body((statement_t*)statement);
5322 assert(scope == &statement->scope);
5323 set_scope(last_scope);
5324 environment_pop_to(top);
5326 return (statement_t*) statement;
5330 * Parse a goto statement.
5332 static statement_t *parse_goto(void)
5336 if(token.type != T_IDENTIFIER) {
5337 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5341 symbol_t *symbol = token.v.symbol;
5344 declaration_t *label = get_label(symbol);
5346 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5348 statement->statement.kind = STATEMENT_GOTO;
5349 statement->statement.source_position = token.source_position;
5351 statement->label = label;
5353 /* remember the goto's in a list for later checking */
5354 if (goto_last == NULL) {
5355 goto_first = goto_last = statement;
5357 goto_last->next = statement;
5362 return (statement_t*) statement;
5366 * Parse a continue statement.
5368 static statement_t *parse_continue(void)
5370 statement_t *statement;
5371 if (current_loop == NULL) {
5372 errorf(HERE, "continue statement not within loop");
5375 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5377 statement->base.source_position = token.source_position;
5387 * Parse a break statement.
5389 static statement_t *parse_break(void)
5391 statement_t *statement;
5392 if (current_switch == NULL && current_loop == NULL) {
5393 errorf(HERE, "break statement not within loop or switch");
5396 statement = allocate_statement_zero(STATEMENT_BREAK);
5398 statement->base.source_position = token.source_position;
5408 * Check if a given declaration represents a local variable.
5410 static bool is_local_var_declaration(const declaration_t *declaration) {
5411 switch ((storage_class_tag_t) declaration->storage_class) {
5412 case STORAGE_CLASS_NONE:
5413 case STORAGE_CLASS_AUTO:
5414 case STORAGE_CLASS_REGISTER: {
5415 const type_t *type = skip_typeref(declaration->type);
5416 if(is_type_function(type)) {
5428 * Check if a given expression represents a local variable.
5430 static bool is_local_variable(const expression_t *expression)
5432 if (expression->base.kind != EXPR_REFERENCE) {
5435 const declaration_t *declaration = expression->reference.declaration;
5436 return is_local_var_declaration(declaration);
5440 * Parse a return statement.
5442 static statement_t *parse_return(void)
5446 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5448 statement->statement.kind = STATEMENT_RETURN;
5449 statement->statement.source_position = token.source_position;
5451 expression_t *return_value = NULL;
5452 if(token.type != ';') {
5453 return_value = parse_expression();
5457 const type_t *const func_type = current_function->type;
5458 assert(is_type_function(func_type));
5459 type_t *const return_type = skip_typeref(func_type->function.return_type);
5461 if(return_value != NULL) {
5462 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5464 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5465 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5466 warningf(statement->statement.source_position,
5467 "'return' with a value, in function returning void");
5468 return_value = NULL;
5470 type_t *const res_type = semantic_assign(return_type,
5471 return_value, "'return'");
5472 if (res_type == NULL) {
5473 errorf(statement->statement.source_position,
5474 "cannot return something of type '%T' in function returning '%T'",
5475 return_value->base.datatype, return_type);
5477 return_value = create_implicit_cast(return_value, res_type);
5480 /* check for returning address of a local var */
5481 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5482 const expression_t *expression = return_value->unary.value;
5483 if (is_local_variable(expression)) {
5484 warningf(statement->statement.source_position,
5485 "function returns address of local variable");
5489 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5490 warningf(statement->statement.source_position,
5491 "'return' without value, in function returning non-void");
5494 statement->return_value = return_value;
5496 return (statement_t*) statement;
5500 * Parse a declaration statement.
5502 static statement_t *parse_declaration_statement(void)
5504 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5506 statement->base.source_position = token.source_position;
5508 declaration_t *before = last_declaration;
5509 parse_declaration(record_declaration);
5511 if(before == NULL) {
5512 statement->declaration.declarations_begin = scope->declarations;
5514 statement->declaration.declarations_begin = before->next;
5516 statement->declaration.declarations_end = last_declaration;
5522 * Parse an expression statement, ie. expr ';'.
5524 static statement_t *parse_expression_statement(void)
5526 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5528 statement->base.source_position = token.source_position;
5529 expression_t *const expr = parse_expression();
5530 statement->expression.expression = expr;
5532 if (warning.unused_value && !expression_has_effect(expr)) {
5533 warningf(expr->base.source_position, "statement has no effect");
5542 * Parse a statement.
5544 static statement_t *parse_statement(void)
5546 statement_t *statement = NULL;
5548 /* declaration or statement */
5549 switch(token.type) {
5551 statement = parse_asm_statement();
5555 statement = parse_case_statement();
5559 statement = parse_default_statement();
5563 statement = parse_compound_statement();
5567 statement = parse_if();
5571 statement = parse_switch();
5575 statement = parse_while();
5579 statement = parse_do();
5583 statement = parse_for();
5587 statement = parse_goto();
5591 statement = parse_continue();
5595 statement = parse_break();
5599 statement = parse_return();
5603 if (warning.empty_statement) {
5604 warningf(HERE, "statement is empty");
5611 if(look_ahead(1)->type == ':') {
5612 statement = parse_label_statement();
5616 if(is_typedef_symbol(token.v.symbol)) {
5617 statement = parse_declaration_statement();
5621 statement = parse_expression_statement();
5624 case T___extension__:
5625 /* this can be a prefix to a declaration or an expression statement */
5626 /* we simply eat it now and parse the rest with tail recursion */
5629 } while(token.type == T___extension__);
5630 statement = parse_statement();
5634 statement = parse_declaration_statement();
5638 statement = parse_expression_statement();
5642 assert(statement == NULL
5643 || statement->base.source_position.input_name != NULL);
5649 * Parse a compound statement.
5651 static statement_t *parse_compound_statement(void)
5653 compound_statement_t *const compound_statement
5654 = allocate_ast_zero(sizeof(compound_statement[0]));
5655 compound_statement->statement.kind = STATEMENT_COMPOUND;
5656 compound_statement->statement.source_position = token.source_position;
5660 int top = environment_top();
5661 scope_t *last_scope = scope;
5662 set_scope(&compound_statement->scope);
5664 statement_t *last_statement = NULL;
5666 while(token.type != '}' && token.type != T_EOF) {
5667 statement_t *statement = parse_statement();
5668 if(statement == NULL)
5671 if(last_statement != NULL) {
5672 last_statement->base.next = statement;
5674 compound_statement->statements = statement;
5677 while(statement->base.next != NULL)
5678 statement = statement->base.next;
5680 last_statement = statement;
5683 if(token.type == '}') {
5686 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5689 assert(scope == &compound_statement->scope);
5690 set_scope(last_scope);
5691 environment_pop_to(top);
5693 return (statement_t*) compound_statement;
5697 * Initialize builtin types.
5699 static void initialize_builtin_types(void)
5701 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5702 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5703 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5704 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5705 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5706 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5707 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5708 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5710 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5711 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5712 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5713 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5717 * Check for unused functions in the given scope.
5719 static void check_unused_functions(const scope_t *scope) {
5720 bool first_err = true;
5721 const declaration_t *declaration = scope->declarations;
5723 for (; declaration != NULL; declaration = declaration->next) {
5724 if (! declaration->used) {
5725 if (declaration->storage_class == STORAGE_CLASS_STATIC) {
5726 const type_t *type = declaration->type;
5728 if (is_type_function(type)) {
5731 diagnosticf("%s: At top level:\n",
5732 declaration->source_position.input_name);
5734 warningf(declaration->source_position,
5735 "'%Y' defined but not used",
5736 declaration->symbol);
5744 * Parse a translation unit.
5746 static translation_unit_t *parse_translation_unit(void)
5748 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5750 assert(global_scope == NULL);
5751 global_scope = &unit->scope;
5753 assert(scope == NULL);
5754 set_scope(&unit->scope);
5756 initialize_builtin_types();
5758 while(token.type != T_EOF) {
5759 if (token.type == ';') {
5760 /* TODO error in strict mode */
5761 warningf(HERE, "stray ';' outside of function");
5764 parse_external_declaration();
5768 assert(scope == &unit->scope);
5770 last_declaration = NULL;
5772 assert(global_scope == &unit->scope);
5773 if (warning.unused_function) {
5774 check_unused_functions(global_scope);
5776 global_scope = NULL;
5784 * @return the translation unit or NULL if errors occurred.
5786 translation_unit_t *parse(void)
5788 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5789 label_stack = NEW_ARR_F(stack_entry_t, 0);
5790 diagnostic_count = 0;
5794 type_set_output(stderr);
5795 ast_set_output(stderr);
5797 lookahead_bufpos = 0;
5798 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5801 translation_unit_t *unit = parse_translation_unit();
5803 DEL_ARR_F(environment_stack);
5804 DEL_ARR_F(label_stack);
5813 * Initialize the parser.
5815 void init_parser(void)
5817 init_expression_parsers();
5818 obstack_init(&temp_obst);
5820 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5821 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5825 * Terminate the parser.
5827 void exit_parser(void)
5829 obstack_free(&temp_obst, NULL);