2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct declaration_specifiers_t declaration_specifiers_t;
52 struct declaration_specifiers_t {
53 source_position_t source_position;
54 unsigned char declared_storage_class;
56 decl_modifiers_t decl_modifiers;
60 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
63 static token_t lookahead_buffer[MAX_LOOKAHEAD];
64 static int lookahead_bufpos;
65 static stack_entry_t *environment_stack = NULL;
66 static stack_entry_t *label_stack = NULL;
67 static scope_t *global_scope = NULL;
68 static scope_t *scope = NULL;
69 static declaration_t *last_declaration = NULL;
70 static declaration_t *current_function = NULL;
71 static switch_statement_t *current_switch = NULL;
72 static statement_t *current_loop = NULL;
73 static goto_statement_t *goto_first = NULL;
74 static goto_statement_t *goto_last = NULL;
75 static label_statement_t *label_first = NULL;
76 static label_statement_t *label_last = NULL;
77 static struct obstack temp_obst;
79 /** The current source position. */
80 #define HERE token.source_position
82 static type_t *type_valist;
84 static statement_t *parse_compound_statement(void);
85 static statement_t *parse_statement(void);
87 static expression_t *parse_sub_expression(unsigned precedence);
88 static expression_t *parse_expression(void);
89 static type_t *parse_typename(void);
91 static void parse_compound_type_entries(declaration_t *compound_declaration);
92 static declaration_t *parse_declarator(
93 const declaration_specifiers_t *specifiers, bool may_be_abstract);
94 static declaration_t *record_declaration(declaration_t *declaration);
96 static void semantic_comparison(binary_expression_t *expression);
98 #define STORAGE_CLASSES \
105 #define TYPE_QUALIFIERS \
112 #ifdef PROVIDE_COMPLEX
113 #define COMPLEX_SPECIFIERS \
115 #define IMAGINARY_SPECIFIERS \
118 #define COMPLEX_SPECIFIERS
119 #define IMAGINARY_SPECIFIERS
122 #define TYPE_SPECIFIERS \
137 case T___builtin_va_list: \
141 #define DECLARATION_START \
146 #define TYPENAME_START \
151 * Allocate an AST node with given size and
152 * initialize all fields with zero.
154 static void *allocate_ast_zero(size_t size)
156 void *res = allocate_ast(size);
157 memset(res, 0, size);
161 static declaration_t *allocate_declaration_zero(void)
163 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
164 declaration->type = type_error_type;
169 * Returns the size of a statement node.
171 * @param kind the statement kind
173 static size_t get_statement_struct_size(statement_kind_t kind)
175 static const size_t sizes[] = {
176 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
177 [STATEMENT_RETURN] = sizeof(return_statement_t),
178 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
179 [STATEMENT_IF] = sizeof(if_statement_t),
180 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
181 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
182 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
183 [STATEMENT_BREAK] = sizeof(statement_base_t),
184 [STATEMENT_GOTO] = sizeof(goto_statement_t),
185 [STATEMENT_LABEL] = sizeof(label_statement_t),
186 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
187 [STATEMENT_WHILE] = sizeof(while_statement_t),
188 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
189 [STATEMENT_FOR] = sizeof(for_statement_t),
190 [STATEMENT_ASM] = sizeof(asm_statement_t)
192 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
193 assert(sizes[kind] != 0);
198 * Allocate a statement node of given kind and initialize all
201 static statement_t *allocate_statement_zero(statement_kind_t kind)
203 size_t size = get_statement_struct_size(kind);
204 statement_t *res = allocate_ast_zero(size);
206 res->base.kind = kind;
211 * Returns the size of an expression node.
213 * @param kind the expression kind
215 static size_t get_expression_struct_size(expression_kind_t kind)
217 static const size_t sizes[] = {
218 [EXPR_INVALID] = sizeof(expression_base_t),
219 [EXPR_REFERENCE] = sizeof(reference_expression_t),
220 [EXPR_CONST] = sizeof(const_expression_t),
221 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
222 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
223 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
224 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
225 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
226 [EXPR_CALL] = sizeof(call_expression_t),
227 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
228 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
229 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
230 [EXPR_SELECT] = sizeof(select_expression_t),
231 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
232 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
233 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
234 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
235 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
236 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
237 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
238 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
239 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
240 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
241 [EXPR_VA_START] = sizeof(va_start_expression_t),
242 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
243 [EXPR_STATEMENT] = sizeof(statement_expression_t),
245 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
246 return sizes[EXPR_UNARY_FIRST];
248 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
249 return sizes[EXPR_BINARY_FIRST];
251 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
252 assert(sizes[kind] != 0);
257 * Allocate an expression node of given kind and initialize all
260 static expression_t *allocate_expression_zero(expression_kind_t kind)
262 size_t size = get_expression_struct_size(kind);
263 expression_t *res = allocate_ast_zero(size);
265 res->base.kind = kind;
266 res->base.type = type_error_type;
271 * Returns the size of a type node.
273 * @param kind the type kind
275 static size_t get_type_struct_size(type_kind_t kind)
277 static const size_t sizes[] = {
278 [TYPE_ATOMIC] = sizeof(atomic_type_t),
279 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
280 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
281 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
282 [TYPE_ENUM] = sizeof(enum_type_t),
283 [TYPE_FUNCTION] = sizeof(function_type_t),
284 [TYPE_POINTER] = sizeof(pointer_type_t),
285 [TYPE_ARRAY] = sizeof(array_type_t),
286 [TYPE_BUILTIN] = sizeof(builtin_type_t),
287 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
288 [TYPE_TYPEOF] = sizeof(typeof_type_t),
290 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
291 assert(kind <= TYPE_TYPEOF);
292 assert(sizes[kind] != 0);
297 * Allocate a type node of given kind and initialize all
300 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
302 size_t size = get_type_struct_size(kind);
303 type_t *res = obstack_alloc(type_obst, size);
304 memset(res, 0, size);
306 res->base.kind = kind;
307 res->base.source_position = source_position;
312 * Returns the size of an initializer node.
314 * @param kind the initializer kind
316 static size_t get_initializer_size(initializer_kind_t kind)
318 static const size_t sizes[] = {
319 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
320 [INITIALIZER_STRING] = sizeof(initializer_string_t),
321 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
322 [INITIALIZER_LIST] = sizeof(initializer_list_t),
323 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
325 assert(kind < sizeof(sizes) / sizeof(*sizes));
326 assert(sizes[kind] != 0);
331 * Allocate an initializer node of given kind and initialize all
334 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
336 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
343 * Free a type from the type obstack.
345 static void free_type(void *type)
347 obstack_free(type_obst, type);
351 * Returns the index of the top element of the environment stack.
353 static size_t environment_top(void)
355 return ARR_LEN(environment_stack);
359 * Returns the index of the top element of the label stack.
361 static size_t label_top(void)
363 return ARR_LEN(label_stack);
368 * Return the next token.
370 static inline void next_token(void)
372 token = lookahead_buffer[lookahead_bufpos];
373 lookahead_buffer[lookahead_bufpos] = lexer_token;
376 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
379 print_token(stderr, &token);
380 fprintf(stderr, "\n");
385 * Return the next token with a given lookahead.
387 static inline const token_t *look_ahead(int num)
389 assert(num > 0 && num <= MAX_LOOKAHEAD);
390 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
391 return &lookahead_buffer[pos];
394 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
397 * Report a parse error because an expected token was not found.
399 static void parse_error_expected(const char *message, ...)
401 if(message != NULL) {
402 errorf(HERE, "%s", message);
405 va_start(ap, message);
406 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
411 * Report a type error.
413 static void type_error(const char *msg, const source_position_t source_position,
416 errorf(source_position, "%s, but found type '%T'", msg, type);
420 * Report an incompatible type.
422 static void type_error_incompatible(const char *msg,
423 const source_position_t source_position, type_t *type1, type_t *type2)
425 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
429 * Eat an complete block, ie. '{ ... }'.
431 static void eat_block(void)
433 if(token.type == '{')
436 while(token.type != '}') {
437 if(token.type == T_EOF)
439 if(token.type == '{') {
449 * Eat a statement until an ';' token.
451 static void eat_statement(void)
453 while(token.type != ';') {
454 if(token.type == T_EOF)
456 if(token.type == '}')
458 if(token.type == '{') {
468 * Eat a parenthesed term, ie. '( ... )'.
470 static void eat_paren(void)
472 if(token.type == '(')
475 while(token.type != ')') {
476 if(token.type == T_EOF)
478 if(token.type == ')' || token.type == ';' || token.type == '}') {
481 if(token.type == '(') {
485 if(token.type == '{') {
494 #define expect(expected) \
496 if(UNLIKELY(token.type != (expected))) { \
497 parse_error_expected(NULL, (expected), 0); \
504 #define expect_block(expected) \
506 if(UNLIKELY(token.type != (expected))) { \
507 parse_error_expected(NULL, (expected), 0); \
514 #define expect_void(expected) \
516 if(UNLIKELY(token.type != (expected))) { \
517 parse_error_expected(NULL, (expected), 0); \
524 static void set_scope(scope_t *new_scope)
527 scope->last_declaration = last_declaration;
531 last_declaration = new_scope->last_declaration;
535 * Search a symbol in a given namespace and returns its declaration or
536 * NULL if this symbol was not found.
538 static declaration_t *get_declaration(const symbol_t *const symbol,
539 const namespace_t namespc)
541 declaration_t *declaration = symbol->declaration;
542 for( ; declaration != NULL; declaration = declaration->symbol_next) {
543 if(declaration->namespc == namespc)
551 * pushs an environment_entry on the environment stack and links the
552 * corresponding symbol to the new entry
554 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
556 symbol_t *symbol = declaration->symbol;
557 namespace_t namespc = (namespace_t) declaration->namespc;
559 /* replace/add declaration into declaration list of the symbol */
560 declaration_t *iter = symbol->declaration;
562 symbol->declaration = declaration;
564 declaration_t *iter_last = NULL;
565 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
566 /* replace an entry? */
567 if(iter->namespc == namespc) {
568 if(iter_last == NULL) {
569 symbol->declaration = declaration;
571 iter_last->symbol_next = declaration;
573 declaration->symbol_next = iter->symbol_next;
578 assert(iter_last->symbol_next == NULL);
579 iter_last->symbol_next = declaration;
583 /* remember old declaration */
585 entry.symbol = symbol;
586 entry.old_declaration = iter;
587 entry.namespc = (unsigned short) namespc;
588 ARR_APP1(stack_entry_t, *stack_ptr, entry);
591 static void environment_push(declaration_t *declaration)
593 assert(declaration->source_position.input_name != NULL);
594 assert(declaration->parent_scope != NULL);
595 stack_push(&environment_stack, declaration);
598 static void label_push(declaration_t *declaration)
600 declaration->parent_scope = ¤t_function->scope;
601 stack_push(&label_stack, declaration);
605 * pops symbols from the environment stack until @p new_top is the top element
607 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
609 stack_entry_t *stack = *stack_ptr;
610 size_t top = ARR_LEN(stack);
613 assert(new_top <= top);
617 for(i = top; i > new_top; --i) {
618 stack_entry_t *entry = &stack[i - 1];
620 declaration_t *old_declaration = entry->old_declaration;
621 symbol_t *symbol = entry->symbol;
622 namespace_t namespc = (namespace_t)entry->namespc;
624 /* replace/remove declaration */
625 declaration_t *declaration = symbol->declaration;
626 assert(declaration != NULL);
627 if(declaration->namespc == namespc) {
628 if(old_declaration == NULL) {
629 symbol->declaration = declaration->symbol_next;
631 symbol->declaration = old_declaration;
634 declaration_t *iter_last = declaration;
635 declaration_t *iter = declaration->symbol_next;
636 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
637 /* replace an entry? */
638 if(iter->namespc == namespc) {
639 assert(iter_last != NULL);
640 iter_last->symbol_next = old_declaration;
641 if(old_declaration != NULL) {
642 old_declaration->symbol_next = iter->symbol_next;
647 assert(iter != NULL);
651 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
654 static void environment_pop_to(size_t new_top)
656 stack_pop_to(&environment_stack, new_top);
659 static void label_pop_to(size_t new_top)
661 stack_pop_to(&label_stack, new_top);
665 static int get_rank(const type_t *type)
667 assert(!is_typeref(type));
668 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
669 * and esp. footnote 108). However we can't fold constants (yet), so we
670 * can't decide whether unsigned int is possible, while int always works.
671 * (unsigned int would be preferable when possible... for stuff like
672 * struct { enum { ... } bla : 4; } ) */
673 if(type->kind == TYPE_ENUM)
674 return ATOMIC_TYPE_INT;
676 assert(type->kind == TYPE_ATOMIC);
677 return type->atomic.akind;
680 static type_t *promote_integer(type_t *type)
682 if(type->kind == TYPE_BITFIELD)
683 type = type->bitfield.base;
685 if(get_rank(type) < ATOMIC_TYPE_INT)
692 * Create a cast expression.
694 * @param expression the expression to cast
695 * @param dest_type the destination type
697 static expression_t *create_cast_expression(expression_t *expression,
700 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
702 cast->unary.value = expression;
703 cast->base.type = dest_type;
709 * Check if a given expression represents the 0 pointer constant.
711 static bool is_null_pointer_constant(const expression_t *expression)
713 /* skip void* cast */
714 if(expression->kind == EXPR_UNARY_CAST
715 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
716 expression = expression->unary.value;
719 /* TODO: not correct yet, should be any constant integer expression
720 * which evaluates to 0 */
721 if (expression->kind != EXPR_CONST)
724 type_t *const type = skip_typeref(expression->base.type);
725 if (!is_type_integer(type))
728 return expression->conste.v.int_value == 0;
732 * Create an implicit cast expression.
734 * @param expression the expression to cast
735 * @param dest_type the destination type
737 static expression_t *create_implicit_cast(expression_t *expression,
740 type_t *const source_type = expression->base.type;
742 if (source_type == dest_type)
745 return create_cast_expression(expression, dest_type);
748 /** Implements the rules from § 6.5.16.1 */
749 static type_t *semantic_assign(type_t *orig_type_left,
750 const expression_t *const right,
753 type_t *const orig_type_right = right->base.type;
754 type_t *const type_left = skip_typeref(orig_type_left);
755 type_t *const type_right = skip_typeref(orig_type_right);
757 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
758 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
759 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
760 && is_type_pointer(type_right))) {
761 return orig_type_left;
764 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
765 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
766 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
768 /* the left type has all qualifiers from the right type */
769 unsigned missing_qualifiers
770 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
771 if(missing_qualifiers != 0) {
772 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
773 return orig_type_left;
776 points_to_left = get_unqualified_type(points_to_left);
777 points_to_right = get_unqualified_type(points_to_right);
779 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
780 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
781 return orig_type_left;
784 if (!types_compatible(points_to_left, points_to_right)) {
785 warningf(right->base.source_position,
786 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
787 orig_type_left, context, right, orig_type_right);
790 return orig_type_left;
793 if ((is_type_compound(type_left) && is_type_compound(type_right))
794 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
795 type_t *const unqual_type_left = get_unqualified_type(type_left);
796 type_t *const unqual_type_right = get_unqualified_type(type_right);
797 if (types_compatible(unqual_type_left, unqual_type_right)) {
798 return orig_type_left;
802 if (!is_type_valid(type_left))
805 if (!is_type_valid(type_right))
806 return orig_type_right;
811 static expression_t *parse_constant_expression(void)
813 /* start parsing at precedence 7 (conditional expression) */
814 expression_t *result = parse_sub_expression(7);
816 if(!is_constant_expression(result)) {
817 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
823 static expression_t *parse_assignment_expression(void)
825 /* start parsing at precedence 2 (assignment expression) */
826 return parse_sub_expression(2);
829 static type_t *make_global_typedef(const char *name, type_t *type)
831 symbol_t *const symbol = symbol_table_insert(name);
833 declaration_t *const declaration = allocate_declaration_zero();
834 declaration->namespc = NAMESPACE_NORMAL;
835 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
836 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
837 declaration->type = type;
838 declaration->symbol = symbol;
839 declaration->source_position = builtin_source_position;
841 record_declaration(declaration);
843 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
844 typedef_type->typedeft.declaration = declaration;
849 static string_t parse_string_literals(void)
851 assert(token.type == T_STRING_LITERAL);
852 string_t result = token.v.string;
856 while (token.type == T_STRING_LITERAL) {
857 result = concat_strings(&result, &token.v.string);
864 static void parse_attributes(void)
868 case T___attribute__: {
876 errorf(HERE, "EOF while parsing attribute");
895 if(token.type != T_STRING_LITERAL) {
896 parse_error_expected("while parsing assembler attribute",
901 parse_string_literals();
906 goto attributes_finished;
914 static designator_t *parse_designation(void)
916 designator_t *result = NULL;
917 designator_t *last = NULL;
920 designator_t *designator;
923 designator = allocate_ast_zero(sizeof(designator[0]));
924 designator->source_position = token.source_position;
926 designator->array_index = parse_constant_expression();
930 designator = allocate_ast_zero(sizeof(designator[0]));
931 designator->source_position = token.source_position;
933 if(token.type != T_IDENTIFIER) {
934 parse_error_expected("while parsing designator",
938 designator->symbol = token.v.symbol;
946 assert(designator != NULL);
948 last->next = designator;
956 static initializer_t *initializer_from_string(array_type_t *type,
957 const string_t *const string)
959 /* TODO: check len vs. size of array type */
962 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
963 initializer->string.string = *string;
968 static initializer_t *initializer_from_wide_string(array_type_t *const type,
969 wide_string_t *const string)
971 /* TODO: check len vs. size of array type */
974 initializer_t *const initializer =
975 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
976 initializer->wide_string.string = *string;
981 static initializer_t *initializer_from_expression(type_t *orig_type,
982 expression_t *expression)
984 /* TODO check that expression is a constant expression */
986 /* § 6.7.8.14/15 char array may be initialized by string literals */
987 type_t *type = skip_typeref(orig_type);
988 type_t *expr_type_orig = expression->base.type;
989 type_t *expr_type = skip_typeref(expr_type_orig);
990 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
991 array_type_t *const array_type = &type->array;
992 type_t *const element_type = skip_typeref(array_type->element_type);
994 if (element_type->kind == TYPE_ATOMIC) {
995 atomic_type_kind_t akind = element_type->atomic.akind;
996 switch (expression->kind) {
997 case EXPR_STRING_LITERAL:
998 if (akind == ATOMIC_TYPE_CHAR
999 || akind == ATOMIC_TYPE_SCHAR
1000 || akind == ATOMIC_TYPE_UCHAR) {
1001 return initializer_from_string(array_type,
1002 &expression->string.value);
1005 case EXPR_WIDE_STRING_LITERAL: {
1006 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1007 if (get_unqualified_type(element_type) == bare_wchar_type) {
1008 return initializer_from_wide_string(array_type,
1009 &expression->wide_string.value);
1019 type_t *const res_type = semantic_assign(type, expression, "initializer");
1020 if (res_type == NULL)
1023 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1024 result->value.value = create_implicit_cast(expression, res_type);
1029 static bool is_initializer_constant(const expression_t *expression)
1031 return is_constant_expression(expression)
1032 || is_address_constant(expression);
1035 static initializer_t *parse_scalar_initializer(type_t *type,
1036 bool must_be_constant)
1038 /* there might be extra {} hierarchies */
1040 while(token.type == '{') {
1043 warningf(HERE, "extra curly braces around scalar initializer");
1048 expression_t *expression = parse_assignment_expression();
1049 if(must_be_constant && !is_initializer_constant(expression)) {
1050 errorf(expression->base.source_position,
1051 "Initialisation expression '%E' is not constant\n",
1055 initializer_t *initializer = initializer_from_expression(type, expression);
1057 if(initializer == NULL) {
1058 errorf(expression->base.source_position,
1059 "expression '%E' doesn't match expected type '%T'",
1065 bool additional_warning_displayed = false;
1067 if(token.type == ',') {
1070 if(token.type != '}') {
1071 if(!additional_warning_displayed) {
1072 warningf(HERE, "additional elements in scalar initializer");
1073 additional_warning_displayed = true;
1083 typedef struct type_path_entry_t type_path_entry_t;
1084 struct type_path_entry_t {
1088 declaration_t *compound_entry;
1092 typedef struct type_path_t type_path_t;
1093 struct type_path_t {
1094 type_path_entry_t *path;
1095 type_t *top_type; /**< type of the element the path points */
1096 size_t max_index; /**< largest index in outermost array */
1100 static __attribute__((unused)) void debug_print_type_path(
1101 const type_path_t *path)
1103 size_t len = ARR_LEN(path->path);
1106 fprintf(stderr, "invalid path");
1110 for(size_t i = 0; i < len; ++i) {
1111 const type_path_entry_t *entry = & path->path[i];
1113 type_t *type = skip_typeref(entry->type);
1114 if(is_type_compound(type)) {
1115 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1116 } else if(is_type_array(type)) {
1117 fprintf(stderr, "[%u]", entry->v.index);
1119 fprintf(stderr, "-INVALID-");
1122 fprintf(stderr, " (");
1123 print_type(path->top_type);
1124 fprintf(stderr, ")");
1127 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1129 size_t len = ARR_LEN(path->path);
1131 return & path->path[len-1];
1134 static type_path_entry_t *append_to_type_path(type_path_t *path)
1136 size_t len = ARR_LEN(path->path);
1137 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1139 type_path_entry_t *result = & path->path[len];
1140 memset(result, 0, sizeof(result[0]));
1144 static void descend_into_subtype(type_path_t *path)
1146 type_t *orig_top_type = path->top_type;
1147 type_t *top_type = skip_typeref(orig_top_type);
1149 assert(is_type_compound(top_type) || is_type_array(top_type));
1151 type_path_entry_t *top = append_to_type_path(path);
1152 top->type = top_type;
1154 if(is_type_compound(top_type)) {
1155 declaration_t *declaration = top_type->compound.declaration;
1156 declaration_t *entry = declaration->scope.declarations;
1158 top->v.compound_entry = entry;
1159 path->top_type = entry->type;
1161 assert(is_type_array(top_type));
1164 path->top_type = top_type->array.element_type;
1168 static void ascend_from_subtype(type_path_t *path)
1170 type_path_entry_t *top = get_type_path_top(path);
1172 path->top_type = top->type;
1174 size_t len = ARR_LEN(path->path);
1175 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1178 static void ascend_to(type_path_t *path, size_t top_path_level)
1180 size_t len = ARR_LEN(path->path);
1181 assert(len >= top_path_level);
1183 while(len > top_path_level) {
1184 ascend_from_subtype(path);
1185 len = ARR_LEN(path->path);
1189 static bool walk_designator(type_path_t *path, const designator_t *designator,
1190 bool used_in_offsetof)
1192 for( ; designator != NULL; designator = designator->next) {
1193 type_path_entry_t *top = get_type_path_top(path);
1194 type_t *orig_type = top->type;
1196 type_t *type = skip_typeref(orig_type);
1198 if(designator->symbol != NULL) {
1199 symbol_t *symbol = designator->symbol;
1200 if(!is_type_compound(type)) {
1201 if(is_type_valid(type)) {
1202 errorf(designator->source_position,
1203 "'.%Y' designator used for non-compound type '%T'",
1209 declaration_t *declaration = type->compound.declaration;
1210 declaration_t *iter = declaration->scope.declarations;
1211 for( ; iter != NULL; iter = iter->next) {
1212 if(iter->symbol == symbol) {
1217 errorf(designator->source_position,
1218 "'%T' has no member named '%Y'", orig_type, symbol);
1221 if(used_in_offsetof) {
1222 type_t *real_type = skip_typeref(iter->type);
1223 if(real_type->kind == TYPE_BITFIELD) {
1224 errorf(designator->source_position,
1225 "offsetof designator '%Y' may not specify bitfield",
1231 top->type = orig_type;
1232 top->v.compound_entry = iter;
1233 orig_type = iter->type;
1235 expression_t *array_index = designator->array_index;
1236 assert(designator->array_index != NULL);
1238 if(!is_type_array(type)) {
1239 if(is_type_valid(type)) {
1240 errorf(designator->source_position,
1241 "[%E] designator used for non-array type '%T'",
1242 array_index, orig_type);
1246 if(!is_type_valid(array_index->base.type)) {
1250 long index = fold_constant(array_index);
1251 if(!used_in_offsetof) {
1253 errorf(designator->source_position,
1254 "array index [%E] must be positive", array_index);
1257 if(type->array.size_constant == true) {
1258 long array_size = type->array.size;
1259 if(index >= array_size) {
1260 errorf(designator->source_position,
1261 "designator [%E] (%d) exceeds array size %d",
1262 array_index, index, array_size);
1268 top->type = orig_type;
1269 top->v.index = (size_t) index;
1270 orig_type = type->array.element_type;
1272 path->top_type = orig_type;
1274 if(designator->next != NULL) {
1275 descend_into_subtype(path);
1279 path->invalid = false;
1286 static void advance_current_object(type_path_t *path, size_t top_path_level)
1291 type_path_entry_t *top = get_type_path_top(path);
1293 type_t *type = skip_typeref(top->type);
1294 if(is_type_union(type)) {
1295 /* in unions only the first element is initialized */
1296 top->v.compound_entry = NULL;
1297 } else if(is_type_struct(type)) {
1298 declaration_t *entry = top->v.compound_entry;
1300 entry = entry->next;
1301 top->v.compound_entry = entry;
1303 path->top_type = entry->type;
1307 assert(is_type_array(type));
1311 if(!type->array.size_constant || top->v.index < type->array.size) {
1316 /* we're past the last member of the current sub-aggregate, try if we
1317 * can ascend in the type hierarchy and continue with another subobject */
1318 size_t len = ARR_LEN(path->path);
1320 if(len > top_path_level) {
1321 ascend_from_subtype(path);
1322 advance_current_object(path, top_path_level);
1324 path->invalid = true;
1328 static void skip_initializers(void)
1330 if(token.type == '{')
1333 while(token.type != '}') {
1334 if(token.type == T_EOF)
1336 if(token.type == '{') {
1344 static initializer_t *parse_sub_initializer(type_path_t *path,
1345 type_t *outer_type, size_t top_path_level, bool must_be_constant)
1347 type_t *orig_type = path->top_type;
1348 type_t *type = skip_typeref(orig_type);
1350 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1351 * initializers in this case. */
1352 if(!is_type_valid(type)) {
1353 skip_initializers();
1357 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1360 designator_t *designator = NULL;
1361 if(token.type == '.' || token.type == '[') {
1362 designator = parse_designation();
1364 /* reset path to toplevel, evaluate designator from there */
1365 ascend_to(path, top_path_level);
1366 if(!walk_designator(path, designator, false)) {
1367 /* can't continue after designation error */
1371 initializer_t *designator_initializer
1372 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1373 designator_initializer->designator.designator = designator;
1374 ARR_APP1(initializer_t*, initializers, designator_initializer);
1379 if(token.type == '{') {
1380 if(is_type_scalar(type)) {
1381 sub = parse_scalar_initializer(type, must_be_constant);
1384 descend_into_subtype(path);
1386 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1389 ascend_from_subtype(path);
1394 /* must be an expression */
1395 expression_t *expression = parse_assignment_expression();
1397 if(must_be_constant && !is_initializer_constant(expression)) {
1398 errorf(expression->base.source_position,
1399 "Initialisation expression '%E' is not constant\n",
1403 /* handle { "string" } special case */
1404 if((expression->kind == EXPR_STRING_LITERAL
1405 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1406 && outer_type != NULL) {
1407 sub = initializer_from_expression(outer_type, expression);
1409 if(token.type == ',') {
1412 if(token.type != '}') {
1413 warningf(HERE, "excessive elements in initializer for type '%T'",
1416 /* TODO: eat , ... */
1421 /* descend into subtypes until expression matches type */
1423 orig_type = path->top_type;
1424 type = skip_typeref(orig_type);
1426 sub = initializer_from_expression(orig_type, expression);
1430 if(!is_type_valid(type)) {
1433 if(is_type_scalar(type)) {
1434 errorf(expression->base.source_position,
1435 "expression '%E' doesn't match expected type '%T'",
1436 expression, orig_type);
1440 descend_into_subtype(path);
1444 /* update largest index of top array */
1445 const type_path_entry_t *first = &path->path[0];
1446 type_t *first_type = first->type;
1447 first_type = skip_typeref(first_type);
1448 if(is_type_array(first_type)) {
1449 size_t index = first->v.index;
1450 if(index > path->max_index)
1451 path->max_index = index;
1454 /* append to initializers list */
1455 ARR_APP1(initializer_t*, initializers, sub);
1457 if(token.type == '}') {
1461 if(token.type == '}') {
1465 advance_current_object(path, top_path_level);
1466 orig_type = path->top_type;
1467 type = skip_typeref(orig_type);
1470 size_t len = ARR_LEN(initializers);
1471 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1472 initializer_t *result = allocate_ast_zero(size);
1473 result->kind = INITIALIZER_LIST;
1474 result->list.len = len;
1475 memcpy(&result->list.initializers, initializers,
1476 len * sizeof(initializers[0]));
1478 ascend_to(path, top_path_level);
1483 skip_initializers();
1484 DEL_ARR_F(initializers);
1485 ascend_to(path, top_path_level);
1489 typedef struct parse_initializer_env_t {
1490 type_t *type; /* the type of the initializer. In case of an
1491 array type with unspecified size this gets
1492 adjusted to the actual size. */
1493 initializer_t *initializer; /* initializer will be filled in here */
1494 bool must_be_constant;
1495 } parse_initializer_env_t;
1497 static void parse_initializer(parse_initializer_env_t *env)
1499 type_t *type = skip_typeref(env->type);
1500 initializer_t *result = NULL;
1503 if(is_type_scalar(type)) {
1504 /* TODO: § 6.7.8.11; eat {} without warning */
1505 result = parse_scalar_initializer(type, env->must_be_constant);
1506 } else if(token.type == '{') {
1510 memset(&path, 0, sizeof(path));
1511 path.top_type = env->type;
1512 path.path = NEW_ARR_F(type_path_entry_t, 0);
1514 descend_into_subtype(&path);
1516 result = parse_sub_initializer(&path, env->type, 1,
1517 env->must_be_constant);
1519 max_index = path.max_index;
1520 DEL_ARR_F(path.path);
1524 /* parse_scalar_initializer also works in this case: we simply
1525 * have an expression without {} around it */
1526 result = parse_scalar_initializer(type, env->must_be_constant);
1529 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1530 * the array type size */
1531 if(is_type_array(type) && type->array.size_expression == NULL
1532 && result != NULL) {
1534 switch (result->kind) {
1535 case INITIALIZER_LIST:
1536 size = max_index + 1;
1539 case INITIALIZER_STRING:
1540 size = result->string.string.size;
1543 case INITIALIZER_WIDE_STRING:
1544 size = result->wide_string.string.size;
1548 panic("invalid initializer type");
1551 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1552 cnst->base.type = type_size_t;
1553 cnst->conste.v.int_value = size;
1555 type_t *new_type = duplicate_type(type);
1557 new_type->array.size_expression = cnst;
1558 new_type->array.size_constant = true;
1559 new_type->array.size = size;
1560 env->type = new_type;
1563 env->initializer = result;
1566 static declaration_t *append_declaration(declaration_t *declaration);
1568 static declaration_t *parse_compound_type_specifier(bool is_struct)
1576 symbol_t *symbol = NULL;
1577 declaration_t *declaration = NULL;
1579 if (token.type == T___attribute__) {
1584 if(token.type == T_IDENTIFIER) {
1585 symbol = token.v.symbol;
1589 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1591 declaration = get_declaration(symbol, NAMESPACE_UNION);
1593 } else if(token.type != '{') {
1595 parse_error_expected("while parsing struct type specifier",
1596 T_IDENTIFIER, '{', 0);
1598 parse_error_expected("while parsing union type specifier",
1599 T_IDENTIFIER, '{', 0);
1605 if(declaration == NULL) {
1606 declaration = allocate_declaration_zero();
1607 declaration->namespc =
1608 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1609 declaration->source_position = token.source_position;
1610 declaration->symbol = symbol;
1611 declaration->parent_scope = scope;
1612 if (symbol != NULL) {
1613 environment_push(declaration);
1615 append_declaration(declaration);
1618 if(token.type == '{') {
1619 if(declaration->init.is_defined) {
1620 assert(symbol != NULL);
1621 errorf(HERE, "multiple definitions of '%s %Y'",
1622 is_struct ? "struct" : "union", symbol);
1623 declaration->scope.declarations = NULL;
1625 declaration->init.is_defined = true;
1627 parse_compound_type_entries(declaration);
1634 static void parse_enum_entries(type_t *const enum_type)
1638 if(token.type == '}') {
1640 errorf(HERE, "empty enum not allowed");
1645 if(token.type != T_IDENTIFIER) {
1646 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1651 declaration_t *const entry = allocate_declaration_zero();
1652 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1653 entry->type = enum_type;
1654 entry->symbol = token.v.symbol;
1655 entry->source_position = token.source_position;
1658 if(token.type == '=') {
1660 expression_t *value = parse_constant_expression();
1662 value = create_implicit_cast(value, enum_type);
1663 entry->init.enum_value = value;
1668 record_declaration(entry);
1670 if(token.type != ',')
1673 } while(token.type != '}');
1678 static type_t *parse_enum_specifier(void)
1682 declaration_t *declaration;
1685 if(token.type == T_IDENTIFIER) {
1686 symbol = token.v.symbol;
1689 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1690 } else if(token.type != '{') {
1691 parse_error_expected("while parsing enum type specifier",
1692 T_IDENTIFIER, '{', 0);
1699 if(declaration == NULL) {
1700 declaration = allocate_declaration_zero();
1701 declaration->namespc = NAMESPACE_ENUM;
1702 declaration->source_position = token.source_position;
1703 declaration->symbol = symbol;
1704 declaration->parent_scope = scope;
1707 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1708 type->enumt.declaration = declaration;
1710 if(token.type == '{') {
1711 if(declaration->init.is_defined) {
1712 errorf(HERE, "multiple definitions of enum %Y", symbol);
1714 if (symbol != NULL) {
1715 environment_push(declaration);
1717 append_declaration(declaration);
1718 declaration->init.is_defined = 1;
1720 parse_enum_entries(type);
1728 * if a symbol is a typedef to another type, return true
1730 static bool is_typedef_symbol(symbol_t *symbol)
1732 const declaration_t *const declaration =
1733 get_declaration(symbol, NAMESPACE_NORMAL);
1735 declaration != NULL &&
1736 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1739 static type_t *parse_typeof(void)
1747 expression_t *expression = NULL;
1750 switch(token.type) {
1751 case T___extension__:
1752 /* this can be a prefix to a typename or an expression */
1753 /* we simply eat it now. */
1756 } while(token.type == T___extension__);
1760 if(is_typedef_symbol(token.v.symbol)) {
1761 type = parse_typename();
1763 expression = parse_expression();
1764 type = expression->base.type;
1769 type = parse_typename();
1773 expression = parse_expression();
1774 type = expression->base.type;
1780 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1781 typeof_type->typeoft.expression = expression;
1782 typeof_type->typeoft.typeof_type = type;
1788 SPECIFIER_SIGNED = 1 << 0,
1789 SPECIFIER_UNSIGNED = 1 << 1,
1790 SPECIFIER_LONG = 1 << 2,
1791 SPECIFIER_INT = 1 << 3,
1792 SPECIFIER_DOUBLE = 1 << 4,
1793 SPECIFIER_CHAR = 1 << 5,
1794 SPECIFIER_SHORT = 1 << 6,
1795 SPECIFIER_LONG_LONG = 1 << 7,
1796 SPECIFIER_FLOAT = 1 << 8,
1797 SPECIFIER_BOOL = 1 << 9,
1798 SPECIFIER_VOID = 1 << 10,
1799 #ifdef PROVIDE_COMPLEX
1800 SPECIFIER_COMPLEX = 1 << 11,
1801 SPECIFIER_IMAGINARY = 1 << 12,
1805 static type_t *create_builtin_type(symbol_t *const symbol,
1806 type_t *const real_type)
1808 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1809 type->builtin.symbol = symbol;
1810 type->builtin.real_type = real_type;
1812 type_t *result = typehash_insert(type);
1813 if (type != result) {
1820 static type_t *get_typedef_type(symbol_t *symbol)
1822 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1823 if(declaration == NULL
1824 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1827 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1828 type->typedeft.declaration = declaration;
1833 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1835 type_t *type = NULL;
1836 unsigned type_qualifiers = 0;
1837 unsigned type_specifiers = 0;
1840 specifiers->source_position = token.source_position;
1843 switch(token.type) {
1846 #define MATCH_STORAGE_CLASS(token, class) \
1848 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
1849 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1851 specifiers->declared_storage_class = class; \
1855 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1856 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1857 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1858 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1859 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1862 switch (specifiers->declared_storage_class) {
1863 case STORAGE_CLASS_NONE:
1864 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
1867 case STORAGE_CLASS_EXTERN:
1868 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
1871 case STORAGE_CLASS_STATIC:
1872 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
1876 errorf(HERE, "multiple storage classes in declaration specifiers");
1882 /* type qualifiers */
1883 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1885 type_qualifiers |= qualifier; \
1889 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1890 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1891 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1893 case T___extension__:
1898 /* type specifiers */
1899 #define MATCH_SPECIFIER(token, specifier, name) \
1902 if(type_specifiers & specifier) { \
1903 errorf(HERE, "multiple " name " type specifiers given"); \
1905 type_specifiers |= specifier; \
1909 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1910 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1911 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1912 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1913 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1914 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1915 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1916 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1917 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1918 #ifdef PROVIDE_COMPLEX
1919 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1920 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1923 /* only in microsoft mode */
1924 specifiers->decl_modifiers |= DM_FORCEINLINE;
1928 specifiers->is_inline = true;
1933 if(type_specifiers & SPECIFIER_LONG_LONG) {
1934 errorf(HERE, "multiple type specifiers given");
1935 } else if(type_specifiers & SPECIFIER_LONG) {
1936 type_specifiers |= SPECIFIER_LONG_LONG;
1938 type_specifiers |= SPECIFIER_LONG;
1943 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1945 type->compound.declaration = parse_compound_type_specifier(true);
1949 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1951 type->compound.declaration = parse_compound_type_specifier(false);
1955 type = parse_enum_specifier();
1958 type = parse_typeof();
1960 case T___builtin_va_list:
1961 type = duplicate_type(type_valist);
1965 case T___attribute__:
1969 case T_IDENTIFIER: {
1970 /* only parse identifier if we haven't found a type yet */
1971 if(type != NULL || type_specifiers != 0)
1972 goto finish_specifiers;
1974 type_t *typedef_type = get_typedef_type(token.v.symbol);
1976 if(typedef_type == NULL)
1977 goto finish_specifiers;
1980 type = typedef_type;
1984 /* function specifier */
1986 goto finish_specifiers;
1993 atomic_type_kind_t atomic_type;
1995 /* match valid basic types */
1996 switch(type_specifiers) {
1997 case SPECIFIER_VOID:
1998 atomic_type = ATOMIC_TYPE_VOID;
2000 case SPECIFIER_CHAR:
2001 atomic_type = ATOMIC_TYPE_CHAR;
2003 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2004 atomic_type = ATOMIC_TYPE_SCHAR;
2006 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2007 atomic_type = ATOMIC_TYPE_UCHAR;
2009 case SPECIFIER_SHORT:
2010 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2011 case SPECIFIER_SHORT | SPECIFIER_INT:
2012 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2013 atomic_type = ATOMIC_TYPE_SHORT;
2015 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2016 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2017 atomic_type = ATOMIC_TYPE_USHORT;
2020 case SPECIFIER_SIGNED:
2021 case SPECIFIER_SIGNED | SPECIFIER_INT:
2022 atomic_type = ATOMIC_TYPE_INT;
2024 case SPECIFIER_UNSIGNED:
2025 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2026 atomic_type = ATOMIC_TYPE_UINT;
2028 case SPECIFIER_LONG:
2029 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2030 case SPECIFIER_LONG | SPECIFIER_INT:
2031 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2032 atomic_type = ATOMIC_TYPE_LONG;
2034 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2035 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2036 atomic_type = ATOMIC_TYPE_ULONG;
2038 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2039 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2040 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2041 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2043 atomic_type = ATOMIC_TYPE_LONGLONG;
2045 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2046 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2048 atomic_type = ATOMIC_TYPE_ULONGLONG;
2050 case SPECIFIER_FLOAT:
2051 atomic_type = ATOMIC_TYPE_FLOAT;
2053 case SPECIFIER_DOUBLE:
2054 atomic_type = ATOMIC_TYPE_DOUBLE;
2056 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2057 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2059 case SPECIFIER_BOOL:
2060 atomic_type = ATOMIC_TYPE_BOOL;
2062 #ifdef PROVIDE_COMPLEX
2063 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2064 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2066 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2067 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2069 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2070 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2072 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2073 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2075 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2076 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2078 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2079 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2083 /* invalid specifier combination, give an error message */
2084 if(type_specifiers == 0) {
2085 if (! strict_mode) {
2086 if (warning.implicit_int) {
2087 warningf(HERE, "no type specifiers in declaration, using 'int'");
2089 atomic_type = ATOMIC_TYPE_INT;
2092 errorf(HERE, "no type specifiers given in declaration");
2094 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2095 (type_specifiers & SPECIFIER_UNSIGNED)) {
2096 errorf(HERE, "signed and unsigned specifiers gives");
2097 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2098 errorf(HERE, "only integer types can be signed or unsigned");
2100 errorf(HERE, "multiple datatypes in declaration");
2102 atomic_type = ATOMIC_TYPE_INVALID;
2105 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2106 type->atomic.akind = atomic_type;
2109 if(type_specifiers != 0) {
2110 errorf(HERE, "multiple datatypes in declaration");
2114 type->base.qualifiers = type_qualifiers;
2116 type_t *result = typehash_insert(type);
2117 if(newtype && result != type) {
2121 specifiers->type = result;
2124 static type_qualifiers_t parse_type_qualifiers(void)
2126 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2129 switch(token.type) {
2130 /* type qualifiers */
2131 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2132 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2133 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2136 return type_qualifiers;
2141 static declaration_t *parse_identifier_list(void)
2143 declaration_t *declarations = NULL;
2144 declaration_t *last_declaration = NULL;
2146 declaration_t *const declaration = allocate_declaration_zero();
2147 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2148 declaration->source_position = token.source_position;
2149 declaration->symbol = token.v.symbol;
2152 if(last_declaration != NULL) {
2153 last_declaration->next = declaration;
2155 declarations = declaration;
2157 last_declaration = declaration;
2159 if(token.type != ',')
2162 } while(token.type == T_IDENTIFIER);
2164 return declarations;
2167 static void semantic_parameter(declaration_t *declaration)
2169 /* TODO: improve error messages */
2171 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2172 errorf(HERE, "typedef not allowed in parameter list");
2173 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2174 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2175 errorf(HERE, "parameter may only have none or register storage class");
2178 type_t *const orig_type = declaration->type;
2179 type_t * type = skip_typeref(orig_type);
2181 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2182 * into a pointer. § 6.7.5.3 (7) */
2183 if (is_type_array(type)) {
2184 type_t *const element_type = type->array.element_type;
2186 type = make_pointer_type(element_type, type->base.qualifiers);
2188 declaration->type = type;
2191 if(is_type_incomplete(type)) {
2192 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2193 orig_type, declaration->symbol);
2197 static declaration_t *parse_parameter(void)
2199 declaration_specifiers_t specifiers;
2200 memset(&specifiers, 0, sizeof(specifiers));
2202 parse_declaration_specifiers(&specifiers);
2204 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2206 semantic_parameter(declaration);
2211 static declaration_t *parse_parameters(function_type_t *type)
2213 if(token.type == T_IDENTIFIER) {
2214 symbol_t *symbol = token.v.symbol;
2215 if(!is_typedef_symbol(symbol)) {
2216 type->kr_style_parameters = true;
2217 return parse_identifier_list();
2221 if(token.type == ')') {
2222 type->unspecified_parameters = 1;
2225 if(token.type == T_void && look_ahead(1)->type == ')') {
2230 declaration_t *declarations = NULL;
2231 declaration_t *declaration;
2232 declaration_t *last_declaration = NULL;
2233 function_parameter_t *parameter;
2234 function_parameter_t *last_parameter = NULL;
2237 switch(token.type) {
2241 return declarations;
2244 case T___extension__:
2246 declaration = parse_parameter();
2248 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2249 memset(parameter, 0, sizeof(parameter[0]));
2250 parameter->type = declaration->type;
2252 if(last_parameter != NULL) {
2253 last_declaration->next = declaration;
2254 last_parameter->next = parameter;
2256 type->parameters = parameter;
2257 declarations = declaration;
2259 last_parameter = parameter;
2260 last_declaration = declaration;
2264 return declarations;
2266 if(token.type != ',')
2267 return declarations;
2277 } construct_type_kind_t;
2279 typedef struct construct_type_t construct_type_t;
2280 struct construct_type_t {
2281 construct_type_kind_t kind;
2282 construct_type_t *next;
2285 typedef struct parsed_pointer_t parsed_pointer_t;
2286 struct parsed_pointer_t {
2287 construct_type_t construct_type;
2288 type_qualifiers_t type_qualifiers;
2291 typedef struct construct_function_type_t construct_function_type_t;
2292 struct construct_function_type_t {
2293 construct_type_t construct_type;
2294 type_t *function_type;
2297 typedef struct parsed_array_t parsed_array_t;
2298 struct parsed_array_t {
2299 construct_type_t construct_type;
2300 type_qualifiers_t type_qualifiers;
2306 typedef struct construct_base_type_t construct_base_type_t;
2307 struct construct_base_type_t {
2308 construct_type_t construct_type;
2312 static construct_type_t *parse_pointer_declarator(void)
2316 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2317 memset(pointer, 0, sizeof(pointer[0]));
2318 pointer->construct_type.kind = CONSTRUCT_POINTER;
2319 pointer->type_qualifiers = parse_type_qualifiers();
2321 return (construct_type_t*) pointer;
2324 static construct_type_t *parse_array_declarator(void)
2328 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2329 memset(array, 0, sizeof(array[0]));
2330 array->construct_type.kind = CONSTRUCT_ARRAY;
2332 if(token.type == T_static) {
2333 array->is_static = true;
2337 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2338 if(type_qualifiers != 0) {
2339 if(token.type == T_static) {
2340 array->is_static = true;
2344 array->type_qualifiers = type_qualifiers;
2346 if(token.type == '*' && look_ahead(1)->type == ']') {
2347 array->is_variable = true;
2349 } else if(token.type != ']') {
2350 array->size = parse_assignment_expression();
2355 return (construct_type_t*) array;
2358 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2363 if(declaration != NULL) {
2364 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2366 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2369 declaration_t *parameters = parse_parameters(&type->function);
2370 if(declaration != NULL) {
2371 declaration->scope.declarations = parameters;
2374 construct_function_type_t *construct_function_type =
2375 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2376 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2377 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2378 construct_function_type->function_type = type;
2382 return (construct_type_t*) construct_function_type;
2385 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2386 bool may_be_abstract)
2388 /* construct a single linked list of construct_type_t's which describe
2389 * how to construct the final declarator type */
2390 construct_type_t *first = NULL;
2391 construct_type_t *last = NULL;
2394 while(token.type == '*') {
2395 construct_type_t *type = parse_pointer_declarator();
2406 /* TODO: find out if this is correct */
2409 construct_type_t *inner_types = NULL;
2411 switch(token.type) {
2413 if(declaration == NULL) {
2414 errorf(HERE, "no identifier expected in typename");
2416 declaration->symbol = token.v.symbol;
2417 declaration->source_position = token.source_position;
2423 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2429 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2430 /* avoid a loop in the outermost scope, because eat_statement doesn't
2432 if(token.type == '}' && current_function == NULL) {
2440 construct_type_t *p = last;
2443 construct_type_t *type;
2444 switch(token.type) {
2446 type = parse_function_declarator(declaration);
2449 type = parse_array_declarator();
2452 goto declarator_finished;
2455 /* insert in the middle of the list (behind p) */
2457 type->next = p->next;
2468 declarator_finished:
2471 /* append inner_types at the end of the list, we don't to set last anymore
2472 * as it's not needed anymore */
2474 assert(first == NULL);
2475 first = inner_types;
2477 last->next = inner_types;
2483 static type_t *construct_declarator_type(construct_type_t *construct_list,
2486 construct_type_t *iter = construct_list;
2487 for( ; iter != NULL; iter = iter->next) {
2488 switch(iter->kind) {
2489 case CONSTRUCT_INVALID:
2490 panic("invalid type construction found");
2491 case CONSTRUCT_FUNCTION: {
2492 construct_function_type_t *construct_function_type
2493 = (construct_function_type_t*) iter;
2495 type_t *function_type = construct_function_type->function_type;
2497 function_type->function.return_type = type;
2499 type_t *skipped_return_type = skip_typeref(type);
2500 if (is_type_function(skipped_return_type)) {
2501 errorf(HERE, "function returning function is not allowed");
2502 type = type_error_type;
2503 } else if (is_type_array(skipped_return_type)) {
2504 errorf(HERE, "function returning array is not allowed");
2505 type = type_error_type;
2507 type = function_type;
2512 case CONSTRUCT_POINTER: {
2513 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2514 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2515 pointer_type->pointer.points_to = type;
2516 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2518 type = pointer_type;
2522 case CONSTRUCT_ARRAY: {
2523 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2524 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2526 expression_t *size_expression = parsed_array->size;
2527 if(size_expression != NULL) {
2529 = create_implicit_cast(size_expression, type_size_t);
2532 array_type->base.qualifiers = parsed_array->type_qualifiers;
2533 array_type->array.element_type = type;
2534 array_type->array.is_static = parsed_array->is_static;
2535 array_type->array.is_variable = parsed_array->is_variable;
2536 array_type->array.size_expression = size_expression;
2538 if(size_expression != NULL) {
2539 if(is_constant_expression(size_expression)) {
2540 array_type->array.size_constant = true;
2541 array_type->array.size
2542 = fold_constant(size_expression);
2544 array_type->array.is_vla = true;
2548 type_t *skipped_type = skip_typeref(type);
2549 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2550 errorf(HERE, "array of void is not allowed");
2551 type = type_error_type;
2559 type_t *hashed_type = typehash_insert(type);
2560 if(hashed_type != type) {
2561 /* the function type was constructed earlier freeing it here will
2562 * destroy other types... */
2563 if(iter->kind != CONSTRUCT_FUNCTION) {
2573 static declaration_t *parse_declarator(
2574 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2576 declaration_t *const declaration = allocate_declaration_zero();
2577 declaration->declared_storage_class = specifiers->declared_storage_class;
2578 declaration->modifiers = specifiers->decl_modifiers;
2579 declaration->is_inline = specifiers->is_inline;
2581 declaration->storage_class = specifiers->declared_storage_class;
2582 if(declaration->storage_class == STORAGE_CLASS_NONE
2583 && scope != global_scope) {
2584 declaration->storage_class = STORAGE_CLASS_AUTO;
2587 construct_type_t *construct_type
2588 = parse_inner_declarator(declaration, may_be_abstract);
2589 type_t *const type = specifiers->type;
2590 declaration->type = construct_declarator_type(construct_type, type);
2592 if(construct_type != NULL) {
2593 obstack_free(&temp_obst, construct_type);
2599 static type_t *parse_abstract_declarator(type_t *base_type)
2601 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2603 type_t *result = construct_declarator_type(construct_type, base_type);
2604 if(construct_type != NULL) {
2605 obstack_free(&temp_obst, construct_type);
2611 static declaration_t *append_declaration(declaration_t* const declaration)
2613 if (last_declaration != NULL) {
2614 last_declaration->next = declaration;
2616 scope->declarations = declaration;
2618 last_declaration = declaration;
2623 * Check if the declaration of main is suspicious. main should be a
2624 * function with external linkage, returning int, taking either zero
2625 * arguments, two, or three arguments of appropriate types, ie.
2627 * int main([ int argc, char **argv [, char **env ] ]).
2629 * @param decl the declaration to check
2630 * @param type the function type of the declaration
2632 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2634 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2635 warningf(decl->source_position, "'main' is normally a non-static function");
2637 if (skip_typeref(func_type->return_type) != type_int) {
2638 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2640 const function_parameter_t *parm = func_type->parameters;
2642 type_t *const first_type = parm->type;
2643 if (!types_compatible(skip_typeref(first_type), type_int)) {
2644 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2648 type_t *const second_type = parm->type;
2649 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2650 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2654 type_t *const third_type = parm->type;
2655 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2656 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2660 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2664 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2670 * Check if a symbol is the equal to "main".
2672 static bool is_sym_main(const symbol_t *const sym)
2674 return strcmp(sym->string, "main") == 0;
2677 static declaration_t *internal_record_declaration(
2678 declaration_t *const declaration,
2679 const bool is_function_definition)
2681 const symbol_t *const symbol = declaration->symbol;
2682 const namespace_t namespc = (namespace_t)declaration->namespc;
2684 type_t *const orig_type = declaration->type;
2685 type_t *const type = skip_typeref(orig_type);
2686 if (is_type_function(type) &&
2687 type->function.unspecified_parameters &&
2688 warning.strict_prototypes) {
2689 warningf(declaration->source_position,
2690 "function declaration '%#T' is not a prototype",
2691 orig_type, declaration->symbol);
2694 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2695 check_type_of_main(declaration, &type->function);
2698 assert(declaration->symbol != NULL);
2699 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2701 assert(declaration != previous_declaration);
2702 if (previous_declaration != NULL) {
2703 if (previous_declaration->parent_scope == scope) {
2704 /* can happen for K&R style declarations */
2705 if(previous_declaration->type == NULL) {
2706 previous_declaration->type = declaration->type;
2709 const type_t *prev_type = skip_typeref(previous_declaration->type);
2710 if (!types_compatible(type, prev_type)) {
2711 errorf(declaration->source_position,
2712 "declaration '%#T' is incompatible with "
2713 "previous declaration '%#T'",
2714 orig_type, symbol, previous_declaration->type, symbol);
2715 errorf(previous_declaration->source_position,
2716 "previous declaration of '%Y' was here", symbol);
2718 unsigned old_storage_class
2719 = previous_declaration->storage_class;
2720 unsigned new_storage_class = declaration->storage_class;
2722 if(is_type_incomplete(prev_type)) {
2723 previous_declaration->type = type;
2727 /* pretend no storage class means extern for function
2728 * declarations (except if the previous declaration is neither
2729 * none nor extern) */
2730 if (is_type_function(type)) {
2731 switch (old_storage_class) {
2732 case STORAGE_CLASS_NONE:
2733 old_storage_class = STORAGE_CLASS_EXTERN;
2735 case STORAGE_CLASS_EXTERN:
2736 if (is_function_definition) {
2737 if (warning.missing_prototypes &&
2738 prev_type->function.unspecified_parameters &&
2739 !is_sym_main(symbol)) {
2740 warningf(declaration->source_position,
2741 "no previous prototype for '%#T'",
2744 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2745 new_storage_class = STORAGE_CLASS_EXTERN;
2753 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2754 new_storage_class == STORAGE_CLASS_EXTERN) {
2755 warn_redundant_declaration:
2756 if (warning.redundant_decls) {
2757 warningf(declaration->source_position,
2758 "redundant declaration for '%Y'", symbol);
2759 warningf(previous_declaration->source_position,
2760 "previous declaration of '%Y' was here",
2763 } else if (current_function == NULL) {
2764 if (old_storage_class != STORAGE_CLASS_STATIC &&
2765 new_storage_class == STORAGE_CLASS_STATIC) {
2766 errorf(declaration->source_position,
2767 "static declaration of '%Y' follows non-static declaration",
2769 errorf(previous_declaration->source_position,
2770 "previous declaration of '%Y' was here", symbol);
2772 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2773 goto warn_redundant_declaration;
2775 if (new_storage_class == STORAGE_CLASS_NONE) {
2776 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2777 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
2781 if (old_storage_class == new_storage_class) {
2782 errorf(declaration->source_position,
2783 "redeclaration of '%Y'", symbol);
2785 errorf(declaration->source_position,
2786 "redeclaration of '%Y' with different linkage",
2789 errorf(previous_declaration->source_position,
2790 "previous declaration of '%Y' was here", symbol);
2793 return previous_declaration;
2795 } else if (is_function_definition) {
2796 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2797 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2798 warningf(declaration->source_position,
2799 "no previous prototype for '%#T'", orig_type, symbol);
2800 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2801 warningf(declaration->source_position,
2802 "no previous declaration for '%#T'", orig_type,
2806 } else if (warning.missing_declarations &&
2807 scope == global_scope &&
2808 !is_type_function(type) && (
2809 declaration->storage_class == STORAGE_CLASS_NONE ||
2810 declaration->storage_class == STORAGE_CLASS_THREAD
2812 warningf(declaration->source_position,
2813 "no previous declaration for '%#T'", orig_type, symbol);
2816 assert(declaration->parent_scope == NULL);
2817 assert(scope != NULL);
2819 declaration->parent_scope = scope;
2821 environment_push(declaration);
2822 return append_declaration(declaration);
2825 static declaration_t *record_declaration(declaration_t *declaration)
2827 return internal_record_declaration(declaration, false);
2830 static declaration_t *record_function_definition(declaration_t *declaration)
2832 return internal_record_declaration(declaration, true);
2835 static void parser_error_multiple_definition(declaration_t *declaration,
2836 const source_position_t source_position)
2838 errorf(source_position, "multiple definition of symbol '%Y'",
2839 declaration->symbol);
2840 errorf(declaration->source_position,
2841 "this is the location of the previous definition.");
2844 static bool is_declaration_specifier(const token_t *token,
2845 bool only_type_specifiers)
2847 switch(token->type) {
2851 return is_typedef_symbol(token->v.symbol);
2853 case T___extension__:
2856 return !only_type_specifiers;
2863 static void parse_init_declarator_rest(declaration_t *declaration)
2867 type_t *orig_type = declaration->type;
2868 type_t *type = skip_typeref(orig_type);
2870 if(declaration->init.initializer != NULL) {
2871 parser_error_multiple_definition(declaration, token.source_position);
2874 bool must_be_constant = false;
2875 if(declaration->storage_class == STORAGE_CLASS_STATIC
2876 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
2877 || declaration->parent_scope == global_scope) {
2878 must_be_constant = true;
2881 parse_initializer_env_t env;
2882 env.type = orig_type;
2883 env.must_be_constant = must_be_constant;
2884 parse_initializer(&env);
2886 if(env.type != orig_type) {
2887 orig_type = env.type;
2888 type = skip_typeref(orig_type);
2889 declaration->type = env.type;
2892 if(is_type_function(type)) {
2893 errorf(declaration->source_position,
2894 "initializers not allowed for function types at declator '%Y' (type '%T')",
2895 declaration->symbol, orig_type);
2897 declaration->init.initializer = env.initializer;
2901 /* parse rest of a declaration without any declarator */
2902 static void parse_anonymous_declaration_rest(
2903 const declaration_specifiers_t *specifiers,
2904 parsed_declaration_func finished_declaration)
2908 declaration_t *const declaration = allocate_declaration_zero();
2909 declaration->type = specifiers->type;
2910 declaration->declared_storage_class = specifiers->declared_storage_class;
2911 declaration->source_position = specifiers->source_position;
2913 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
2914 warningf(declaration->source_position, "useless storage class in empty declaration");
2916 declaration->storage_class = STORAGE_CLASS_NONE;
2918 type_t *type = declaration->type;
2919 switch (type->kind) {
2920 case TYPE_COMPOUND_STRUCT:
2921 case TYPE_COMPOUND_UNION: {
2922 if (type->compound.declaration->symbol == NULL) {
2923 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2932 warningf(declaration->source_position, "empty declaration");
2936 finished_declaration(declaration);
2939 static void parse_declaration_rest(declaration_t *ndeclaration,
2940 const declaration_specifiers_t *specifiers,
2941 parsed_declaration_func finished_declaration)
2944 declaration_t *declaration = finished_declaration(ndeclaration);
2946 type_t *orig_type = declaration->type;
2947 type_t *type = skip_typeref(orig_type);
2949 if (type->kind != TYPE_FUNCTION &&
2950 declaration->is_inline &&
2951 is_type_valid(type)) {
2952 warningf(declaration->source_position,
2953 "variable '%Y' declared 'inline'\n", declaration->symbol);
2956 if(token.type == '=') {
2957 parse_init_declarator_rest(declaration);
2960 if(token.type != ',')
2964 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2969 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2971 symbol_t *symbol = declaration->symbol;
2972 if(symbol == NULL) {
2973 errorf(HERE, "anonymous declaration not valid as function parameter");
2976 namespace_t namespc = (namespace_t) declaration->namespc;
2977 if(namespc != NAMESPACE_NORMAL) {
2978 return record_declaration(declaration);
2981 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2982 if(previous_declaration == NULL ||
2983 previous_declaration->parent_scope != scope) {
2984 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2989 if(previous_declaration->type == NULL) {
2990 previous_declaration->type = declaration->type;
2991 previous_declaration->declared_storage_class = declaration->declared_storage_class;
2992 previous_declaration->storage_class = declaration->storage_class;
2993 previous_declaration->parent_scope = scope;
2994 return previous_declaration;
2996 return record_declaration(declaration);
3000 static void parse_declaration(parsed_declaration_func finished_declaration)
3002 declaration_specifiers_t specifiers;
3003 memset(&specifiers, 0, sizeof(specifiers));
3004 parse_declaration_specifiers(&specifiers);
3006 if(token.type == ';') {
3007 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3009 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3010 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3014 static void parse_kr_declaration_list(declaration_t *declaration)
3016 type_t *type = skip_typeref(declaration->type);
3017 if(!is_type_function(type))
3020 if(!type->function.kr_style_parameters)
3023 /* push function parameters */
3024 int top = environment_top();
3025 scope_t *last_scope = scope;
3026 set_scope(&declaration->scope);
3028 declaration_t *parameter = declaration->scope.declarations;
3029 for( ; parameter != NULL; parameter = parameter->next) {
3030 assert(parameter->parent_scope == NULL);
3031 parameter->parent_scope = scope;
3032 environment_push(parameter);
3035 /* parse declaration list */
3036 while(is_declaration_specifier(&token, false)) {
3037 parse_declaration(finished_kr_declaration);
3040 /* pop function parameters */
3041 assert(scope == &declaration->scope);
3042 set_scope(last_scope);
3043 environment_pop_to(top);
3045 /* update function type */
3046 type_t *new_type = duplicate_type(type);
3047 new_type->function.kr_style_parameters = false;
3049 function_parameter_t *parameters = NULL;
3050 function_parameter_t *last_parameter = NULL;
3052 declaration_t *parameter_declaration = declaration->scope.declarations;
3053 for( ; parameter_declaration != NULL;
3054 parameter_declaration = parameter_declaration->next) {
3055 type_t *parameter_type = parameter_declaration->type;
3056 if(parameter_type == NULL) {
3058 errorf(HERE, "no type specified for function parameter '%Y'",
3059 parameter_declaration->symbol);
3061 if (warning.implicit_int) {
3062 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3063 parameter_declaration->symbol);
3065 parameter_type = type_int;
3066 parameter_declaration->type = parameter_type;
3070 semantic_parameter(parameter_declaration);
3071 parameter_type = parameter_declaration->type;
3073 function_parameter_t *function_parameter
3074 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3075 memset(function_parameter, 0, sizeof(function_parameter[0]));
3077 function_parameter->type = parameter_type;
3078 if(last_parameter != NULL) {
3079 last_parameter->next = function_parameter;
3081 parameters = function_parameter;
3083 last_parameter = function_parameter;
3085 new_type->function.parameters = parameters;
3087 type = typehash_insert(new_type);
3088 if(type != new_type) {
3089 obstack_free(type_obst, new_type);
3092 declaration->type = type;
3095 static bool first_err = true;
3098 * When called with first_err set, prints the name of the current function,
3101 static void print_in_function(void) {
3104 diagnosticf("%s: In function '%Y':\n",
3105 current_function->source_position.input_name,
3106 current_function->symbol);
3111 * Check if all labels are defined in the current function.
3112 * Check if all labels are used in the current function.
3114 static void check_labels(void)
3116 for (const goto_statement_t *goto_statement = goto_first;
3117 goto_statement != NULL;
3118 goto_statement = goto_statement->next) {
3119 declaration_t *label = goto_statement->label;
3122 if (label->source_position.input_name == NULL) {
3123 print_in_function();
3124 errorf(goto_statement->base.source_position,
3125 "label '%Y' used but not defined", label->symbol);
3128 goto_first = goto_last = NULL;
3130 if (warning.unused_label) {
3131 for (const label_statement_t *label_statement = label_first;
3132 label_statement != NULL;
3133 label_statement = label_statement->next) {
3134 const declaration_t *label = label_statement->label;
3136 if (! label->used) {
3137 print_in_function();
3138 warningf(label_statement->base.source_position,
3139 "label '%Y' defined but not used", label->symbol);
3143 label_first = label_last = NULL;
3147 * Check declarations of current_function for unused entities.
3149 static void check_declarations(void)
3151 if (warning.unused_parameter) {
3152 const scope_t *scope = ¤t_function->scope;
3154 const declaration_t *parameter = scope->declarations;
3155 for (; parameter != NULL; parameter = parameter->next) {
3156 if (! parameter->used) {
3157 print_in_function();
3158 warningf(parameter->source_position,
3159 "unused parameter '%Y'", parameter->symbol);
3163 if (warning.unused_variable) {
3167 static void parse_external_declaration(void)
3169 /* function-definitions and declarations both start with declaration
3171 declaration_specifiers_t specifiers;
3172 memset(&specifiers, 0, sizeof(specifiers));
3173 parse_declaration_specifiers(&specifiers);
3175 /* must be a declaration */
3176 if(token.type == ';') {
3177 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3181 /* declarator is common to both function-definitions and declarations */
3182 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3184 /* must be a declaration */
3185 if(token.type == ',' || token.type == '=' || token.type == ';') {
3186 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3190 /* must be a function definition */
3191 parse_kr_declaration_list(ndeclaration);
3193 if(token.type != '{') {
3194 parse_error_expected("while parsing function definition", '{', 0);
3199 type_t *type = ndeclaration->type;
3201 /* note that we don't skip typerefs: the standard doesn't allow them here
3202 * (so we can't use is_type_function here) */
3203 if(type->kind != TYPE_FUNCTION) {
3204 if (is_type_valid(type)) {
3205 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3206 type, ndeclaration->symbol);
3212 /* § 6.7.5.3 (14) a function definition with () means no
3213 * parameters (and not unspecified parameters) */
3214 if(type->function.unspecified_parameters) {
3215 type_t *duplicate = duplicate_type(type);
3216 duplicate->function.unspecified_parameters = false;
3218 type = typehash_insert(duplicate);
3219 if(type != duplicate) {
3220 obstack_free(type_obst, duplicate);
3222 ndeclaration->type = type;
3225 declaration_t *const declaration = record_function_definition(ndeclaration);
3226 if(ndeclaration != declaration) {
3227 declaration->scope = ndeclaration->scope;
3229 type = skip_typeref(declaration->type);
3231 /* push function parameters and switch scope */
3232 int top = environment_top();
3233 scope_t *last_scope = scope;
3234 set_scope(&declaration->scope);
3236 declaration_t *parameter = declaration->scope.declarations;
3237 for( ; parameter != NULL; parameter = parameter->next) {
3238 if(parameter->parent_scope == &ndeclaration->scope) {
3239 parameter->parent_scope = scope;
3241 assert(parameter->parent_scope == NULL
3242 || parameter->parent_scope == scope);
3243 parameter->parent_scope = scope;
3244 environment_push(parameter);
3247 if(declaration->init.statement != NULL) {
3248 parser_error_multiple_definition(declaration, token.source_position);
3250 goto end_of_parse_external_declaration;
3252 /* parse function body */
3253 int label_stack_top = label_top();
3254 declaration_t *old_current_function = current_function;
3255 current_function = declaration;
3257 declaration->init.statement = parse_compound_statement();
3260 check_declarations();
3262 assert(current_function == declaration);
3263 current_function = old_current_function;
3264 label_pop_to(label_stack_top);
3267 end_of_parse_external_declaration:
3268 assert(scope == &declaration->scope);
3269 set_scope(last_scope);
3270 environment_pop_to(top);
3273 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3274 source_position_t source_position)
3276 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3277 type->bitfield.base = base;
3278 type->bitfield.size = size;
3283 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3286 declaration_t *iter = compound_declaration->scope.declarations;
3287 for( ; iter != NULL; iter = iter->next) {
3288 if(iter->namespc != NAMESPACE_NORMAL)
3291 if(iter->symbol == NULL) {
3292 type_t *type = skip_typeref(iter->type);
3293 if(is_type_compound(type)) {
3294 declaration_t *result
3295 = find_compound_entry(type->compound.declaration, symbol);
3302 if(iter->symbol == symbol) {
3310 static void parse_compound_declarators(declaration_t *struct_declaration,
3311 const declaration_specifiers_t *specifiers)
3313 declaration_t *last_declaration = struct_declaration->scope.declarations;
3314 if(last_declaration != NULL) {
3315 while(last_declaration->next != NULL) {
3316 last_declaration = last_declaration->next;
3321 declaration_t *declaration;
3323 if(token.type == ':') {
3324 source_position_t source_position = HERE;
3327 type_t *base_type = specifiers->type;
3328 expression_t *size = parse_constant_expression();
3330 if(!is_type_integer(skip_typeref(base_type))) {
3331 errorf(HERE, "bitfield base type '%T' is not an integer type",
3335 type_t *type = make_bitfield_type(base_type, size, source_position);
3337 declaration = allocate_declaration_zero();
3338 declaration->namespc = NAMESPACE_NORMAL;
3339 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3340 declaration->storage_class = STORAGE_CLASS_NONE;
3341 declaration->source_position = source_position;
3342 declaration->modifiers = specifiers->decl_modifiers;
3343 declaration->type = type;
3345 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3347 type_t *orig_type = declaration->type;
3348 type_t *type = skip_typeref(orig_type);
3350 if(token.type == ':') {
3351 source_position_t source_position = HERE;
3353 expression_t *size = parse_constant_expression();
3355 if(!is_type_integer(type)) {
3356 errorf(HERE, "bitfield base type '%T' is not an "
3357 "integer type", orig_type);
3360 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3361 declaration->type = bitfield_type;
3363 /* TODO we ignore arrays for now... what is missing is a check
3364 * that they're at the end of the struct */
3365 if(is_type_incomplete(type) && !is_type_array(type)) {
3367 "compound member '%Y' has incomplete type '%T'",
3368 declaration->symbol, orig_type);
3369 } else if(is_type_function(type)) {
3370 errorf(HERE, "compound member '%Y' must not have function "
3371 "type '%T'", declaration->symbol, orig_type);
3376 /* make sure we don't define a symbol multiple times */
3377 symbol_t *symbol = declaration->symbol;
3378 if(symbol != NULL) {
3379 declaration_t *prev_decl
3380 = find_compound_entry(struct_declaration, symbol);
3382 if(prev_decl != NULL) {
3383 assert(prev_decl->symbol == symbol);
3384 errorf(declaration->source_position,
3385 "multiple declarations of symbol '%Y'", symbol);
3386 errorf(prev_decl->source_position,
3387 "previous declaration of '%Y' was here", symbol);
3391 /* append declaration */
3392 if(last_declaration != NULL) {
3393 last_declaration->next = declaration;
3395 struct_declaration->scope.declarations = declaration;
3397 last_declaration = declaration;
3399 if(token.type != ',')
3406 static void parse_compound_type_entries(declaration_t *compound_declaration)
3410 while(token.type != '}' && token.type != T_EOF) {
3411 declaration_specifiers_t specifiers;
3412 memset(&specifiers, 0, sizeof(specifiers));
3413 parse_declaration_specifiers(&specifiers);
3415 parse_compound_declarators(compound_declaration, &specifiers);
3417 if(token.type == T_EOF) {
3418 errorf(HERE, "EOF while parsing struct");
3423 static type_t *parse_typename(void)
3425 declaration_specifiers_t specifiers;
3426 memset(&specifiers, 0, sizeof(specifiers));
3427 parse_declaration_specifiers(&specifiers);
3428 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3429 /* TODO: improve error message, user does probably not know what a
3430 * storage class is...
3432 errorf(HERE, "typename may not have a storage class");
3435 type_t *result = parse_abstract_declarator(specifiers.type);
3443 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3444 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3445 expression_t *left);
3447 typedef struct expression_parser_function_t expression_parser_function_t;
3448 struct expression_parser_function_t {
3449 unsigned precedence;
3450 parse_expression_function parser;
3451 unsigned infix_precedence;
3452 parse_expression_infix_function infix_parser;
3455 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3458 * Creates a new invalid expression.
3460 static expression_t *create_invalid_expression(void)
3462 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3463 expression->base.source_position = token.source_position;
3468 * Prints an error message if an expression was expected but not read
3470 static expression_t *expected_expression_error(void)
3472 /* skip the error message if the error token was read */
3473 if (token.type != T_ERROR) {
3474 errorf(HERE, "expected expression, got token '%K'", &token);
3478 return create_invalid_expression();
3482 * Parse a string constant.
3484 static expression_t *parse_string_const(void)
3487 if (token.type == T_STRING_LITERAL) {
3488 string_t res = token.v.string;
3490 while (token.type == T_STRING_LITERAL) {
3491 res = concat_strings(&res, &token.v.string);
3494 if (token.type != T_WIDE_STRING_LITERAL) {
3495 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3496 /* note: that we use type_char_ptr here, which is already the
3497 * automatic converted type. revert_automatic_type_conversion
3498 * will construct the array type */
3499 cnst->base.type = type_char_ptr;
3500 cnst->string.value = res;
3504 wres = concat_string_wide_string(&res, &token.v.wide_string);
3506 wres = token.v.wide_string;
3511 switch (token.type) {
3512 case T_WIDE_STRING_LITERAL:
3513 wres = concat_wide_strings(&wres, &token.v.wide_string);
3516 case T_STRING_LITERAL:
3517 wres = concat_wide_string_string(&wres, &token.v.string);
3521 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3522 cnst->base.type = type_wchar_t_ptr;
3523 cnst->wide_string.value = wres;
3532 * Parse an integer constant.
3534 static expression_t *parse_int_const(void)
3536 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3537 cnst->base.source_position = HERE;
3538 cnst->base.type = token.datatype;
3539 cnst->conste.v.int_value = token.v.intvalue;
3547 * Parse a character constant.
3549 static expression_t *parse_character_constant(void)
3551 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
3553 cnst->base.source_position = HERE;
3554 cnst->base.type = token.datatype;
3555 cnst->conste.v.character = token.v.string;
3557 if (cnst->conste.v.character.size != 1) {
3558 if (warning.multichar && (c_mode & _GNUC)) {
3560 warningf(HERE, "multi-character character constant");
3562 errorf(HERE, "more than 1 characters in character constant");
3571 * Parse a wide character constant.
3573 static expression_t *parse_wide_character_constant(void)
3575 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
3577 cnst->base.source_position = HERE;
3578 cnst->base.type = token.datatype;
3579 cnst->conste.v.wide_character = token.v.wide_string;
3581 if (cnst->conste.v.wide_character.size != 1) {
3582 if (warning.multichar && (c_mode & _GNUC)) {
3584 warningf(HERE, "multi-character character constant");
3586 errorf(HERE, "more than 1 characters in character constant");
3595 * Parse a float constant.
3597 static expression_t *parse_float_const(void)
3599 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3600 cnst->base.type = token.datatype;
3601 cnst->conste.v.float_value = token.v.floatvalue;
3608 static declaration_t *create_implicit_function(symbol_t *symbol,
3609 const source_position_t source_position)
3611 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3612 ntype->function.return_type = type_int;
3613 ntype->function.unspecified_parameters = true;
3615 type_t *type = typehash_insert(ntype);
3620 declaration_t *const declaration = allocate_declaration_zero();
3621 declaration->storage_class = STORAGE_CLASS_EXTERN;
3622 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
3623 declaration->type = type;
3624 declaration->symbol = symbol;
3625 declaration->source_position = source_position;
3626 declaration->parent_scope = global_scope;
3628 scope_t *old_scope = scope;
3629 set_scope(global_scope);
3631 environment_push(declaration);
3632 /* prepends the declaration to the global declarations list */
3633 declaration->next = scope->declarations;
3634 scope->declarations = declaration;
3636 assert(scope == global_scope);
3637 set_scope(old_scope);
3643 * Creates a return_type (func)(argument_type) function type if not
3646 * @param return_type the return type
3647 * @param argument_type the argument type
3649 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3651 function_parameter_t *parameter
3652 = obstack_alloc(type_obst, sizeof(parameter[0]));
3653 memset(parameter, 0, sizeof(parameter[0]));
3654 parameter->type = argument_type;
3656 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3657 type->function.return_type = return_type;
3658 type->function.parameters = parameter;
3660 type_t *result = typehash_insert(type);
3661 if(result != type) {
3669 * Creates a function type for some function like builtins.
3671 * @param symbol the symbol describing the builtin
3673 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3675 switch(symbol->ID) {
3676 case T___builtin_alloca:
3677 return make_function_1_type(type_void_ptr, type_size_t);
3678 case T___builtin_nan:
3679 return make_function_1_type(type_double, type_char_ptr);
3680 case T___builtin_nanf:
3681 return make_function_1_type(type_float, type_char_ptr);
3682 case T___builtin_nand:
3683 return make_function_1_type(type_long_double, type_char_ptr);
3684 case T___builtin_va_end:
3685 return make_function_1_type(type_void, type_valist);
3687 panic("not implemented builtin symbol found");
3692 * Performs automatic type cast as described in § 6.3.2.1.
3694 * @param orig_type the original type
3696 static type_t *automatic_type_conversion(type_t *orig_type)
3698 type_t *type = skip_typeref(orig_type);
3699 if(is_type_array(type)) {
3700 array_type_t *array_type = &type->array;
3701 type_t *element_type = array_type->element_type;
3702 unsigned qualifiers = array_type->type.qualifiers;
3704 return make_pointer_type(element_type, qualifiers);
3707 if(is_type_function(type)) {
3708 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3715 * reverts the automatic casts of array to pointer types and function
3716 * to function-pointer types as defined § 6.3.2.1
3718 type_t *revert_automatic_type_conversion(const expression_t *expression)
3720 switch (expression->kind) {
3721 case EXPR_REFERENCE: return expression->reference.declaration->type;
3722 case EXPR_SELECT: return expression->select.compound_entry->type;
3724 case EXPR_UNARY_DEREFERENCE: {
3725 const expression_t *const value = expression->unary.value;
3726 type_t *const type = skip_typeref(value->base.type);
3727 assert(is_type_pointer(type));
3728 return type->pointer.points_to;
3731 case EXPR_BUILTIN_SYMBOL:
3732 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3734 case EXPR_ARRAY_ACCESS: {
3735 const expression_t *array_ref = expression->array_access.array_ref;
3736 type_t *type_left = skip_typeref(array_ref->base.type);
3737 if (!is_type_valid(type_left))
3739 assert(is_type_pointer(type_left));
3740 return type_left->pointer.points_to;
3743 case EXPR_STRING_LITERAL: {
3744 size_t size = expression->string.value.size;
3745 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
3748 case EXPR_WIDE_STRING_LITERAL: {
3749 size_t size = expression->wide_string.value.size;
3750 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
3753 case EXPR_COMPOUND_LITERAL:
3754 return expression->compound_literal.type;
3759 return expression->base.type;
3762 static expression_t *parse_reference(void)
3764 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3766 reference_expression_t *ref = &expression->reference;
3767 ref->symbol = token.v.symbol;
3769 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3771 source_position_t source_position = token.source_position;
3774 if(declaration == NULL) {
3775 if (! strict_mode && token.type == '(') {
3776 /* an implicitly defined function */
3777 if (warning.implicit_function_declaration) {
3778 warningf(HERE, "implicit declaration of function '%Y'",
3782 declaration = create_implicit_function(ref->symbol,
3785 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3786 return create_invalid_expression();
3790 type_t *type = declaration->type;
3792 /* we always do the auto-type conversions; the & and sizeof parser contains
3793 * code to revert this! */
3794 type = automatic_type_conversion(type);
3796 ref->declaration = declaration;
3797 ref->base.type = type;
3799 /* this declaration is used */
3800 declaration->used = true;
3805 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3809 /* TODO check if explicit cast is allowed and issue warnings/errors */
3812 static expression_t *parse_compound_literal(type_t *type)
3814 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
3816 parse_initializer_env_t env;
3818 env.must_be_constant = false;
3819 parse_initializer(&env);
3822 expression->compound_literal.type = type;
3823 expression->compound_literal.initializer = env.initializer;
3824 expression->base.type = automatic_type_conversion(type);
3829 static expression_t *parse_cast(void)
3831 source_position_t source_position = token.source_position;
3833 type_t *type = parse_typename();
3837 if(token.type == '{') {
3838 return parse_compound_literal(type);
3841 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3842 cast->base.source_position = source_position;
3844 expression_t *value = parse_sub_expression(20);
3846 check_cast_allowed(value, type);
3848 cast->base.type = type;
3849 cast->unary.value = value;
3854 static expression_t *parse_statement_expression(void)
3856 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3858 statement_t *statement = parse_compound_statement();
3859 expression->statement.statement = statement;
3860 expression->base.source_position = statement->base.source_position;
3862 /* find last statement and use its type */
3863 type_t *type = type_void;
3864 const statement_t *stmt = statement->compound.statements;
3866 while (stmt->base.next != NULL)
3867 stmt = stmt->base.next;
3869 if (stmt->kind == STATEMENT_EXPRESSION) {
3870 type = stmt->expression.expression->base.type;
3873 warningf(expression->base.source_position, "empty statement expression ({})");
3875 expression->base.type = type;
3882 static expression_t *parse_brace_expression(void)
3886 switch(token.type) {
3888 /* gcc extension: a statement expression */
3889 return parse_statement_expression();
3893 return parse_cast();
3895 if(is_typedef_symbol(token.v.symbol)) {
3896 return parse_cast();
3900 expression_t *result = parse_expression();
3906 static expression_t *parse_function_keyword(void)
3911 if (current_function == NULL) {
3912 errorf(HERE, "'__func__' used outside of a function");
3915 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3916 expression->base.type = type_char_ptr;
3921 static expression_t *parse_pretty_function_keyword(void)
3923 eat(T___PRETTY_FUNCTION__);
3926 if (current_function == NULL) {
3927 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3930 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3931 expression->base.type = type_char_ptr;
3936 static designator_t *parse_designator(void)
3938 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3939 result->source_position = HERE;
3941 if(token.type != T_IDENTIFIER) {
3942 parse_error_expected("while parsing member designator",
3947 result->symbol = token.v.symbol;
3950 designator_t *last_designator = result;
3952 if(token.type == '.') {
3954 if(token.type != T_IDENTIFIER) {
3955 parse_error_expected("while parsing member designator",
3960 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3961 designator->source_position = HERE;
3962 designator->symbol = token.v.symbol;
3965 last_designator->next = designator;
3966 last_designator = designator;
3969 if(token.type == '[') {
3971 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3972 designator->source_position = HERE;
3973 designator->array_index = parse_expression();
3974 if(designator->array_index == NULL) {
3980 last_designator->next = designator;
3981 last_designator = designator;
3990 static expression_t *parse_offsetof(void)
3992 eat(T___builtin_offsetof);
3994 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3995 expression->base.type = type_size_t;
3998 type_t *type = parse_typename();
4000 designator_t *designator = parse_designator();
4003 expression->offsetofe.type = type;
4004 expression->offsetofe.designator = designator;
4007 memset(&path, 0, sizeof(path));
4008 path.top_type = type;
4009 path.path = NEW_ARR_F(type_path_entry_t, 0);
4011 descend_into_subtype(&path);
4013 if(!walk_designator(&path, designator, true)) {
4014 return create_invalid_expression();
4017 DEL_ARR_F(path.path);
4022 static expression_t *parse_va_start(void)
4024 eat(T___builtin_va_start);
4026 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4029 expression->va_starte.ap = parse_assignment_expression();
4031 expression_t *const expr = parse_assignment_expression();
4032 if (expr->kind == EXPR_REFERENCE) {
4033 declaration_t *const decl = expr->reference.declaration;
4035 return create_invalid_expression();
4036 if (decl->parent_scope == ¤t_function->scope &&
4037 decl->next == NULL) {
4038 expression->va_starte.parameter = decl;
4043 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4045 return create_invalid_expression();
4048 static expression_t *parse_va_arg(void)
4050 eat(T___builtin_va_arg);
4052 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4055 expression->va_arge.ap = parse_assignment_expression();
4057 expression->base.type = parse_typename();
4063 static expression_t *parse_builtin_symbol(void)
4065 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4067 symbol_t *symbol = token.v.symbol;
4069 expression->builtin_symbol.symbol = symbol;
4072 type_t *type = get_builtin_symbol_type(symbol);
4073 type = automatic_type_conversion(type);
4075 expression->base.type = type;
4079 static expression_t *parse_builtin_constant(void)
4081 eat(T___builtin_constant_p);
4083 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4086 expression->builtin_constant.value = parse_assignment_expression();
4088 expression->base.type = type_int;
4093 static expression_t *parse_builtin_prefetch(void)
4095 eat(T___builtin_prefetch);
4097 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4100 expression->builtin_prefetch.adr = parse_assignment_expression();
4101 if (token.type == ',') {
4103 expression->builtin_prefetch.rw = parse_assignment_expression();
4105 if (token.type == ',') {
4107 expression->builtin_prefetch.locality = parse_assignment_expression();
4110 expression->base.type = type_void;
4115 static expression_t *parse_compare_builtin(void)
4117 expression_t *expression;
4119 switch(token.type) {
4120 case T___builtin_isgreater:
4121 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4123 case T___builtin_isgreaterequal:
4124 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4126 case T___builtin_isless:
4127 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4129 case T___builtin_islessequal:
4130 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4132 case T___builtin_islessgreater:
4133 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4135 case T___builtin_isunordered:
4136 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4139 panic("invalid compare builtin found");
4142 expression->base.source_position = HERE;
4146 expression->binary.left = parse_assignment_expression();
4148 expression->binary.right = parse_assignment_expression();
4151 type_t *const orig_type_left = expression->binary.left->base.type;
4152 type_t *const orig_type_right = expression->binary.right->base.type;
4154 type_t *const type_left = skip_typeref(orig_type_left);
4155 type_t *const type_right = skip_typeref(orig_type_right);
4156 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4157 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4158 type_error_incompatible("invalid operands in comparison",
4159 expression->base.source_position, orig_type_left, orig_type_right);
4162 semantic_comparison(&expression->binary);
4168 static expression_t *parse_builtin_expect(void)
4170 eat(T___builtin_expect);
4172 expression_t *expression
4173 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4176 expression->binary.left = parse_assignment_expression();
4178 expression->binary.right = parse_constant_expression();
4181 expression->base.type = expression->binary.left->base.type;
4186 static expression_t *parse_assume(void) {
4189 expression_t *expression
4190 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4193 expression->unary.value = parse_assignment_expression();
4196 expression->base.type = type_void;
4200 static expression_t *parse_primary_expression(void)
4202 switch (token.type) {
4203 case T_INTEGER: return parse_int_const();
4204 case T_CHARACTER_CONSTANT: return parse_character_constant();
4205 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4206 case T_FLOATINGPOINT: return parse_float_const();
4207 case T_STRING_LITERAL:
4208 case T_WIDE_STRING_LITERAL: return parse_string_const();
4209 case T_IDENTIFIER: return parse_reference();
4210 case T___FUNCTION__:
4211 case T___func__: return parse_function_keyword();
4212 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4213 case T___builtin_offsetof: return parse_offsetof();
4214 case T___builtin_va_start: return parse_va_start();
4215 case T___builtin_va_arg: return parse_va_arg();
4216 case T___builtin_expect: return parse_builtin_expect();
4217 case T___builtin_alloca:
4218 case T___builtin_nan:
4219 case T___builtin_nand:
4220 case T___builtin_nanf:
4221 case T___builtin_va_end: return parse_builtin_symbol();
4222 case T___builtin_isgreater:
4223 case T___builtin_isgreaterequal:
4224 case T___builtin_isless:
4225 case T___builtin_islessequal:
4226 case T___builtin_islessgreater:
4227 case T___builtin_isunordered: return parse_compare_builtin();
4228 case T___builtin_constant_p: return parse_builtin_constant();
4229 case T___builtin_prefetch: return parse_builtin_prefetch();
4230 case T_assume: return parse_assume();
4232 case '(': return parse_brace_expression();
4235 errorf(HERE, "unexpected token %K, expected an expression", &token);
4238 return create_invalid_expression();
4242 * Check if the expression has the character type and issue a warning then.
4244 static void check_for_char_index_type(const expression_t *expression) {
4245 type_t *const type = expression->base.type;
4246 const type_t *const base_type = skip_typeref(type);
4248 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4249 warning.char_subscripts) {
4250 warningf(expression->base.source_position,
4251 "array subscript has type '%T'", type);
4255 static expression_t *parse_array_expression(unsigned precedence,
4262 expression_t *inside = parse_expression();
4264 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4266 array_access_expression_t *array_access = &expression->array_access;
4268 type_t *const orig_type_left = left->base.type;
4269 type_t *const orig_type_inside = inside->base.type;
4271 type_t *const type_left = skip_typeref(orig_type_left);
4272 type_t *const type_inside = skip_typeref(orig_type_inside);
4274 type_t *return_type;
4275 if (is_type_pointer(type_left)) {
4276 return_type = type_left->pointer.points_to;
4277 array_access->array_ref = left;
4278 array_access->index = inside;
4279 check_for_char_index_type(inside);
4280 } else if (is_type_pointer(type_inside)) {
4281 return_type = type_inside->pointer.points_to;
4282 array_access->array_ref = inside;
4283 array_access->index = left;
4284 array_access->flipped = true;
4285 check_for_char_index_type(left);
4287 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4289 "array access on object with non-pointer types '%T', '%T'",
4290 orig_type_left, orig_type_inside);
4292 return_type = type_error_type;
4293 array_access->array_ref = create_invalid_expression();
4296 if(token.type != ']') {
4297 parse_error_expected("Problem while parsing array access", ']', 0);
4302 return_type = automatic_type_conversion(return_type);
4303 expression->base.type = return_type;
4308 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4310 expression_t *tp_expression = allocate_expression_zero(kind);
4311 tp_expression->base.type = type_size_t;
4313 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4315 tp_expression->typeprop.type = parse_typename();
4318 expression_t *expression = parse_sub_expression(precedence);
4319 expression->base.type = revert_automatic_type_conversion(expression);
4321 tp_expression->typeprop.type = expression->base.type;
4322 tp_expression->typeprop.tp_expression = expression;
4325 return tp_expression;
4328 static expression_t *parse_sizeof(unsigned precedence)
4331 return parse_typeprop(EXPR_SIZEOF, precedence);
4334 static expression_t *parse_alignof(unsigned precedence)
4337 return parse_typeprop(EXPR_SIZEOF, precedence);
4340 static expression_t *parse_select_expression(unsigned precedence,
4341 expression_t *compound)
4344 assert(token.type == '.' || token.type == T_MINUSGREATER);
4346 bool is_pointer = (token.type == T_MINUSGREATER);
4349 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4350 select->select.compound = compound;
4352 if(token.type != T_IDENTIFIER) {
4353 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4356 symbol_t *symbol = token.v.symbol;
4357 select->select.symbol = symbol;
4360 type_t *const orig_type = compound->base.type;
4361 type_t *const type = skip_typeref(orig_type);
4363 type_t *type_left = type;
4365 if (!is_type_pointer(type)) {
4366 if (is_type_valid(type)) {
4367 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4369 return create_invalid_expression();
4371 type_left = type->pointer.points_to;
4373 type_left = skip_typeref(type_left);
4375 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4376 type_left->kind != TYPE_COMPOUND_UNION) {
4377 if (is_type_valid(type_left)) {
4378 errorf(HERE, "request for member '%Y' in something not a struct or "
4379 "union, but '%T'", symbol, type_left);
4381 return create_invalid_expression();
4384 declaration_t *const declaration = type_left->compound.declaration;
4386 if(!declaration->init.is_defined) {
4387 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4389 return create_invalid_expression();
4392 declaration_t *iter = find_compound_entry(declaration, symbol);
4394 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4395 return create_invalid_expression();
4398 /* we always do the auto-type conversions; the & and sizeof parser contains
4399 * code to revert this! */
4400 type_t *expression_type = automatic_type_conversion(iter->type);
4402 select->select.compound_entry = iter;
4403 select->base.type = expression_type;
4405 if(expression_type->kind == TYPE_BITFIELD) {
4406 expression_t *extract
4407 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4408 extract->unary.value = select;
4409 extract->base.type = expression_type->bitfield.base;
4418 * Parse a call expression, ie. expression '( ... )'.
4420 * @param expression the function address
4422 static expression_t *parse_call_expression(unsigned precedence,
4423 expression_t *expression)
4426 expression_t *result = allocate_expression_zero(EXPR_CALL);
4428 call_expression_t *call = &result->call;
4429 call->function = expression;
4431 type_t *const orig_type = expression->base.type;
4432 type_t *const type = skip_typeref(orig_type);
4434 function_type_t *function_type = NULL;
4435 if (is_type_pointer(type)) {
4436 type_t *const to_type = skip_typeref(type->pointer.points_to);
4438 if (is_type_function(to_type)) {
4439 function_type = &to_type->function;
4440 call->base.type = function_type->return_type;
4444 if (function_type == NULL && is_type_valid(type)) {
4445 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4448 /* parse arguments */
4451 if(token.type != ')') {
4452 call_argument_t *last_argument = NULL;
4455 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4457 argument->expression = parse_assignment_expression();
4458 if(last_argument == NULL) {
4459 call->arguments = argument;
4461 last_argument->next = argument;
4463 last_argument = argument;
4465 if(token.type != ',')
4472 if(function_type != NULL) {
4473 function_parameter_t *parameter = function_type->parameters;
4474 call_argument_t *argument = call->arguments;
4475 for( ; parameter != NULL && argument != NULL;
4476 parameter = parameter->next, argument = argument->next) {
4477 type_t *expected_type = parameter->type;
4478 /* TODO report scope in error messages */
4479 expression_t *const arg_expr = argument->expression;
4480 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4481 if (res_type == NULL) {
4482 /* TODO improve error message */
4483 errorf(arg_expr->base.source_position,
4484 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4485 arg_expr, arg_expr->base.type, expected_type);
4487 argument->expression = create_implicit_cast(argument->expression, expected_type);
4490 /* too few parameters */
4491 if(parameter != NULL) {
4492 errorf(HERE, "too few arguments to function '%E'", expression);
4493 } else if(argument != NULL) {
4494 /* too many parameters */
4495 if(!function_type->variadic
4496 && !function_type->unspecified_parameters) {
4497 errorf(HERE, "too many arguments to function '%E'", expression);
4499 /* do default promotion */
4500 for( ; argument != NULL; argument = argument->next) {
4501 type_t *type = argument->expression->base.type;
4503 type = skip_typeref(type);
4504 if(is_type_integer(type)) {
4505 type = promote_integer(type);
4506 } else if(type == type_float) {
4510 argument->expression
4511 = create_implicit_cast(argument->expression, type);
4514 check_format(&result->call);
4517 check_format(&result->call);
4524 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4526 static bool same_compound_type(const type_t *type1, const type_t *type2)
4529 is_type_compound(type1) &&
4530 type1->kind == type2->kind &&
4531 type1->compound.declaration == type2->compound.declaration;
4535 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4537 * @param expression the conditional expression
4539 static expression_t *parse_conditional_expression(unsigned precedence,
4540 expression_t *expression)
4544 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4546 conditional_expression_t *conditional = &result->conditional;
4547 conditional->condition = expression;
4550 type_t *const condition_type_orig = expression->base.type;
4551 type_t *const condition_type = skip_typeref(condition_type_orig);
4552 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4553 type_error("expected a scalar type in conditional condition",
4554 expression->base.source_position, condition_type_orig);
4557 expression_t *true_expression = parse_expression();
4559 expression_t *false_expression = parse_sub_expression(precedence);
4561 type_t *const orig_true_type = true_expression->base.type;
4562 type_t *const orig_false_type = false_expression->base.type;
4563 type_t *const true_type = skip_typeref(orig_true_type);
4564 type_t *const false_type = skip_typeref(orig_false_type);
4567 type_t *result_type;
4568 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4569 result_type = semantic_arithmetic(true_type, false_type);
4571 true_expression = create_implicit_cast(true_expression, result_type);
4572 false_expression = create_implicit_cast(false_expression, result_type);
4574 conditional->true_expression = true_expression;
4575 conditional->false_expression = false_expression;
4576 conditional->base.type = result_type;
4577 } else if (same_compound_type(true_type, false_type) || (
4578 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4579 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4581 /* just take 1 of the 2 types */
4582 result_type = true_type;
4583 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4584 && pointers_compatible(true_type, false_type)) {
4586 result_type = true_type;
4587 } else if (is_type_pointer(true_type)
4588 && is_null_pointer_constant(false_expression)) {
4589 result_type = true_type;
4590 } else if (is_type_pointer(false_type)
4591 && is_null_pointer_constant(true_expression)) {
4592 result_type = false_type;
4594 /* TODO: one pointer to void*, other some pointer */
4596 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4597 type_error_incompatible("while parsing conditional",
4598 expression->base.source_position, true_type,
4601 result_type = type_error_type;
4604 conditional->true_expression
4605 = create_implicit_cast(true_expression, result_type);
4606 conditional->false_expression
4607 = create_implicit_cast(false_expression, result_type);
4608 conditional->base.type = result_type;
4613 * Parse an extension expression.
4615 static expression_t *parse_extension(unsigned precedence)
4617 eat(T___extension__);
4619 /* TODO enable extensions */
4620 expression_t *expression = parse_sub_expression(precedence);
4621 /* TODO disable extensions */
4625 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4627 eat(T___builtin_classify_type);
4629 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4630 result->base.type = type_int;
4633 expression_t *expression = parse_sub_expression(precedence);
4635 result->classify_type.type_expression = expression;
4640 static void semantic_incdec(unary_expression_t *expression)
4642 type_t *const orig_type = expression->value->base.type;
4643 type_t *const type = skip_typeref(orig_type);
4644 /* TODO !is_type_real && !is_type_pointer */
4645 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4646 if (is_type_valid(type)) {
4647 /* TODO: improve error message */
4648 errorf(HERE, "operation needs an arithmetic or pointer type");
4653 expression->base.type = orig_type;
4656 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4658 type_t *const orig_type = expression->value->base.type;
4659 type_t *const type = skip_typeref(orig_type);
4660 if(!is_type_arithmetic(type)) {
4661 if (is_type_valid(type)) {
4662 /* TODO: improve error message */
4663 errorf(HERE, "operation needs an arithmetic type");
4668 expression->base.type = orig_type;
4671 static void semantic_unexpr_scalar(unary_expression_t *expression)
4673 type_t *const orig_type = expression->value->base.type;
4674 type_t *const type = skip_typeref(orig_type);
4675 if (!is_type_scalar(type)) {
4676 if (is_type_valid(type)) {
4677 errorf(HERE, "operand of ! must be of scalar type");
4682 expression->base.type = orig_type;
4685 static void semantic_unexpr_integer(unary_expression_t *expression)
4687 type_t *const orig_type = expression->value->base.type;
4688 type_t *const type = skip_typeref(orig_type);
4689 if (!is_type_integer(type)) {
4690 if (is_type_valid(type)) {
4691 errorf(HERE, "operand of ~ must be of integer type");
4696 expression->base.type = orig_type;
4699 static void semantic_dereference(unary_expression_t *expression)
4701 type_t *const orig_type = expression->value->base.type;
4702 type_t *const type = skip_typeref(orig_type);
4703 if(!is_type_pointer(type)) {
4704 if (is_type_valid(type)) {
4705 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4710 type_t *result_type = type->pointer.points_to;
4711 result_type = automatic_type_conversion(result_type);
4712 expression->base.type = result_type;
4716 * Check the semantic of the address taken expression.
4718 static void semantic_take_addr(unary_expression_t *expression)
4720 expression_t *value = expression->value;
4721 value->base.type = revert_automatic_type_conversion(value);
4723 type_t *orig_type = value->base.type;
4724 if(!is_type_valid(orig_type))
4727 if(value->kind == EXPR_REFERENCE) {
4728 declaration_t *const declaration = value->reference.declaration;
4729 if(declaration != NULL) {
4730 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4731 errorf(expression->base.source_position,
4732 "address of register variable '%Y' requested",
4733 declaration->symbol);
4735 declaration->address_taken = 1;
4739 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4742 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4743 static expression_t *parse_##unexpression_type(unsigned precedence) \
4747 expression_t *unary_expression \
4748 = allocate_expression_zero(unexpression_type); \
4749 unary_expression->base.source_position = HERE; \
4750 unary_expression->unary.value = parse_sub_expression(precedence); \
4752 sfunc(&unary_expression->unary); \
4754 return unary_expression; \
4757 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4758 semantic_unexpr_arithmetic)
4759 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4760 semantic_unexpr_arithmetic)
4761 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4762 semantic_unexpr_scalar)
4763 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4764 semantic_dereference)
4765 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4767 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4768 semantic_unexpr_integer)
4769 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4771 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4774 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4776 static expression_t *parse_##unexpression_type(unsigned precedence, \
4777 expression_t *left) \
4779 (void) precedence; \
4782 expression_t *unary_expression \
4783 = allocate_expression_zero(unexpression_type); \
4784 unary_expression->unary.value = left; \
4786 sfunc(&unary_expression->unary); \
4788 return unary_expression; \
4791 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4792 EXPR_UNARY_POSTFIX_INCREMENT,
4794 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4795 EXPR_UNARY_POSTFIX_DECREMENT,
4798 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4800 /* TODO: handle complex + imaginary types */
4802 /* § 6.3.1.8 Usual arithmetic conversions */
4803 if(type_left == type_long_double || type_right == type_long_double) {
4804 return type_long_double;
4805 } else if(type_left == type_double || type_right == type_double) {
4807 } else if(type_left == type_float || type_right == type_float) {
4811 type_right = promote_integer(type_right);
4812 type_left = promote_integer(type_left);
4814 if(type_left == type_right)
4817 bool signed_left = is_type_signed(type_left);
4818 bool signed_right = is_type_signed(type_right);
4819 int rank_left = get_rank(type_left);
4820 int rank_right = get_rank(type_right);
4821 if(rank_left < rank_right) {
4822 if(signed_left == signed_right || !signed_right) {
4828 if(signed_left == signed_right || !signed_left) {
4837 * Check the semantic restrictions for a binary expression.
4839 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4841 expression_t *const left = expression->left;
4842 expression_t *const right = expression->right;
4843 type_t *const orig_type_left = left->base.type;
4844 type_t *const orig_type_right = right->base.type;
4845 type_t *const type_left = skip_typeref(orig_type_left);
4846 type_t *const type_right = skip_typeref(orig_type_right);
4848 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4849 /* TODO: improve error message */
4850 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4851 errorf(HERE, "operation needs arithmetic types");
4856 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4857 expression->left = create_implicit_cast(left, arithmetic_type);
4858 expression->right = create_implicit_cast(right, arithmetic_type);
4859 expression->base.type = arithmetic_type;
4862 static void semantic_shift_op(binary_expression_t *expression)
4864 expression_t *const left = expression->left;
4865 expression_t *const right = expression->right;
4866 type_t *const orig_type_left = left->base.type;
4867 type_t *const orig_type_right = right->base.type;
4868 type_t * type_left = skip_typeref(orig_type_left);
4869 type_t * type_right = skip_typeref(orig_type_right);
4871 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4872 /* TODO: improve error message */
4873 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4874 errorf(HERE, "operation needs integer types");
4879 type_left = promote_integer(type_left);
4880 type_right = promote_integer(type_right);
4882 expression->left = create_implicit_cast(left, type_left);
4883 expression->right = create_implicit_cast(right, type_right);
4884 expression->base.type = type_left;
4887 static void semantic_add(binary_expression_t *expression)
4889 expression_t *const left = expression->left;
4890 expression_t *const right = expression->right;
4891 type_t *const orig_type_left = left->base.type;
4892 type_t *const orig_type_right = right->base.type;
4893 type_t *const type_left = skip_typeref(orig_type_left);
4894 type_t *const type_right = skip_typeref(orig_type_right);
4897 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4898 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4899 expression->left = create_implicit_cast(left, arithmetic_type);
4900 expression->right = create_implicit_cast(right, arithmetic_type);
4901 expression->base.type = arithmetic_type;
4903 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4904 expression->base.type = type_left;
4905 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4906 expression->base.type = type_right;
4907 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4908 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4912 static void semantic_sub(binary_expression_t *expression)
4914 expression_t *const left = expression->left;
4915 expression_t *const right = expression->right;
4916 type_t *const orig_type_left = left->base.type;
4917 type_t *const orig_type_right = right->base.type;
4918 type_t *const type_left = skip_typeref(orig_type_left);
4919 type_t *const type_right = skip_typeref(orig_type_right);
4922 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4923 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4924 expression->left = create_implicit_cast(left, arithmetic_type);
4925 expression->right = create_implicit_cast(right, arithmetic_type);
4926 expression->base.type = arithmetic_type;
4928 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4929 expression->base.type = type_left;
4930 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4931 if(!pointers_compatible(type_left, type_right)) {
4933 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4934 orig_type_left, orig_type_right);
4936 expression->base.type = type_ptrdiff_t;
4938 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4939 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4940 orig_type_left, orig_type_right);
4945 * Check the semantics of comparison expressions.
4947 * @param expression The expression to check.
4949 static void semantic_comparison(binary_expression_t *expression)
4951 expression_t *left = expression->left;
4952 expression_t *right = expression->right;
4953 type_t *orig_type_left = left->base.type;
4954 type_t *orig_type_right = right->base.type;
4956 type_t *type_left = skip_typeref(orig_type_left);
4957 type_t *type_right = skip_typeref(orig_type_right);
4959 /* TODO non-arithmetic types */
4960 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4961 if (warning.sign_compare &&
4962 (expression->base.kind != EXPR_BINARY_EQUAL &&
4963 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4964 (is_type_signed(type_left) != is_type_signed(type_right))) {
4965 warningf(expression->base.source_position,
4966 "comparison between signed and unsigned");
4968 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4969 expression->left = create_implicit_cast(left, arithmetic_type);
4970 expression->right = create_implicit_cast(right, arithmetic_type);
4971 expression->base.type = arithmetic_type;
4972 if (warning.float_equal &&
4973 (expression->base.kind == EXPR_BINARY_EQUAL ||
4974 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4975 is_type_float(arithmetic_type)) {
4976 warningf(expression->base.source_position,
4977 "comparing floating point with == or != is unsafe");
4979 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4980 /* TODO check compatibility */
4981 } else if (is_type_pointer(type_left)) {
4982 expression->right = create_implicit_cast(right, type_left);
4983 } else if (is_type_pointer(type_right)) {
4984 expression->left = create_implicit_cast(left, type_right);
4985 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4986 type_error_incompatible("invalid operands in comparison",
4987 expression->base.source_position,
4988 type_left, type_right);
4990 expression->base.type = type_int;
4993 static void semantic_arithmetic_assign(binary_expression_t *expression)
4995 expression_t *left = expression->left;
4996 expression_t *right = expression->right;
4997 type_t *orig_type_left = left->base.type;
4998 type_t *orig_type_right = right->base.type;
5000 type_t *type_left = skip_typeref(orig_type_left);
5001 type_t *type_right = skip_typeref(orig_type_right);
5003 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5004 /* TODO: improve error message */
5005 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5006 errorf(HERE, "operation needs arithmetic types");
5011 /* combined instructions are tricky. We can't create an implicit cast on
5012 * the left side, because we need the uncasted form for the store.
5013 * The ast2firm pass has to know that left_type must be right_type
5014 * for the arithmetic operation and create a cast by itself */
5015 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5016 expression->right = create_implicit_cast(right, arithmetic_type);
5017 expression->base.type = type_left;
5020 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5022 expression_t *const left = expression->left;
5023 expression_t *const right = expression->right;
5024 type_t *const orig_type_left = left->base.type;
5025 type_t *const orig_type_right = right->base.type;
5026 type_t *const type_left = skip_typeref(orig_type_left);
5027 type_t *const type_right = skip_typeref(orig_type_right);
5029 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5030 /* combined instructions are tricky. We can't create an implicit cast on
5031 * the left side, because we need the uncasted form for the store.
5032 * The ast2firm pass has to know that left_type must be right_type
5033 * for the arithmetic operation and create a cast by itself */
5034 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5035 expression->right = create_implicit_cast(right, arithmetic_type);
5036 expression->base.type = type_left;
5037 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5038 expression->base.type = type_left;
5039 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5040 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5045 * Check the semantic restrictions of a logical expression.
5047 static void semantic_logical_op(binary_expression_t *expression)
5049 expression_t *const left = expression->left;
5050 expression_t *const right = expression->right;
5051 type_t *const orig_type_left = left->base.type;
5052 type_t *const orig_type_right = right->base.type;
5053 type_t *const type_left = skip_typeref(orig_type_left);
5054 type_t *const type_right = skip_typeref(orig_type_right);
5056 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5057 /* TODO: improve error message */
5058 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5059 errorf(HERE, "operation needs scalar types");
5064 expression->base.type = type_int;
5068 * Checks if a compound type has constant fields.
5070 static bool has_const_fields(const compound_type_t *type)
5072 const scope_t *scope = &type->declaration->scope;
5073 const declaration_t *declaration = scope->declarations;
5075 for (; declaration != NULL; declaration = declaration->next) {
5076 if (declaration->namespc != NAMESPACE_NORMAL)
5079 const type_t *decl_type = skip_typeref(declaration->type);
5080 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5088 * Check the semantic restrictions of a binary assign expression.
5090 static void semantic_binexpr_assign(binary_expression_t *expression)
5092 expression_t *left = expression->left;
5093 type_t *orig_type_left = left->base.type;
5095 type_t *type_left = revert_automatic_type_conversion(left);
5096 type_left = skip_typeref(orig_type_left);
5098 /* must be a modifiable lvalue */
5099 if (is_type_array(type_left)) {
5100 errorf(HERE, "cannot assign to arrays ('%E')", left);
5103 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5104 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5108 if(is_type_incomplete(type_left)) {
5110 "left-hand side of assignment '%E' has incomplete type '%T'",
5111 left, orig_type_left);
5114 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5115 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5116 left, orig_type_left);
5120 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5122 if (res_type == NULL) {
5123 errorf(expression->base.source_position,
5124 "cannot assign to '%T' from '%T'",
5125 orig_type_left, expression->right->base.type);
5127 expression->right = create_implicit_cast(expression->right, res_type);
5130 expression->base.type = orig_type_left;
5134 * Determine if the outermost operation (or parts thereof) of the given
5135 * expression has no effect in order to generate a warning about this fact.
5136 * Therefore in some cases this only examines some of the operands of the
5137 * expression (see comments in the function and examples below).
5139 * f() + 23; // warning, because + has no effect
5140 * x || f(); // no warning, because x controls execution of f()
5141 * x ? y : f(); // warning, because y has no effect
5142 * (void)x; // no warning to be able to suppress the warning
5143 * This function can NOT be used for an "expression has definitely no effect"-
5145 static bool expression_has_effect(const expression_t *const expr)
5147 switch (expr->kind) {
5148 case EXPR_UNKNOWN: break;
5149 case EXPR_INVALID: break;
5150 case EXPR_REFERENCE: return false;
5151 case EXPR_CONST: return false;
5152 case EXPR_CHARACTER_CONSTANT: return false;
5153 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5154 case EXPR_STRING_LITERAL: return false;
5155 case EXPR_WIDE_STRING_LITERAL: return false;
5158 const call_expression_t *const call = &expr->call;
5159 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5162 switch (call->function->builtin_symbol.symbol->ID) {
5163 case T___builtin_va_end: return true;
5164 default: return false;
5168 /* Generate the warning if either the left or right hand side of a
5169 * conditional expression has no effect */
5170 case EXPR_CONDITIONAL: {
5171 const conditional_expression_t *const cond = &expr->conditional;
5173 expression_has_effect(cond->true_expression) &&
5174 expression_has_effect(cond->false_expression);
5177 case EXPR_SELECT: return false;
5178 case EXPR_ARRAY_ACCESS: return false;
5179 case EXPR_SIZEOF: return false;
5180 case EXPR_CLASSIFY_TYPE: return false;
5181 case EXPR_ALIGNOF: return false;
5183 case EXPR_FUNCTION: return false;
5184 case EXPR_PRETTY_FUNCTION: return false;
5185 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5186 case EXPR_BUILTIN_CONSTANT_P: return false;
5187 case EXPR_BUILTIN_PREFETCH: return true;
5188 case EXPR_OFFSETOF: return false;
5189 case EXPR_VA_START: return true;
5190 case EXPR_VA_ARG: return true;
5191 case EXPR_STATEMENT: return true; // TODO
5192 case EXPR_COMPOUND_LITERAL: return false;
5194 case EXPR_UNARY_NEGATE: return false;
5195 case EXPR_UNARY_PLUS: return false;
5196 case EXPR_UNARY_BITWISE_NEGATE: return false;
5197 case EXPR_UNARY_NOT: return false;
5198 case EXPR_UNARY_DEREFERENCE: return false;
5199 case EXPR_UNARY_TAKE_ADDRESS: return false;
5200 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5201 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5202 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5203 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5205 /* Treat void casts as if they have an effect in order to being able to
5206 * suppress the warning */
5207 case EXPR_UNARY_CAST: {
5208 type_t *const type = skip_typeref(expr->base.type);
5209 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5212 case EXPR_UNARY_CAST_IMPLICIT: return true;
5213 case EXPR_UNARY_ASSUME: return true;
5214 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5216 case EXPR_BINARY_ADD: return false;
5217 case EXPR_BINARY_SUB: return false;
5218 case EXPR_BINARY_MUL: return false;
5219 case EXPR_BINARY_DIV: return false;
5220 case EXPR_BINARY_MOD: return false;
5221 case EXPR_BINARY_EQUAL: return false;
5222 case EXPR_BINARY_NOTEQUAL: return false;
5223 case EXPR_BINARY_LESS: return false;
5224 case EXPR_BINARY_LESSEQUAL: return false;
5225 case EXPR_BINARY_GREATER: return false;
5226 case EXPR_BINARY_GREATEREQUAL: return false;
5227 case EXPR_BINARY_BITWISE_AND: return false;
5228 case EXPR_BINARY_BITWISE_OR: return false;
5229 case EXPR_BINARY_BITWISE_XOR: return false;
5230 case EXPR_BINARY_SHIFTLEFT: return false;
5231 case EXPR_BINARY_SHIFTRIGHT: return false;
5232 case EXPR_BINARY_ASSIGN: return true;
5233 case EXPR_BINARY_MUL_ASSIGN: return true;
5234 case EXPR_BINARY_DIV_ASSIGN: return true;
5235 case EXPR_BINARY_MOD_ASSIGN: return true;
5236 case EXPR_BINARY_ADD_ASSIGN: return true;
5237 case EXPR_BINARY_SUB_ASSIGN: return true;
5238 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5239 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5240 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5241 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5242 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5244 /* Only examine the right hand side of && and ||, because the left hand
5245 * side already has the effect of controlling the execution of the right
5247 case EXPR_BINARY_LOGICAL_AND:
5248 case EXPR_BINARY_LOGICAL_OR:
5249 /* Only examine the right hand side of a comma expression, because the left
5250 * hand side has a separate warning */
5251 case EXPR_BINARY_COMMA:
5252 return expression_has_effect(expr->binary.right);
5254 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5255 case EXPR_BINARY_ISGREATER: return false;
5256 case EXPR_BINARY_ISGREATEREQUAL: return false;
5257 case EXPR_BINARY_ISLESS: return false;
5258 case EXPR_BINARY_ISLESSEQUAL: return false;
5259 case EXPR_BINARY_ISLESSGREATER: return false;
5260 case EXPR_BINARY_ISUNORDERED: return false;
5263 panic("unexpected statement");
5266 static void semantic_comma(binary_expression_t *expression)
5268 if (warning.unused_value) {
5269 const expression_t *const left = expression->left;
5270 if (!expression_has_effect(left)) {
5271 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5274 expression->base.type = expression->right->base.type;
5277 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5278 static expression_t *parse_##binexpression_type(unsigned precedence, \
5279 expression_t *left) \
5282 source_position_t pos = HERE; \
5284 expression_t *right = parse_sub_expression(precedence + lr); \
5286 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5287 binexpr->base.source_position = pos; \
5288 binexpr->binary.left = left; \
5289 binexpr->binary.right = right; \
5290 sfunc(&binexpr->binary); \
5295 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5296 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5297 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5298 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5299 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5300 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5301 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5302 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5303 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5305 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5306 semantic_comparison, 1)
5307 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5308 semantic_comparison, 1)
5309 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5310 semantic_comparison, 1)
5311 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5312 semantic_comparison, 1)
5314 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5315 semantic_binexpr_arithmetic, 1)
5316 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5317 semantic_binexpr_arithmetic, 1)
5318 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5319 semantic_binexpr_arithmetic, 1)
5320 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5321 semantic_logical_op, 1)
5322 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5323 semantic_logical_op, 1)
5324 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5325 semantic_shift_op, 1)
5326 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5327 semantic_shift_op, 1)
5328 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5329 semantic_arithmetic_addsubb_assign, 0)
5330 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5331 semantic_arithmetic_addsubb_assign, 0)
5332 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5333 semantic_arithmetic_assign, 0)
5334 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5335 semantic_arithmetic_assign, 0)
5336 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5337 semantic_arithmetic_assign, 0)
5338 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5339 semantic_arithmetic_assign, 0)
5340 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5341 semantic_arithmetic_assign, 0)
5342 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5343 semantic_arithmetic_assign, 0)
5344 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5345 semantic_arithmetic_assign, 0)
5346 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5347 semantic_arithmetic_assign, 0)
5349 static expression_t *parse_sub_expression(unsigned precedence)
5351 if(token.type < 0) {
5352 return expected_expression_error();
5355 expression_parser_function_t *parser
5356 = &expression_parsers[token.type];
5357 source_position_t source_position = token.source_position;
5360 if(parser->parser != NULL) {
5361 left = parser->parser(parser->precedence);
5363 left = parse_primary_expression();
5365 assert(left != NULL);
5366 left->base.source_position = source_position;
5369 if(token.type < 0) {
5370 return expected_expression_error();
5373 parser = &expression_parsers[token.type];
5374 if(parser->infix_parser == NULL)
5376 if(parser->infix_precedence < precedence)
5379 left = parser->infix_parser(parser->infix_precedence, left);
5381 assert(left != NULL);
5382 assert(left->kind != EXPR_UNKNOWN);
5383 left->base.source_position = source_position;
5390 * Parse an expression.
5392 static expression_t *parse_expression(void)
5394 return parse_sub_expression(1);
5398 * Register a parser for a prefix-like operator with given precedence.
5400 * @param parser the parser function
5401 * @param token_type the token type of the prefix token
5402 * @param precedence the precedence of the operator
5404 static void register_expression_parser(parse_expression_function parser,
5405 int token_type, unsigned precedence)
5407 expression_parser_function_t *entry = &expression_parsers[token_type];
5409 if(entry->parser != NULL) {
5410 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5411 panic("trying to register multiple expression parsers for a token");
5413 entry->parser = parser;
5414 entry->precedence = precedence;
5418 * Register a parser for an infix operator with given precedence.
5420 * @param parser the parser function
5421 * @param token_type the token type of the infix operator
5422 * @param precedence the precedence of the operator
5424 static void register_infix_parser(parse_expression_infix_function parser,
5425 int token_type, unsigned precedence)
5427 expression_parser_function_t *entry = &expression_parsers[token_type];
5429 if(entry->infix_parser != NULL) {
5430 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5431 panic("trying to register multiple infix expression parsers for a "
5434 entry->infix_parser = parser;
5435 entry->infix_precedence = precedence;
5439 * Initialize the expression parsers.
5441 static void init_expression_parsers(void)
5443 memset(&expression_parsers, 0, sizeof(expression_parsers));
5445 register_infix_parser(parse_array_expression, '[', 30);
5446 register_infix_parser(parse_call_expression, '(', 30);
5447 register_infix_parser(parse_select_expression, '.', 30);
5448 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5449 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5451 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5454 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5455 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5456 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5457 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5458 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5459 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5460 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5461 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5462 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5463 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5464 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5465 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5466 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5467 T_EXCLAMATIONMARKEQUAL, 13);
5468 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5469 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5470 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5471 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5472 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5473 register_infix_parser(parse_conditional_expression, '?', 7);
5474 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5475 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5476 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5477 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5478 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5479 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5480 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5481 T_LESSLESSEQUAL, 2);
5482 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5483 T_GREATERGREATEREQUAL, 2);
5484 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5486 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5488 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5491 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5493 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5494 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5495 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5496 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5497 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5498 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5499 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5501 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5503 register_expression_parser(parse_sizeof, T_sizeof, 25);
5504 register_expression_parser(parse_alignof, T___alignof__, 25);
5505 register_expression_parser(parse_extension, T___extension__, 25);
5506 register_expression_parser(parse_builtin_classify_type,
5507 T___builtin_classify_type, 25);
5511 * Parse a asm statement constraints specification.
5513 static asm_constraint_t *parse_asm_constraints(void)
5515 asm_constraint_t *result = NULL;
5516 asm_constraint_t *last = NULL;
5518 while(token.type == T_STRING_LITERAL || token.type == '[') {
5519 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5520 memset(constraint, 0, sizeof(constraint[0]));
5522 if(token.type == '[') {
5524 if(token.type != T_IDENTIFIER) {
5525 parse_error_expected("while parsing asm constraint",
5529 constraint->symbol = token.v.symbol;
5534 constraint->constraints = parse_string_literals();
5536 constraint->expression = parse_expression();
5540 last->next = constraint;
5542 result = constraint;
5546 if(token.type != ',')
5555 * Parse a asm statement clobber specification.
5557 static asm_clobber_t *parse_asm_clobbers(void)
5559 asm_clobber_t *result = NULL;
5560 asm_clobber_t *last = NULL;
5562 while(token.type == T_STRING_LITERAL) {
5563 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5564 clobber->clobber = parse_string_literals();
5567 last->next = clobber;
5573 if(token.type != ',')
5582 * Parse an asm statement.
5584 static statement_t *parse_asm_statement(void)
5588 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5589 statement->base.source_position = token.source_position;
5591 asm_statement_t *asm_statement = &statement->asms;
5593 if(token.type == T_volatile) {
5595 asm_statement->is_volatile = true;
5599 asm_statement->asm_text = parse_string_literals();
5601 if(token.type != ':')
5605 asm_statement->inputs = parse_asm_constraints();
5606 if(token.type != ':')
5610 asm_statement->outputs = parse_asm_constraints();
5611 if(token.type != ':')
5615 asm_statement->clobbers = parse_asm_clobbers();
5624 * Parse a case statement.
5626 static statement_t *parse_case_statement(void)
5630 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5632 statement->base.source_position = token.source_position;
5633 statement->case_label.expression = parse_expression();
5635 if (c_mode & _GNUC) {
5636 if (token.type == T_DOTDOTDOT) {
5638 statement->case_label.end_range = parse_expression();
5644 if (! is_constant_expression(statement->case_label.expression)) {
5645 errorf(statement->base.source_position,
5646 "case label does not reduce to an integer constant");
5648 /* TODO: check if the case label is already known */
5649 if (current_switch != NULL) {
5650 /* link all cases into the switch statement */
5651 if (current_switch->last_case == NULL) {
5652 current_switch->first_case =
5653 current_switch->last_case = &statement->case_label;
5655 current_switch->last_case->next = &statement->case_label;
5658 errorf(statement->base.source_position,
5659 "case label not within a switch statement");
5662 statement->case_label.statement = parse_statement();
5668 * Finds an existing default label of a switch statement.
5670 static case_label_statement_t *
5671 find_default_label(const switch_statement_t *statement)
5673 case_label_statement_t *label = statement->first_case;
5674 for ( ; label != NULL; label = label->next) {
5675 if (label->expression == NULL)
5682 * Parse a default statement.
5684 static statement_t *parse_default_statement(void)
5688 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5690 statement->base.source_position = token.source_position;
5693 if (current_switch != NULL) {
5694 const case_label_statement_t *def_label = find_default_label(current_switch);
5695 if (def_label != NULL) {
5696 errorf(HERE, "multiple default labels in one switch");
5697 errorf(def_label->base.source_position,
5698 "this is the first default label");
5700 /* link all cases into the switch statement */
5701 if (current_switch->last_case == NULL) {
5702 current_switch->first_case =
5703 current_switch->last_case = &statement->case_label;
5705 current_switch->last_case->next = &statement->case_label;
5709 errorf(statement->base.source_position,
5710 "'default' label not within a switch statement");
5712 statement->case_label.statement = parse_statement();
5718 * Return the declaration for a given label symbol or create a new one.
5720 static declaration_t *get_label(symbol_t *symbol)
5722 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5723 assert(current_function != NULL);
5724 /* if we found a label in the same function, then we already created the
5726 if(candidate != NULL
5727 && candidate->parent_scope == ¤t_function->scope) {
5731 /* otherwise we need to create a new one */
5732 declaration_t *const declaration = allocate_declaration_zero();
5733 declaration->namespc = NAMESPACE_LABEL;
5734 declaration->symbol = symbol;
5736 label_push(declaration);
5742 * Parse a label statement.
5744 static statement_t *parse_label_statement(void)
5746 assert(token.type == T_IDENTIFIER);
5747 symbol_t *symbol = token.v.symbol;
5750 declaration_t *label = get_label(symbol);
5752 /* if source position is already set then the label is defined twice,
5753 * otherwise it was just mentioned in a goto so far */
5754 if(label->source_position.input_name != NULL) {
5755 errorf(HERE, "duplicate label '%Y'", symbol);
5756 errorf(label->source_position, "previous definition of '%Y' was here",
5759 label->source_position = token.source_position;
5762 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5764 statement->base.source_position = token.source_position;
5765 statement->label.label = label;
5769 if(token.type == '}') {
5770 /* TODO only warn? */
5771 errorf(HERE, "label at end of compound statement");
5774 if (token.type == ';') {
5775 /* eat an empty statement here, to avoid the warning about an empty
5776 * after a label. label:; is commonly used to have a label before
5780 statement->label.statement = parse_statement();
5784 /* remember the labels's in a list for later checking */
5785 if (label_last == NULL) {
5786 label_first = &statement->label;
5788 label_last->next = &statement->label;
5790 label_last = &statement->label;
5796 * Parse an if statement.
5798 static statement_t *parse_if(void)
5802 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5803 statement->base.source_position = token.source_position;
5806 statement->ifs.condition = parse_expression();
5809 statement->ifs.true_statement = parse_statement();
5810 if(token.type == T_else) {
5812 statement->ifs.false_statement = parse_statement();
5819 * Parse a switch statement.
5821 static statement_t *parse_switch(void)
5825 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5826 statement->base.source_position = token.source_position;
5829 expression_t *const expr = parse_expression();
5830 type_t * type = skip_typeref(expr->base.type);
5831 if (is_type_integer(type)) {
5832 type = promote_integer(type);
5833 } else if (is_type_valid(type)) {
5834 errorf(expr->base.source_position,
5835 "switch quantity is not an integer, but '%T'", type);
5836 type = type_error_type;
5838 statement->switchs.expression = create_implicit_cast(expr, type);
5841 switch_statement_t *rem = current_switch;
5842 current_switch = &statement->switchs;
5843 statement->switchs.body = parse_statement();
5844 current_switch = rem;
5846 if (warning.switch_default
5847 && find_default_label(&statement->switchs) == NULL) {
5848 warningf(statement->base.source_position, "switch has no default case");
5854 static statement_t *parse_loop_body(statement_t *const loop)
5856 statement_t *const rem = current_loop;
5857 current_loop = loop;
5859 statement_t *const body = parse_statement();
5866 * Parse a while statement.
5868 static statement_t *parse_while(void)
5872 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5873 statement->base.source_position = token.source_position;
5876 statement->whiles.condition = parse_expression();
5879 statement->whiles.body = parse_loop_body(statement);
5885 * Parse a do statement.
5887 static statement_t *parse_do(void)
5891 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5893 statement->base.source_position = token.source_position;
5895 statement->do_while.body = parse_loop_body(statement);
5899 statement->do_while.condition = parse_expression();
5907 * Parse a for statement.
5909 static statement_t *parse_for(void)
5913 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5914 statement->base.source_position = token.source_position;
5918 int top = environment_top();
5919 scope_t *last_scope = scope;
5920 set_scope(&statement->fors.scope);
5922 if(token.type != ';') {
5923 if(is_declaration_specifier(&token, false)) {
5924 parse_declaration(record_declaration);
5926 expression_t *const init = parse_expression();
5927 statement->fors.initialisation = init;
5928 if (warning.unused_value && !expression_has_effect(init)) {
5929 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5937 if(token.type != ';') {
5938 statement->fors.condition = parse_expression();
5941 if(token.type != ')') {
5942 expression_t *const step = parse_expression();
5943 statement->fors.step = step;
5944 if (warning.unused_value && !expression_has_effect(step)) {
5945 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5949 statement->fors.body = parse_loop_body(statement);
5951 assert(scope == &statement->fors.scope);
5952 set_scope(last_scope);
5953 environment_pop_to(top);
5959 * Parse a goto statement.
5961 static statement_t *parse_goto(void)
5965 if(token.type != T_IDENTIFIER) {
5966 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5970 symbol_t *symbol = token.v.symbol;
5973 declaration_t *label = get_label(symbol);
5975 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5976 statement->base.source_position = token.source_position;
5978 statement->gotos.label = label;
5980 /* remember the goto's in a list for later checking */
5981 if (goto_last == NULL) {
5982 goto_first = &statement->gotos;
5984 goto_last->next = &statement->gotos;
5986 goto_last = &statement->gotos;
5994 * Parse a continue statement.
5996 static statement_t *parse_continue(void)
5998 statement_t *statement;
5999 if (current_loop == NULL) {
6000 errorf(HERE, "continue statement not within loop");
6003 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6005 statement->base.source_position = token.source_position;
6015 * Parse a break statement.
6017 static statement_t *parse_break(void)
6019 statement_t *statement;
6020 if (current_switch == NULL && current_loop == NULL) {
6021 errorf(HERE, "break statement not within loop or switch");
6024 statement = allocate_statement_zero(STATEMENT_BREAK);
6026 statement->base.source_position = token.source_position;
6036 * Check if a given declaration represents a local variable.
6038 static bool is_local_var_declaration(const declaration_t *declaration) {
6039 switch ((storage_class_tag_t) declaration->storage_class) {
6040 case STORAGE_CLASS_AUTO:
6041 case STORAGE_CLASS_REGISTER: {
6042 const type_t *type = skip_typeref(declaration->type);
6043 if(is_type_function(type)) {
6055 * Check if a given declaration represents a variable.
6057 static bool is_var_declaration(const declaration_t *declaration) {
6058 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6061 const type_t *type = skip_typeref(declaration->type);
6062 return !is_type_function(type);
6066 * Check if a given expression represents a local variable.
6068 static bool is_local_variable(const expression_t *expression)
6070 if (expression->base.kind != EXPR_REFERENCE) {
6073 const declaration_t *declaration = expression->reference.declaration;
6074 return is_local_var_declaration(declaration);
6078 * Check if a given expression represents a local variable and
6079 * return its declaration then, else return NULL.
6081 declaration_t *expr_is_variable(const expression_t *expression)
6083 if (expression->base.kind != EXPR_REFERENCE) {
6086 declaration_t *declaration = expression->reference.declaration;
6087 if (is_var_declaration(declaration))
6093 * Parse a return statement.
6095 static statement_t *parse_return(void)
6099 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6100 statement->base.source_position = token.source_position;
6102 expression_t *return_value = NULL;
6103 if(token.type != ';') {
6104 return_value = parse_expression();
6108 const type_t *const func_type = current_function->type;
6109 assert(is_type_function(func_type));
6110 type_t *const return_type = skip_typeref(func_type->function.return_type);
6112 if(return_value != NULL) {
6113 type_t *return_value_type = skip_typeref(return_value->base.type);
6115 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6116 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6117 warningf(statement->base.source_position,
6118 "'return' with a value, in function returning void");
6119 return_value = NULL;
6121 type_t *const res_type = semantic_assign(return_type,
6122 return_value, "'return'");
6123 if (res_type == NULL) {
6124 errorf(statement->base.source_position,
6125 "cannot return something of type '%T' in function returning '%T'",
6126 return_value->base.type, return_type);
6128 return_value = create_implicit_cast(return_value, res_type);
6131 /* check for returning address of a local var */
6132 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6133 const expression_t *expression = return_value->unary.value;
6134 if (is_local_variable(expression)) {
6135 warningf(statement->base.source_position,
6136 "function returns address of local variable");
6140 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6141 warningf(statement->base.source_position,
6142 "'return' without value, in function returning non-void");
6145 statement->returns.value = return_value;
6151 * Parse a declaration statement.
6153 static statement_t *parse_declaration_statement(void)
6155 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6157 statement->base.source_position = token.source_position;
6159 declaration_t *before = last_declaration;
6160 parse_declaration(record_declaration);
6162 if(before == NULL) {
6163 statement->declaration.declarations_begin = scope->declarations;
6165 statement->declaration.declarations_begin = before->next;
6167 statement->declaration.declarations_end = last_declaration;
6173 * Parse an expression statement, ie. expr ';'.
6175 static statement_t *parse_expression_statement(void)
6177 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6179 statement->base.source_position = token.source_position;
6180 expression_t *const expr = parse_expression();
6181 statement->expression.expression = expr;
6183 if (warning.unused_value && !expression_has_effect(expr)) {
6184 warningf(expr->base.source_position, "statement has no effect");
6193 * Parse a statement.
6195 static statement_t *parse_statement(void)
6197 statement_t *statement = NULL;
6199 /* declaration or statement */
6200 switch(token.type) {
6202 statement = parse_asm_statement();
6206 statement = parse_case_statement();
6210 statement = parse_default_statement();
6214 statement = parse_compound_statement();
6218 statement = parse_if();
6222 statement = parse_switch();
6226 statement = parse_while();
6230 statement = parse_do();
6234 statement = parse_for();
6238 statement = parse_goto();
6242 statement = parse_continue();
6246 statement = parse_break();
6250 statement = parse_return();
6254 if (warning.empty_statement) {
6255 warningf(HERE, "statement is empty");
6262 if(look_ahead(1)->type == ':') {
6263 statement = parse_label_statement();
6267 if(is_typedef_symbol(token.v.symbol)) {
6268 statement = parse_declaration_statement();
6272 statement = parse_expression_statement();
6275 case T___extension__:
6276 /* this can be a prefix to a declaration or an expression statement */
6277 /* we simply eat it now and parse the rest with tail recursion */
6280 } while(token.type == T___extension__);
6281 statement = parse_statement();
6285 statement = parse_declaration_statement();
6289 statement = parse_expression_statement();
6293 assert(statement == NULL
6294 || statement->base.source_position.input_name != NULL);
6300 * Parse a compound statement.
6302 static statement_t *parse_compound_statement(void)
6304 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6306 statement->base.source_position = token.source_position;
6310 int top = environment_top();
6311 scope_t *last_scope = scope;
6312 set_scope(&statement->compound.scope);
6314 statement_t *last_statement = NULL;
6316 while(token.type != '}' && token.type != T_EOF) {
6317 statement_t *sub_statement = parse_statement();
6318 if(sub_statement == NULL)
6321 if(last_statement != NULL) {
6322 last_statement->base.next = sub_statement;
6324 statement->compound.statements = sub_statement;
6327 while(sub_statement->base.next != NULL)
6328 sub_statement = sub_statement->base.next;
6330 last_statement = sub_statement;
6333 if(token.type == '}') {
6336 errorf(statement->base.source_position,
6337 "end of file while looking for closing '}'");
6340 assert(scope == &statement->compound.scope);
6341 set_scope(last_scope);
6342 environment_pop_to(top);
6348 * Initialize builtin types.
6350 static void initialize_builtin_types(void)
6352 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6353 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6354 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6355 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6356 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6357 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6358 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6359 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6361 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6362 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6363 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6364 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6368 * Check for unused global static functions and variables
6370 static void check_unused_globals(void)
6372 if (!warning.unused_function && !warning.unused_variable)
6375 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6376 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6379 type_t *const type = decl->type;
6381 if (is_type_function(skip_typeref(type))) {
6382 if (!warning.unused_function || decl->is_inline)
6385 s = (decl->init.statement != NULL ? "defined" : "declared");
6387 if (!warning.unused_variable)
6393 warningf(decl->source_position, "'%#T' %s but not used",
6394 type, decl->symbol, s);
6399 * Parse a translation unit.
6401 static translation_unit_t *parse_translation_unit(void)
6403 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6405 assert(global_scope == NULL);
6406 global_scope = &unit->scope;
6408 assert(scope == NULL);
6409 set_scope(&unit->scope);
6411 initialize_builtin_types();
6413 while(token.type != T_EOF) {
6414 if (token.type == ';') {
6415 /* TODO error in strict mode */
6416 warningf(HERE, "stray ';' outside of function");
6419 parse_external_declaration();
6423 assert(scope == &unit->scope);
6425 last_declaration = NULL;
6427 assert(global_scope == &unit->scope);
6428 check_unused_globals();
6429 global_scope = NULL;
6437 * @return the translation unit or NULL if errors occurred.
6439 translation_unit_t *parse(void)
6441 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6442 label_stack = NEW_ARR_F(stack_entry_t, 0);
6443 diagnostic_count = 0;
6447 type_set_output(stderr);
6448 ast_set_output(stderr);
6450 lookahead_bufpos = 0;
6451 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6454 translation_unit_t *unit = parse_translation_unit();
6456 DEL_ARR_F(environment_stack);
6457 DEL_ARR_F(label_stack);
6466 * Initialize the parser.
6468 void init_parser(void)
6470 init_expression_parsers();
6471 obstack_init(&temp_obst);
6473 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6474 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6478 * Terminate the parser.
6480 void exit_parser(void)
6482 obstack_free(&temp_obst, NULL);