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 == '(') {
489 if(token.type == '{') {
497 #define expect(expected) \
499 if(UNLIKELY(token.type != (expected))) { \
500 parse_error_expected(NULL, (expected), 0); \
507 #define expect_block(expected) \
509 if(UNLIKELY(token.type != (expected))) { \
510 parse_error_expected(NULL, (expected), 0); \
517 #define expect_void(expected) \
519 if(UNLIKELY(token.type != (expected))) { \
520 parse_error_expected(NULL, (expected), 0); \
527 static void set_scope(scope_t *new_scope)
530 scope->last_declaration = last_declaration;
534 last_declaration = new_scope->last_declaration;
538 * Search a symbol in a given namespace and returns its declaration or
539 * NULL if this symbol was not found.
541 static declaration_t *get_declaration(const symbol_t *const symbol,
542 const namespace_t namespc)
544 declaration_t *declaration = symbol->declaration;
545 for( ; declaration != NULL; declaration = declaration->symbol_next) {
546 if(declaration->namespc == namespc)
554 * pushs an environment_entry on the environment stack and links the
555 * corresponding symbol to the new entry
557 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
559 symbol_t *symbol = declaration->symbol;
560 namespace_t namespc = (namespace_t) declaration->namespc;
562 /* replace/add declaration into declaration list of the symbol */
563 declaration_t *iter = symbol->declaration;
565 symbol->declaration = declaration;
567 declaration_t *iter_last = NULL;
568 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
569 /* replace an entry? */
570 if(iter->namespc == namespc) {
571 if(iter_last == NULL) {
572 symbol->declaration = declaration;
574 iter_last->symbol_next = declaration;
576 declaration->symbol_next = iter->symbol_next;
581 assert(iter_last->symbol_next == NULL);
582 iter_last->symbol_next = declaration;
586 /* remember old declaration */
588 entry.symbol = symbol;
589 entry.old_declaration = iter;
590 entry.namespc = (unsigned short) namespc;
591 ARR_APP1(stack_entry_t, *stack_ptr, entry);
594 static void environment_push(declaration_t *declaration)
596 assert(declaration->source_position.input_name != NULL);
597 assert(declaration->parent_scope != NULL);
598 stack_push(&environment_stack, declaration);
601 static void label_push(declaration_t *declaration)
603 declaration->parent_scope = ¤t_function->scope;
604 stack_push(&label_stack, declaration);
608 * pops symbols from the environment stack until @p new_top is the top element
610 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
612 stack_entry_t *stack = *stack_ptr;
613 size_t top = ARR_LEN(stack);
616 assert(new_top <= top);
620 for(i = top; i > new_top; --i) {
621 stack_entry_t *entry = &stack[i - 1];
623 declaration_t *old_declaration = entry->old_declaration;
624 symbol_t *symbol = entry->symbol;
625 namespace_t namespc = (namespace_t)entry->namespc;
627 /* replace/remove declaration */
628 declaration_t *declaration = symbol->declaration;
629 assert(declaration != NULL);
630 if(declaration->namespc == namespc) {
631 if(old_declaration == NULL) {
632 symbol->declaration = declaration->symbol_next;
634 symbol->declaration = old_declaration;
637 declaration_t *iter_last = declaration;
638 declaration_t *iter = declaration->symbol_next;
639 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
640 /* replace an entry? */
641 if(iter->namespc == namespc) {
642 assert(iter_last != NULL);
643 iter_last->symbol_next = old_declaration;
644 if(old_declaration != NULL) {
645 old_declaration->symbol_next = iter->symbol_next;
650 assert(iter != NULL);
654 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
657 static void environment_pop_to(size_t new_top)
659 stack_pop_to(&environment_stack, new_top);
662 static void label_pop_to(size_t new_top)
664 stack_pop_to(&label_stack, new_top);
668 static int get_rank(const type_t *type)
670 assert(!is_typeref(type));
671 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
672 * and esp. footnote 108). However we can't fold constants (yet), so we
673 * can't decide whether unsigned int is possible, while int always works.
674 * (unsigned int would be preferable when possible... for stuff like
675 * struct { enum { ... } bla : 4; } ) */
676 if(type->kind == TYPE_ENUM)
677 return ATOMIC_TYPE_INT;
679 assert(type->kind == TYPE_ATOMIC);
680 return type->atomic.akind;
683 static type_t *promote_integer(type_t *type)
685 if(type->kind == TYPE_BITFIELD)
686 type = type->bitfield.base;
688 if(get_rank(type) < ATOMIC_TYPE_INT)
695 * Create a cast expression.
697 * @param expression the expression to cast
698 * @param dest_type the destination type
700 static expression_t *create_cast_expression(expression_t *expression,
703 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
705 cast->unary.value = expression;
706 cast->base.type = dest_type;
712 * Check if a given expression represents the 0 pointer constant.
714 static bool is_null_pointer_constant(const expression_t *expression)
716 /* skip void* cast */
717 if(expression->kind == EXPR_UNARY_CAST
718 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
719 expression = expression->unary.value;
722 /* TODO: not correct yet, should be any constant integer expression
723 * which evaluates to 0 */
724 if (expression->kind != EXPR_CONST)
727 type_t *const type = skip_typeref(expression->base.type);
728 if (!is_type_integer(type))
731 return expression->conste.v.int_value == 0;
735 * Create an implicit cast expression.
737 * @param expression the expression to cast
738 * @param dest_type the destination type
740 static expression_t *create_implicit_cast(expression_t *expression,
743 type_t *const source_type = expression->base.type;
745 if (source_type == dest_type)
748 return create_cast_expression(expression, dest_type);
751 /** Implements the rules from § 6.5.16.1 */
752 static type_t *semantic_assign(type_t *orig_type_left,
753 const expression_t *const right,
756 type_t *const orig_type_right = right->base.type;
757 type_t *const type_left = skip_typeref(orig_type_left);
758 type_t *const type_right = skip_typeref(orig_type_right);
760 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
761 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
762 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
763 && is_type_pointer(type_right))) {
764 return orig_type_left;
767 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
768 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
769 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
771 /* the left type has all qualifiers from the right type */
772 unsigned missing_qualifiers
773 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
774 if(missing_qualifiers != 0) {
775 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
776 return orig_type_left;
779 points_to_left = get_unqualified_type(points_to_left);
780 points_to_right = get_unqualified_type(points_to_right);
782 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
783 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
784 return orig_type_left;
787 if (!types_compatible(points_to_left, points_to_right)) {
788 warningf(right->base.source_position,
789 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
790 orig_type_left, context, right, orig_type_right);
793 return orig_type_left;
796 if ((is_type_compound(type_left) && is_type_compound(type_right))
797 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
798 type_t *const unqual_type_left = get_unqualified_type(type_left);
799 type_t *const unqual_type_right = get_unqualified_type(type_right);
800 if (types_compatible(unqual_type_left, unqual_type_right)) {
801 return orig_type_left;
805 if (!is_type_valid(type_left))
808 if (!is_type_valid(type_right))
809 return orig_type_right;
814 static expression_t *parse_constant_expression(void)
816 /* start parsing at precedence 7 (conditional expression) */
817 expression_t *result = parse_sub_expression(7);
819 if(!is_constant_expression(result)) {
820 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
826 static expression_t *parse_assignment_expression(void)
828 /* start parsing at precedence 2 (assignment expression) */
829 return parse_sub_expression(2);
832 static type_t *make_global_typedef(const char *name, type_t *type)
834 symbol_t *const symbol = symbol_table_insert(name);
836 declaration_t *const declaration = allocate_declaration_zero();
837 declaration->namespc = NAMESPACE_NORMAL;
838 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
839 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
840 declaration->type = type;
841 declaration->symbol = symbol;
842 declaration->source_position = builtin_source_position;
844 record_declaration(declaration);
846 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
847 typedef_type->typedeft.declaration = declaration;
852 static string_t parse_string_literals(void)
854 assert(token.type == T_STRING_LITERAL);
855 string_t result = token.v.string;
859 while (token.type == T_STRING_LITERAL) {
860 result = concat_strings(&result, &token.v.string);
867 static void parse_attributes(void)
871 case T___attribute__: {
879 errorf(HERE, "EOF while parsing attribute");
898 if(token.type != T_STRING_LITERAL) {
899 parse_error_expected("while parsing assembler attribute",
904 parse_string_literals();
909 goto attributes_finished;
917 static designator_t *parse_designation(void)
919 designator_t *result = NULL;
920 designator_t *last = NULL;
923 designator_t *designator;
926 designator = allocate_ast_zero(sizeof(designator[0]));
927 designator->source_position = token.source_position;
929 designator->array_index = parse_constant_expression();
933 designator = allocate_ast_zero(sizeof(designator[0]));
934 designator->source_position = token.source_position;
936 if(token.type != T_IDENTIFIER) {
937 parse_error_expected("while parsing designator",
941 designator->symbol = token.v.symbol;
949 assert(designator != NULL);
951 last->next = designator;
959 static initializer_t *initializer_from_string(array_type_t *type,
960 const string_t *const string)
962 /* TODO: check len vs. size of array type */
965 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
966 initializer->string.string = *string;
971 static initializer_t *initializer_from_wide_string(array_type_t *const type,
972 wide_string_t *const string)
974 /* TODO: check len vs. size of array type */
977 initializer_t *const initializer =
978 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
979 initializer->wide_string.string = *string;
984 static initializer_t *initializer_from_expression(type_t *orig_type,
985 expression_t *expression)
987 /* TODO check that expression is a constant expression */
989 /* § 6.7.8.14/15 char array may be initialized by string literals */
990 type_t *type = skip_typeref(orig_type);
991 type_t *expr_type_orig = expression->base.type;
992 type_t *expr_type = skip_typeref(expr_type_orig);
993 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
994 array_type_t *const array_type = &type->array;
995 type_t *const element_type = skip_typeref(array_type->element_type);
997 if (element_type->kind == TYPE_ATOMIC) {
998 atomic_type_kind_t akind = element_type->atomic.akind;
999 switch (expression->kind) {
1000 case EXPR_STRING_LITERAL:
1001 if (akind == ATOMIC_TYPE_CHAR
1002 || akind == ATOMIC_TYPE_SCHAR
1003 || akind == ATOMIC_TYPE_UCHAR) {
1004 return initializer_from_string(array_type,
1005 &expression->string.value);
1008 case EXPR_WIDE_STRING_LITERAL: {
1009 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1010 if (get_unqualified_type(element_type) == bare_wchar_type) {
1011 return initializer_from_wide_string(array_type,
1012 &expression->wide_string.value);
1022 type_t *const res_type = semantic_assign(type, expression, "initializer");
1023 if (res_type == NULL)
1026 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1027 result->value.value = create_implicit_cast(expression, res_type);
1032 static bool is_initializer_constant(const expression_t *expression)
1034 return is_constant_expression(expression)
1035 || is_address_constant(expression);
1038 static initializer_t *parse_scalar_initializer(type_t *type,
1039 bool must_be_constant)
1041 /* there might be extra {} hierarchies */
1043 while(token.type == '{') {
1046 warningf(HERE, "extra curly braces around scalar initializer");
1051 expression_t *expression = parse_assignment_expression();
1052 if(must_be_constant && !is_initializer_constant(expression)) {
1053 errorf(expression->base.source_position,
1054 "Initialisation expression '%E' is not constant\n",
1058 initializer_t *initializer = initializer_from_expression(type, expression);
1060 if(initializer == NULL) {
1061 errorf(expression->base.source_position,
1062 "expression '%E' doesn't match expected type '%T'",
1068 bool additional_warning_displayed = false;
1070 if(token.type == ',') {
1073 if(token.type != '}') {
1074 if(!additional_warning_displayed) {
1075 warningf(HERE, "additional elements in scalar initializer");
1076 additional_warning_displayed = true;
1086 typedef struct type_path_entry_t type_path_entry_t;
1087 struct type_path_entry_t {
1091 declaration_t *compound_entry;
1095 typedef struct type_path_t type_path_t;
1096 struct type_path_t {
1097 type_path_entry_t *path;
1098 type_t *top_type; /**< type of the element the path points */
1099 size_t max_index; /**< largest index in outermost array */
1103 static __attribute__((unused)) void debug_print_type_path(
1104 const type_path_t *path)
1106 size_t len = ARR_LEN(path->path);
1109 fprintf(stderr, "invalid path");
1113 for(size_t i = 0; i < len; ++i) {
1114 const type_path_entry_t *entry = & path->path[i];
1116 type_t *type = skip_typeref(entry->type);
1117 if(is_type_compound(type)) {
1118 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1119 } else if(is_type_array(type)) {
1120 fprintf(stderr, "[%u]", entry->v.index);
1122 fprintf(stderr, "-INVALID-");
1125 fprintf(stderr, " (");
1126 print_type(path->top_type);
1127 fprintf(stderr, ")");
1130 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1132 size_t len = ARR_LEN(path->path);
1134 return & path->path[len-1];
1137 static type_path_entry_t *append_to_type_path(type_path_t *path)
1139 size_t len = ARR_LEN(path->path);
1140 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1142 type_path_entry_t *result = & path->path[len];
1143 memset(result, 0, sizeof(result[0]));
1147 static void descend_into_subtype(type_path_t *path)
1149 type_t *orig_top_type = path->top_type;
1150 type_t *top_type = skip_typeref(orig_top_type);
1152 assert(is_type_compound(top_type) || is_type_array(top_type));
1154 type_path_entry_t *top = append_to_type_path(path);
1155 top->type = top_type;
1157 if(is_type_compound(top_type)) {
1158 declaration_t *declaration = top_type->compound.declaration;
1159 declaration_t *entry = declaration->scope.declarations;
1161 top->v.compound_entry = entry;
1162 path->top_type = entry->type;
1164 assert(is_type_array(top_type));
1167 path->top_type = top_type->array.element_type;
1171 static void ascend_from_subtype(type_path_t *path)
1173 type_path_entry_t *top = get_type_path_top(path);
1175 path->top_type = top->type;
1177 size_t len = ARR_LEN(path->path);
1178 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1181 static void ascend_to(type_path_t *path, size_t top_path_level)
1183 size_t len = ARR_LEN(path->path);
1184 assert(len >= top_path_level);
1186 while(len > top_path_level) {
1187 ascend_from_subtype(path);
1188 len = ARR_LEN(path->path);
1192 static bool walk_designator(type_path_t *path, const designator_t *designator,
1193 bool used_in_offsetof)
1195 for( ; designator != NULL; designator = designator->next) {
1196 type_path_entry_t *top = get_type_path_top(path);
1197 type_t *orig_type = top->type;
1199 type_t *type = skip_typeref(orig_type);
1201 if(designator->symbol != NULL) {
1202 symbol_t *symbol = designator->symbol;
1203 if(!is_type_compound(type)) {
1204 if(is_type_valid(type)) {
1205 errorf(designator->source_position,
1206 "'.%Y' designator used for non-compound type '%T'",
1212 declaration_t *declaration = type->compound.declaration;
1213 declaration_t *iter = declaration->scope.declarations;
1214 for( ; iter != NULL; iter = iter->next) {
1215 if(iter->symbol == symbol) {
1220 errorf(designator->source_position,
1221 "'%T' has no member named '%Y'", orig_type, symbol);
1224 if(used_in_offsetof) {
1225 type_t *real_type = skip_typeref(iter->type);
1226 if(real_type->kind == TYPE_BITFIELD) {
1227 errorf(designator->source_position,
1228 "offsetof designator '%Y' may not specify bitfield",
1234 top->type = orig_type;
1235 top->v.compound_entry = iter;
1236 orig_type = iter->type;
1238 expression_t *array_index = designator->array_index;
1239 assert(designator->array_index != NULL);
1241 if(!is_type_array(type)) {
1242 if(is_type_valid(type)) {
1243 errorf(designator->source_position,
1244 "[%E] designator used for non-array type '%T'",
1245 array_index, orig_type);
1249 if(!is_type_valid(array_index->base.type)) {
1253 long index = fold_constant(array_index);
1254 if(!used_in_offsetof) {
1256 errorf(designator->source_position,
1257 "array index [%E] must be positive", array_index);
1260 if(type->array.size_constant == true) {
1261 long array_size = type->array.size;
1262 if(index >= array_size) {
1263 errorf(designator->source_position,
1264 "designator [%E] (%d) exceeds array size %d",
1265 array_index, index, array_size);
1271 top->type = orig_type;
1272 top->v.index = (size_t) index;
1273 orig_type = type->array.element_type;
1275 path->top_type = orig_type;
1277 if(designator->next != NULL) {
1278 descend_into_subtype(path);
1282 path->invalid = false;
1289 static void advance_current_object(type_path_t *path, size_t top_path_level)
1294 type_path_entry_t *top = get_type_path_top(path);
1296 type_t *type = skip_typeref(top->type);
1297 if(is_type_union(type)) {
1298 /* in unions only the first element is initialized */
1299 top->v.compound_entry = NULL;
1300 } else if(is_type_struct(type)) {
1301 declaration_t *entry = top->v.compound_entry;
1303 entry = entry->next;
1304 top->v.compound_entry = entry;
1306 path->top_type = entry->type;
1310 assert(is_type_array(type));
1314 if(!type->array.size_constant || top->v.index < type->array.size) {
1319 /* we're past the last member of the current sub-aggregate, try if we
1320 * can ascend in the type hierarchy and continue with another subobject */
1321 size_t len = ARR_LEN(path->path);
1323 if(len > top_path_level) {
1324 ascend_from_subtype(path);
1325 advance_current_object(path, top_path_level);
1327 path->invalid = true;
1331 static void skip_initializers(void)
1333 if(token.type == '{')
1336 while(token.type != '}') {
1337 if(token.type == T_EOF)
1339 if(token.type == '{') {
1347 static initializer_t *parse_sub_initializer(type_path_t *path,
1348 type_t *outer_type, size_t top_path_level, bool must_be_constant)
1350 type_t *orig_type = path->top_type;
1351 type_t *type = skip_typeref(orig_type);
1353 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1354 * initializers in this case. */
1355 if(!is_type_valid(type)) {
1356 skip_initializers();
1360 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1363 designator_t *designator = NULL;
1364 if(token.type == '.' || token.type == '[') {
1365 designator = parse_designation();
1367 /* reset path to toplevel, evaluate designator from there */
1368 ascend_to(path, top_path_level);
1369 if(!walk_designator(path, designator, false)) {
1370 /* can't continue after designation error */
1374 initializer_t *designator_initializer
1375 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1376 designator_initializer->designator.designator = designator;
1377 ARR_APP1(initializer_t*, initializers, designator_initializer);
1382 if(token.type == '{') {
1383 if(is_type_scalar(type)) {
1384 sub = parse_scalar_initializer(type, must_be_constant);
1387 descend_into_subtype(path);
1389 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1392 ascend_from_subtype(path);
1397 /* must be an expression */
1398 expression_t *expression = parse_assignment_expression();
1400 if(must_be_constant && !is_initializer_constant(expression)) {
1401 errorf(expression->base.source_position,
1402 "Initialisation expression '%E' is not constant\n",
1406 /* handle { "string" } special case */
1407 if((expression->kind == EXPR_STRING_LITERAL
1408 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1409 && outer_type != NULL) {
1410 sub = initializer_from_expression(outer_type, expression);
1412 if(token.type == ',') {
1415 if(token.type != '}') {
1416 warningf(HERE, "excessive elements in initializer for type '%T'",
1419 /* TODO: eat , ... */
1424 /* descend into subtypes until expression matches type */
1426 orig_type = path->top_type;
1427 type = skip_typeref(orig_type);
1429 sub = initializer_from_expression(orig_type, expression);
1433 if(!is_type_valid(type)) {
1436 if(is_type_scalar(type)) {
1437 errorf(expression->base.source_position,
1438 "expression '%E' doesn't match expected type '%T'",
1439 expression, orig_type);
1443 descend_into_subtype(path);
1447 /* update largest index of top array */
1448 const type_path_entry_t *first = &path->path[0];
1449 type_t *first_type = first->type;
1450 first_type = skip_typeref(first_type);
1451 if(is_type_array(first_type)) {
1452 size_t index = first->v.index;
1453 if(index > path->max_index)
1454 path->max_index = index;
1457 /* append to initializers list */
1458 ARR_APP1(initializer_t*, initializers, sub);
1460 if(token.type == '}') {
1464 if(token.type == '}') {
1468 advance_current_object(path, top_path_level);
1469 orig_type = path->top_type;
1470 type = skip_typeref(orig_type);
1473 size_t len = ARR_LEN(initializers);
1474 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1475 initializer_t *result = allocate_ast_zero(size);
1476 result->kind = INITIALIZER_LIST;
1477 result->list.len = len;
1478 memcpy(&result->list.initializers, initializers,
1479 len * sizeof(initializers[0]));
1481 ascend_to(path, top_path_level);
1486 skip_initializers();
1487 DEL_ARR_F(initializers);
1488 ascend_to(path, top_path_level);
1492 typedef struct parse_initializer_env_t {
1493 type_t *type; /* the type of the initializer. In case of an
1494 array type with unspecified size this gets
1495 adjusted to the actual size. */
1496 initializer_t *initializer; /* initializer will be filled in here */
1497 bool must_be_constant;
1498 } parse_initializer_env_t;
1500 static void parse_initializer(parse_initializer_env_t *env)
1502 type_t *type = skip_typeref(env->type);
1503 initializer_t *result = NULL;
1506 if(is_type_scalar(type)) {
1507 /* TODO: § 6.7.8.11; eat {} without warning */
1508 result = parse_scalar_initializer(type, env->must_be_constant);
1509 } else if(token.type == '{') {
1513 memset(&path, 0, sizeof(path));
1514 path.top_type = env->type;
1515 path.path = NEW_ARR_F(type_path_entry_t, 0);
1517 descend_into_subtype(&path);
1519 result = parse_sub_initializer(&path, env->type, 1,
1520 env->must_be_constant);
1522 max_index = path.max_index;
1523 DEL_ARR_F(path.path);
1527 /* parse_scalar_initializer also works in this case: we simply
1528 * have an expression without {} around it */
1529 result = parse_scalar_initializer(type, env->must_be_constant);
1532 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1533 * the array type size */
1534 if(is_type_array(type) && type->array.size_expression == NULL
1535 && result != NULL) {
1537 switch (result->kind) {
1538 case INITIALIZER_LIST:
1539 size = max_index + 1;
1542 case INITIALIZER_STRING:
1543 size = result->string.string.size;
1546 case INITIALIZER_WIDE_STRING:
1547 size = result->wide_string.string.size;
1551 panic("invalid initializer type");
1554 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1555 cnst->base.type = type_size_t;
1556 cnst->conste.v.int_value = size;
1558 type_t *new_type = duplicate_type(type);
1560 new_type->array.size_expression = cnst;
1561 new_type->array.size_constant = true;
1562 new_type->array.size = size;
1563 env->type = new_type;
1566 env->initializer = result;
1569 static declaration_t *append_declaration(declaration_t *declaration);
1571 static declaration_t *parse_compound_type_specifier(bool is_struct)
1579 symbol_t *symbol = NULL;
1580 declaration_t *declaration = NULL;
1582 if (token.type == T___attribute__) {
1587 if(token.type == T_IDENTIFIER) {
1588 symbol = token.v.symbol;
1592 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1594 declaration = get_declaration(symbol, NAMESPACE_UNION);
1596 } else if(token.type != '{') {
1598 parse_error_expected("while parsing struct type specifier",
1599 T_IDENTIFIER, '{', 0);
1601 parse_error_expected("while parsing union type specifier",
1602 T_IDENTIFIER, '{', 0);
1608 if(declaration == NULL) {
1609 declaration = allocate_declaration_zero();
1610 declaration->namespc =
1611 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1612 declaration->source_position = token.source_position;
1613 declaration->symbol = symbol;
1614 declaration->parent_scope = scope;
1615 if (symbol != NULL) {
1616 environment_push(declaration);
1618 append_declaration(declaration);
1621 if(token.type == '{') {
1622 if(declaration->init.is_defined) {
1623 assert(symbol != NULL);
1624 errorf(HERE, "multiple definitions of '%s %Y'",
1625 is_struct ? "struct" : "union", symbol);
1626 declaration->scope.declarations = NULL;
1628 declaration->init.is_defined = true;
1630 parse_compound_type_entries(declaration);
1637 static void parse_enum_entries(type_t *const enum_type)
1641 if(token.type == '}') {
1643 errorf(HERE, "empty enum not allowed");
1648 if(token.type != T_IDENTIFIER) {
1649 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1654 declaration_t *const entry = allocate_declaration_zero();
1655 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1656 entry->type = enum_type;
1657 entry->symbol = token.v.symbol;
1658 entry->source_position = token.source_position;
1661 if(token.type == '=') {
1663 expression_t *value = parse_constant_expression();
1665 value = create_implicit_cast(value, enum_type);
1666 entry->init.enum_value = value;
1671 record_declaration(entry);
1673 if(token.type != ',')
1676 } while(token.type != '}');
1681 static type_t *parse_enum_specifier(void)
1685 declaration_t *declaration;
1688 if(token.type == T_IDENTIFIER) {
1689 symbol = token.v.symbol;
1692 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1693 } else if(token.type != '{') {
1694 parse_error_expected("while parsing enum type specifier",
1695 T_IDENTIFIER, '{', 0);
1702 if(declaration == NULL) {
1703 declaration = allocate_declaration_zero();
1704 declaration->namespc = NAMESPACE_ENUM;
1705 declaration->source_position = token.source_position;
1706 declaration->symbol = symbol;
1707 declaration->parent_scope = scope;
1710 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1711 type->enumt.declaration = declaration;
1713 if(token.type == '{') {
1714 if(declaration->init.is_defined) {
1715 errorf(HERE, "multiple definitions of enum %Y", symbol);
1717 if (symbol != NULL) {
1718 environment_push(declaration);
1720 append_declaration(declaration);
1721 declaration->init.is_defined = 1;
1723 parse_enum_entries(type);
1731 * if a symbol is a typedef to another type, return true
1733 static bool is_typedef_symbol(symbol_t *symbol)
1735 const declaration_t *const declaration =
1736 get_declaration(symbol, NAMESPACE_NORMAL);
1738 declaration != NULL &&
1739 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1742 static type_t *parse_typeof(void)
1750 expression_t *expression = NULL;
1753 switch(token.type) {
1754 case T___extension__:
1755 /* this can be a prefix to a typename or an expression */
1756 /* we simply eat it now. */
1759 } while(token.type == T___extension__);
1763 if(is_typedef_symbol(token.v.symbol)) {
1764 type = parse_typename();
1766 expression = parse_expression();
1767 type = expression->base.type;
1772 type = parse_typename();
1776 expression = parse_expression();
1777 type = expression->base.type;
1783 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1784 typeof_type->typeoft.expression = expression;
1785 typeof_type->typeoft.typeof_type = type;
1791 SPECIFIER_SIGNED = 1 << 0,
1792 SPECIFIER_UNSIGNED = 1 << 1,
1793 SPECIFIER_LONG = 1 << 2,
1794 SPECIFIER_INT = 1 << 3,
1795 SPECIFIER_DOUBLE = 1 << 4,
1796 SPECIFIER_CHAR = 1 << 5,
1797 SPECIFIER_SHORT = 1 << 6,
1798 SPECIFIER_LONG_LONG = 1 << 7,
1799 SPECIFIER_FLOAT = 1 << 8,
1800 SPECIFIER_BOOL = 1 << 9,
1801 SPECIFIER_VOID = 1 << 10,
1802 #ifdef PROVIDE_COMPLEX
1803 SPECIFIER_COMPLEX = 1 << 11,
1804 SPECIFIER_IMAGINARY = 1 << 12,
1808 static type_t *create_builtin_type(symbol_t *const symbol,
1809 type_t *const real_type)
1811 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1812 type->builtin.symbol = symbol;
1813 type->builtin.real_type = real_type;
1815 type_t *result = typehash_insert(type);
1816 if (type != result) {
1823 static type_t *get_typedef_type(symbol_t *symbol)
1825 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1826 if(declaration == NULL
1827 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1830 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1831 type->typedeft.declaration = declaration;
1836 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1838 type_t *type = NULL;
1839 unsigned type_qualifiers = 0;
1840 unsigned type_specifiers = 0;
1843 specifiers->source_position = token.source_position;
1846 switch(token.type) {
1849 #define MATCH_STORAGE_CLASS(token, class) \
1851 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
1852 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1854 specifiers->declared_storage_class = class; \
1858 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1859 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1860 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1861 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1862 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1865 switch (specifiers->declared_storage_class) {
1866 case STORAGE_CLASS_NONE:
1867 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
1870 case STORAGE_CLASS_EXTERN:
1871 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
1874 case STORAGE_CLASS_STATIC:
1875 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
1879 errorf(HERE, "multiple storage classes in declaration specifiers");
1885 /* type qualifiers */
1886 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1888 type_qualifiers |= qualifier; \
1892 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1893 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1894 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1896 case T___extension__:
1901 /* type specifiers */
1902 #define MATCH_SPECIFIER(token, specifier, name) \
1905 if(type_specifiers & specifier) { \
1906 errorf(HERE, "multiple " name " type specifiers given"); \
1908 type_specifiers |= specifier; \
1912 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1913 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1914 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1915 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1916 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1917 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1918 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1919 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1920 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1921 #ifdef PROVIDE_COMPLEX
1922 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1923 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1926 /* only in microsoft mode */
1927 specifiers->decl_modifiers |= DM_FORCEINLINE;
1931 specifiers->is_inline = true;
1936 if(type_specifiers & SPECIFIER_LONG_LONG) {
1937 errorf(HERE, "multiple type specifiers given");
1938 } else if(type_specifiers & SPECIFIER_LONG) {
1939 type_specifiers |= SPECIFIER_LONG_LONG;
1941 type_specifiers |= SPECIFIER_LONG;
1946 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1948 type->compound.declaration = parse_compound_type_specifier(true);
1952 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1954 type->compound.declaration = parse_compound_type_specifier(false);
1958 type = parse_enum_specifier();
1961 type = parse_typeof();
1963 case T___builtin_va_list:
1964 type = duplicate_type(type_valist);
1968 case T___attribute__:
1972 case T_IDENTIFIER: {
1973 /* only parse identifier if we haven't found a type yet */
1974 if(type != NULL || type_specifiers != 0)
1975 goto finish_specifiers;
1977 type_t *typedef_type = get_typedef_type(token.v.symbol);
1979 if(typedef_type == NULL)
1980 goto finish_specifiers;
1983 type = typedef_type;
1987 /* function specifier */
1989 goto finish_specifiers;
1996 atomic_type_kind_t atomic_type;
1998 /* match valid basic types */
1999 switch(type_specifiers) {
2000 case SPECIFIER_VOID:
2001 atomic_type = ATOMIC_TYPE_VOID;
2003 case SPECIFIER_CHAR:
2004 atomic_type = ATOMIC_TYPE_CHAR;
2006 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2007 atomic_type = ATOMIC_TYPE_SCHAR;
2009 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2010 atomic_type = ATOMIC_TYPE_UCHAR;
2012 case SPECIFIER_SHORT:
2013 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2014 case SPECIFIER_SHORT | SPECIFIER_INT:
2015 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2016 atomic_type = ATOMIC_TYPE_SHORT;
2018 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2019 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2020 atomic_type = ATOMIC_TYPE_USHORT;
2023 case SPECIFIER_SIGNED:
2024 case SPECIFIER_SIGNED | SPECIFIER_INT:
2025 atomic_type = ATOMIC_TYPE_INT;
2027 case SPECIFIER_UNSIGNED:
2028 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2029 atomic_type = ATOMIC_TYPE_UINT;
2031 case SPECIFIER_LONG:
2032 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2033 case SPECIFIER_LONG | SPECIFIER_INT:
2034 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2035 atomic_type = ATOMIC_TYPE_LONG;
2037 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2038 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2039 atomic_type = ATOMIC_TYPE_ULONG;
2041 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2042 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2043 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2044 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2046 atomic_type = ATOMIC_TYPE_LONGLONG;
2048 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2049 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2051 atomic_type = ATOMIC_TYPE_ULONGLONG;
2053 case SPECIFIER_FLOAT:
2054 atomic_type = ATOMIC_TYPE_FLOAT;
2056 case SPECIFIER_DOUBLE:
2057 atomic_type = ATOMIC_TYPE_DOUBLE;
2059 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2060 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2062 case SPECIFIER_BOOL:
2063 atomic_type = ATOMIC_TYPE_BOOL;
2065 #ifdef PROVIDE_COMPLEX
2066 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2067 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2069 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2070 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2072 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2073 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2075 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2076 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2078 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2079 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2081 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2082 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2086 /* invalid specifier combination, give an error message */
2087 if(type_specifiers == 0) {
2088 if (! strict_mode) {
2089 if (warning.implicit_int) {
2090 warningf(HERE, "no type specifiers in declaration, using 'int'");
2092 atomic_type = ATOMIC_TYPE_INT;
2095 errorf(HERE, "no type specifiers given in declaration");
2097 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2098 (type_specifiers & SPECIFIER_UNSIGNED)) {
2099 errorf(HERE, "signed and unsigned specifiers gives");
2100 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2101 errorf(HERE, "only integer types can be signed or unsigned");
2103 errorf(HERE, "multiple datatypes in declaration");
2105 atomic_type = ATOMIC_TYPE_INVALID;
2108 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2109 type->atomic.akind = atomic_type;
2112 if(type_specifiers != 0) {
2113 errorf(HERE, "multiple datatypes in declaration");
2117 type->base.qualifiers = type_qualifiers;
2119 type_t *result = typehash_insert(type);
2120 if(newtype && result != type) {
2124 specifiers->type = result;
2127 static type_qualifiers_t parse_type_qualifiers(void)
2129 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2132 switch(token.type) {
2133 /* type qualifiers */
2134 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2135 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2136 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2139 return type_qualifiers;
2144 static declaration_t *parse_identifier_list(void)
2146 declaration_t *declarations = NULL;
2147 declaration_t *last_declaration = NULL;
2149 declaration_t *const declaration = allocate_declaration_zero();
2150 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2151 declaration->source_position = token.source_position;
2152 declaration->symbol = token.v.symbol;
2155 if(last_declaration != NULL) {
2156 last_declaration->next = declaration;
2158 declarations = declaration;
2160 last_declaration = declaration;
2162 if(token.type != ',')
2165 } while(token.type == T_IDENTIFIER);
2167 return declarations;
2170 static void semantic_parameter(declaration_t *declaration)
2172 /* TODO: improve error messages */
2174 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2175 errorf(HERE, "typedef not allowed in parameter list");
2176 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2177 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2178 errorf(HERE, "parameter may only have none or register storage class");
2181 type_t *const orig_type = declaration->type;
2182 type_t * type = skip_typeref(orig_type);
2184 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2185 * into a pointer. § 6.7.5.3 (7) */
2186 if (is_type_array(type)) {
2187 type_t *const element_type = type->array.element_type;
2189 type = make_pointer_type(element_type, type->base.qualifiers);
2191 declaration->type = type;
2194 if(is_type_incomplete(type)) {
2195 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2196 orig_type, declaration->symbol);
2200 static declaration_t *parse_parameter(void)
2202 declaration_specifiers_t specifiers;
2203 memset(&specifiers, 0, sizeof(specifiers));
2205 parse_declaration_specifiers(&specifiers);
2207 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2209 semantic_parameter(declaration);
2214 static declaration_t *parse_parameters(function_type_t *type)
2216 if(token.type == T_IDENTIFIER) {
2217 symbol_t *symbol = token.v.symbol;
2218 if(!is_typedef_symbol(symbol)) {
2219 type->kr_style_parameters = true;
2220 return parse_identifier_list();
2224 if(token.type == ')') {
2225 type->unspecified_parameters = 1;
2228 if(token.type == T_void && look_ahead(1)->type == ')') {
2233 declaration_t *declarations = NULL;
2234 declaration_t *declaration;
2235 declaration_t *last_declaration = NULL;
2236 function_parameter_t *parameter;
2237 function_parameter_t *last_parameter = NULL;
2240 switch(token.type) {
2244 return declarations;
2247 case T___extension__:
2249 declaration = parse_parameter();
2251 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2252 memset(parameter, 0, sizeof(parameter[0]));
2253 parameter->type = declaration->type;
2255 if(last_parameter != NULL) {
2256 last_declaration->next = declaration;
2257 last_parameter->next = parameter;
2259 type->parameters = parameter;
2260 declarations = declaration;
2262 last_parameter = parameter;
2263 last_declaration = declaration;
2267 return declarations;
2269 if(token.type != ',')
2270 return declarations;
2280 } construct_type_kind_t;
2282 typedef struct construct_type_t construct_type_t;
2283 struct construct_type_t {
2284 construct_type_kind_t kind;
2285 construct_type_t *next;
2288 typedef struct parsed_pointer_t parsed_pointer_t;
2289 struct parsed_pointer_t {
2290 construct_type_t construct_type;
2291 type_qualifiers_t type_qualifiers;
2294 typedef struct construct_function_type_t construct_function_type_t;
2295 struct construct_function_type_t {
2296 construct_type_t construct_type;
2297 type_t *function_type;
2300 typedef struct parsed_array_t parsed_array_t;
2301 struct parsed_array_t {
2302 construct_type_t construct_type;
2303 type_qualifiers_t type_qualifiers;
2309 typedef struct construct_base_type_t construct_base_type_t;
2310 struct construct_base_type_t {
2311 construct_type_t construct_type;
2315 static construct_type_t *parse_pointer_declarator(void)
2319 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2320 memset(pointer, 0, sizeof(pointer[0]));
2321 pointer->construct_type.kind = CONSTRUCT_POINTER;
2322 pointer->type_qualifiers = parse_type_qualifiers();
2324 return (construct_type_t*) pointer;
2327 static construct_type_t *parse_array_declarator(void)
2331 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2332 memset(array, 0, sizeof(array[0]));
2333 array->construct_type.kind = CONSTRUCT_ARRAY;
2335 if(token.type == T_static) {
2336 array->is_static = true;
2340 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2341 if(type_qualifiers != 0) {
2342 if(token.type == T_static) {
2343 array->is_static = true;
2347 array->type_qualifiers = type_qualifiers;
2349 if(token.type == '*' && look_ahead(1)->type == ']') {
2350 array->is_variable = true;
2352 } else if(token.type != ']') {
2353 array->size = parse_assignment_expression();
2358 return (construct_type_t*) array;
2361 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2366 if(declaration != NULL) {
2367 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2369 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2372 declaration_t *parameters = parse_parameters(&type->function);
2373 if(declaration != NULL) {
2374 declaration->scope.declarations = parameters;
2377 construct_function_type_t *construct_function_type =
2378 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2379 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2380 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2381 construct_function_type->function_type = type;
2385 return (construct_type_t*) construct_function_type;
2388 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2389 bool may_be_abstract)
2391 /* construct a single linked list of construct_type_t's which describe
2392 * how to construct the final declarator type */
2393 construct_type_t *first = NULL;
2394 construct_type_t *last = NULL;
2397 while(token.type == '*') {
2398 construct_type_t *type = parse_pointer_declarator();
2409 /* TODO: find out if this is correct */
2412 construct_type_t *inner_types = NULL;
2414 switch(token.type) {
2416 if(declaration == NULL) {
2417 errorf(HERE, "no identifier expected in typename");
2419 declaration->symbol = token.v.symbol;
2420 declaration->source_position = token.source_position;
2426 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2432 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2433 /* avoid a loop in the outermost scope, because eat_statement doesn't
2435 if(token.type == '}' && current_function == NULL) {
2443 construct_type_t *p = last;
2446 construct_type_t *type;
2447 switch(token.type) {
2449 type = parse_function_declarator(declaration);
2452 type = parse_array_declarator();
2455 goto declarator_finished;
2458 /* insert in the middle of the list (behind p) */
2460 type->next = p->next;
2471 declarator_finished:
2474 /* append inner_types at the end of the list, we don't to set last anymore
2475 * as it's not needed anymore */
2477 assert(first == NULL);
2478 first = inner_types;
2480 last->next = inner_types;
2486 static type_t *construct_declarator_type(construct_type_t *construct_list,
2489 construct_type_t *iter = construct_list;
2490 for( ; iter != NULL; iter = iter->next) {
2491 switch(iter->kind) {
2492 case CONSTRUCT_INVALID:
2493 panic("invalid type construction found");
2494 case CONSTRUCT_FUNCTION: {
2495 construct_function_type_t *construct_function_type
2496 = (construct_function_type_t*) iter;
2498 type_t *function_type = construct_function_type->function_type;
2500 function_type->function.return_type = type;
2502 type_t *skipped_return_type = skip_typeref(type);
2503 if (is_type_function(skipped_return_type)) {
2504 errorf(HERE, "function returning function is not allowed");
2505 type = type_error_type;
2506 } else if (is_type_array(skipped_return_type)) {
2507 errorf(HERE, "function returning array is not allowed");
2508 type = type_error_type;
2510 type = function_type;
2515 case CONSTRUCT_POINTER: {
2516 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2517 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2518 pointer_type->pointer.points_to = type;
2519 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2521 type = pointer_type;
2525 case CONSTRUCT_ARRAY: {
2526 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2527 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2529 expression_t *size_expression = parsed_array->size;
2530 if(size_expression != NULL) {
2532 = create_implicit_cast(size_expression, type_size_t);
2535 array_type->base.qualifiers = parsed_array->type_qualifiers;
2536 array_type->array.element_type = type;
2537 array_type->array.is_static = parsed_array->is_static;
2538 array_type->array.is_variable = parsed_array->is_variable;
2539 array_type->array.size_expression = size_expression;
2541 if(size_expression != NULL) {
2542 if(is_constant_expression(size_expression)) {
2543 array_type->array.size_constant = true;
2544 array_type->array.size
2545 = fold_constant(size_expression);
2547 array_type->array.is_vla = true;
2551 type_t *skipped_type = skip_typeref(type);
2552 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2553 errorf(HERE, "array of void is not allowed");
2554 type = type_error_type;
2562 type_t *hashed_type = typehash_insert(type);
2563 if(hashed_type != type) {
2564 /* the function type was constructed earlier freeing it here will
2565 * destroy other types... */
2566 if(iter->kind != CONSTRUCT_FUNCTION) {
2576 static declaration_t *parse_declarator(
2577 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2579 declaration_t *const declaration = allocate_declaration_zero();
2580 declaration->declared_storage_class = specifiers->declared_storage_class;
2581 declaration->modifiers = specifiers->decl_modifiers;
2582 declaration->is_inline = specifiers->is_inline;
2584 declaration->storage_class = specifiers->declared_storage_class;
2585 if(declaration->storage_class == STORAGE_CLASS_NONE
2586 && scope != global_scope) {
2587 declaration->storage_class = STORAGE_CLASS_AUTO;
2590 construct_type_t *construct_type
2591 = parse_inner_declarator(declaration, may_be_abstract);
2592 type_t *const type = specifiers->type;
2593 declaration->type = construct_declarator_type(construct_type, type);
2595 if(construct_type != NULL) {
2596 obstack_free(&temp_obst, construct_type);
2602 static type_t *parse_abstract_declarator(type_t *base_type)
2604 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2606 type_t *result = construct_declarator_type(construct_type, base_type);
2607 if(construct_type != NULL) {
2608 obstack_free(&temp_obst, construct_type);
2614 static declaration_t *append_declaration(declaration_t* const declaration)
2616 if (last_declaration != NULL) {
2617 last_declaration->next = declaration;
2619 scope->declarations = declaration;
2621 last_declaration = declaration;
2626 * Check if the declaration of main is suspicious. main should be a
2627 * function with external linkage, returning int, taking either zero
2628 * arguments, two, or three arguments of appropriate types, ie.
2630 * int main([ int argc, char **argv [, char **env ] ]).
2632 * @param decl the declaration to check
2633 * @param type the function type of the declaration
2635 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2637 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2638 warningf(decl->source_position, "'main' is normally a non-static function");
2640 if (skip_typeref(func_type->return_type) != type_int) {
2641 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2643 const function_parameter_t *parm = func_type->parameters;
2645 type_t *const first_type = parm->type;
2646 if (!types_compatible(skip_typeref(first_type), type_int)) {
2647 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2651 type_t *const second_type = parm->type;
2652 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2653 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2657 type_t *const third_type = parm->type;
2658 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2659 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2663 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2667 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2673 * Check if a symbol is the equal to "main".
2675 static bool is_sym_main(const symbol_t *const sym)
2677 return strcmp(sym->string, "main") == 0;
2680 static declaration_t *internal_record_declaration(
2681 declaration_t *const declaration,
2682 const bool is_function_definition)
2684 const symbol_t *const symbol = declaration->symbol;
2685 const namespace_t namespc = (namespace_t)declaration->namespc;
2687 type_t *const orig_type = declaration->type;
2688 type_t *const type = skip_typeref(orig_type);
2689 if (is_type_function(type) &&
2690 type->function.unspecified_parameters &&
2691 warning.strict_prototypes) {
2692 warningf(declaration->source_position,
2693 "function declaration '%#T' is not a prototype",
2694 orig_type, declaration->symbol);
2697 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2698 check_type_of_main(declaration, &type->function);
2701 assert(declaration->symbol != NULL);
2702 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2704 assert(declaration != previous_declaration);
2705 if (previous_declaration != NULL) {
2706 if (previous_declaration->parent_scope == scope) {
2707 /* can happen for K&R style declarations */
2708 if(previous_declaration->type == NULL) {
2709 previous_declaration->type = declaration->type;
2712 const type_t *prev_type = skip_typeref(previous_declaration->type);
2713 if (!types_compatible(type, prev_type)) {
2714 errorf(declaration->source_position,
2715 "declaration '%#T' is incompatible with "
2716 "previous declaration '%#T'",
2717 orig_type, symbol, previous_declaration->type, symbol);
2718 errorf(previous_declaration->source_position,
2719 "previous declaration of '%Y' was here", symbol);
2721 unsigned old_storage_class
2722 = previous_declaration->storage_class;
2723 unsigned new_storage_class = declaration->storage_class;
2725 if(is_type_incomplete(prev_type)) {
2726 previous_declaration->type = type;
2730 /* pretend no storage class means extern for function
2731 * declarations (except if the previous declaration is neither
2732 * none nor extern) */
2733 if (is_type_function(type)) {
2734 switch (old_storage_class) {
2735 case STORAGE_CLASS_NONE:
2736 old_storage_class = STORAGE_CLASS_EXTERN;
2738 case STORAGE_CLASS_EXTERN:
2739 if (is_function_definition) {
2740 if (warning.missing_prototypes &&
2741 prev_type->function.unspecified_parameters &&
2742 !is_sym_main(symbol)) {
2743 warningf(declaration->source_position,
2744 "no previous prototype for '%#T'",
2747 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2748 new_storage_class = STORAGE_CLASS_EXTERN;
2756 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2757 new_storage_class == STORAGE_CLASS_EXTERN) {
2758 warn_redundant_declaration:
2759 if (warning.redundant_decls) {
2760 warningf(declaration->source_position,
2761 "redundant declaration for '%Y'", symbol);
2762 warningf(previous_declaration->source_position,
2763 "previous declaration of '%Y' was here",
2766 } else if (current_function == NULL) {
2767 if (old_storage_class != STORAGE_CLASS_STATIC &&
2768 new_storage_class == STORAGE_CLASS_STATIC) {
2769 errorf(declaration->source_position,
2770 "static declaration of '%Y' follows non-static declaration",
2772 errorf(previous_declaration->source_position,
2773 "previous declaration of '%Y' was here", symbol);
2775 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2776 goto warn_redundant_declaration;
2778 if (new_storage_class == STORAGE_CLASS_NONE) {
2779 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2780 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
2784 if (old_storage_class == new_storage_class) {
2785 errorf(declaration->source_position,
2786 "redeclaration of '%Y'", symbol);
2788 errorf(declaration->source_position,
2789 "redeclaration of '%Y' with different linkage",
2792 errorf(previous_declaration->source_position,
2793 "previous declaration of '%Y' was here", symbol);
2796 return previous_declaration;
2798 } else if (is_function_definition) {
2799 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2800 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2801 warningf(declaration->source_position,
2802 "no previous prototype for '%#T'", orig_type, symbol);
2803 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2804 warningf(declaration->source_position,
2805 "no previous declaration for '%#T'", orig_type,
2809 } else if (warning.missing_declarations &&
2810 scope == global_scope &&
2811 !is_type_function(type) && (
2812 declaration->storage_class == STORAGE_CLASS_NONE ||
2813 declaration->storage_class == STORAGE_CLASS_THREAD
2815 warningf(declaration->source_position,
2816 "no previous declaration for '%#T'", orig_type, symbol);
2819 assert(declaration->parent_scope == NULL);
2820 assert(scope != NULL);
2822 declaration->parent_scope = scope;
2824 environment_push(declaration);
2825 return append_declaration(declaration);
2828 static declaration_t *record_declaration(declaration_t *declaration)
2830 return internal_record_declaration(declaration, false);
2833 static declaration_t *record_function_definition(declaration_t *declaration)
2835 return internal_record_declaration(declaration, true);
2838 static void parser_error_multiple_definition(declaration_t *declaration,
2839 const source_position_t source_position)
2841 errorf(source_position, "multiple definition of symbol '%Y'",
2842 declaration->symbol);
2843 errorf(declaration->source_position,
2844 "this is the location of the previous definition.");
2847 static bool is_declaration_specifier(const token_t *token,
2848 bool only_type_specifiers)
2850 switch(token->type) {
2854 return is_typedef_symbol(token->v.symbol);
2856 case T___extension__:
2859 return !only_type_specifiers;
2866 static void parse_init_declarator_rest(declaration_t *declaration)
2870 type_t *orig_type = declaration->type;
2871 type_t *type = skip_typeref(orig_type);
2873 if(declaration->init.initializer != NULL) {
2874 parser_error_multiple_definition(declaration, token.source_position);
2877 bool must_be_constant = false;
2878 if(declaration->storage_class == STORAGE_CLASS_STATIC
2879 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
2880 || declaration->parent_scope == global_scope) {
2881 must_be_constant = true;
2884 parse_initializer_env_t env;
2885 env.type = orig_type;
2886 env.must_be_constant = must_be_constant;
2887 parse_initializer(&env);
2889 if(env.type != orig_type) {
2890 orig_type = env.type;
2891 type = skip_typeref(orig_type);
2892 declaration->type = env.type;
2895 if(is_type_function(type)) {
2896 errorf(declaration->source_position,
2897 "initializers not allowed for function types at declator '%Y' (type '%T')",
2898 declaration->symbol, orig_type);
2900 declaration->init.initializer = env.initializer;
2904 /* parse rest of a declaration without any declarator */
2905 static void parse_anonymous_declaration_rest(
2906 const declaration_specifiers_t *specifiers,
2907 parsed_declaration_func finished_declaration)
2911 declaration_t *const declaration = allocate_declaration_zero();
2912 declaration->type = specifiers->type;
2913 declaration->declared_storage_class = specifiers->declared_storage_class;
2914 declaration->source_position = specifiers->source_position;
2916 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
2917 warningf(declaration->source_position, "useless storage class in empty declaration");
2919 declaration->storage_class = STORAGE_CLASS_NONE;
2921 type_t *type = declaration->type;
2922 switch (type->kind) {
2923 case TYPE_COMPOUND_STRUCT:
2924 case TYPE_COMPOUND_UNION: {
2925 if (type->compound.declaration->symbol == NULL) {
2926 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2935 warningf(declaration->source_position, "empty declaration");
2939 finished_declaration(declaration);
2942 static void parse_declaration_rest(declaration_t *ndeclaration,
2943 const declaration_specifiers_t *specifiers,
2944 parsed_declaration_func finished_declaration)
2947 declaration_t *declaration = finished_declaration(ndeclaration);
2949 type_t *orig_type = declaration->type;
2950 type_t *type = skip_typeref(orig_type);
2952 if (type->kind != TYPE_FUNCTION &&
2953 declaration->is_inline &&
2954 is_type_valid(type)) {
2955 warningf(declaration->source_position,
2956 "variable '%Y' declared 'inline'\n", declaration->symbol);
2959 if(token.type == '=') {
2960 parse_init_declarator_rest(declaration);
2963 if(token.type != ',')
2967 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2972 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2974 symbol_t *symbol = declaration->symbol;
2975 if(symbol == NULL) {
2976 errorf(HERE, "anonymous declaration not valid as function parameter");
2979 namespace_t namespc = (namespace_t) declaration->namespc;
2980 if(namespc != NAMESPACE_NORMAL) {
2981 return record_declaration(declaration);
2984 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2985 if(previous_declaration == NULL ||
2986 previous_declaration->parent_scope != scope) {
2987 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2992 if(previous_declaration->type == NULL) {
2993 previous_declaration->type = declaration->type;
2994 previous_declaration->declared_storage_class = declaration->declared_storage_class;
2995 previous_declaration->storage_class = declaration->storage_class;
2996 previous_declaration->parent_scope = scope;
2997 return previous_declaration;
2999 return record_declaration(declaration);
3003 static void parse_declaration(parsed_declaration_func finished_declaration)
3005 declaration_specifiers_t specifiers;
3006 memset(&specifiers, 0, sizeof(specifiers));
3007 parse_declaration_specifiers(&specifiers);
3009 if(token.type == ';') {
3010 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3012 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3013 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3017 static void parse_kr_declaration_list(declaration_t *declaration)
3019 type_t *type = skip_typeref(declaration->type);
3020 if(!is_type_function(type))
3023 if(!type->function.kr_style_parameters)
3026 /* push function parameters */
3027 int top = environment_top();
3028 scope_t *last_scope = scope;
3029 set_scope(&declaration->scope);
3031 declaration_t *parameter = declaration->scope.declarations;
3032 for( ; parameter != NULL; parameter = parameter->next) {
3033 assert(parameter->parent_scope == NULL);
3034 parameter->parent_scope = scope;
3035 environment_push(parameter);
3038 /* parse declaration list */
3039 while(is_declaration_specifier(&token, false)) {
3040 parse_declaration(finished_kr_declaration);
3043 /* pop function parameters */
3044 assert(scope == &declaration->scope);
3045 set_scope(last_scope);
3046 environment_pop_to(top);
3048 /* update function type */
3049 type_t *new_type = duplicate_type(type);
3050 new_type->function.kr_style_parameters = false;
3052 function_parameter_t *parameters = NULL;
3053 function_parameter_t *last_parameter = NULL;
3055 declaration_t *parameter_declaration = declaration->scope.declarations;
3056 for( ; parameter_declaration != NULL;
3057 parameter_declaration = parameter_declaration->next) {
3058 type_t *parameter_type = parameter_declaration->type;
3059 if(parameter_type == NULL) {
3061 errorf(HERE, "no type specified for function parameter '%Y'",
3062 parameter_declaration->symbol);
3064 if (warning.implicit_int) {
3065 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3066 parameter_declaration->symbol);
3068 parameter_type = type_int;
3069 parameter_declaration->type = parameter_type;
3073 semantic_parameter(parameter_declaration);
3074 parameter_type = parameter_declaration->type;
3076 function_parameter_t *function_parameter
3077 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3078 memset(function_parameter, 0, sizeof(function_parameter[0]));
3080 function_parameter->type = parameter_type;
3081 if(last_parameter != NULL) {
3082 last_parameter->next = function_parameter;
3084 parameters = function_parameter;
3086 last_parameter = function_parameter;
3088 new_type->function.parameters = parameters;
3090 type = typehash_insert(new_type);
3091 if(type != new_type) {
3092 obstack_free(type_obst, new_type);
3095 declaration->type = type;
3098 static bool first_err = true;
3101 * When called with first_err set, prints the name of the current function,
3104 static void print_in_function(void) {
3107 diagnosticf("%s: In function '%Y':\n",
3108 current_function->source_position.input_name,
3109 current_function->symbol);
3114 * Check if all labels are defined in the current function.
3115 * Check if all labels are used in the current function.
3117 static void check_labels(void)
3119 for (const goto_statement_t *goto_statement = goto_first;
3120 goto_statement != NULL;
3121 goto_statement = goto_statement->next) {
3122 declaration_t *label = goto_statement->label;
3125 if (label->source_position.input_name == NULL) {
3126 print_in_function();
3127 errorf(goto_statement->base.source_position,
3128 "label '%Y' used but not defined", label->symbol);
3131 goto_first = goto_last = NULL;
3133 if (warning.unused_label) {
3134 for (const label_statement_t *label_statement = label_first;
3135 label_statement != NULL;
3136 label_statement = label_statement->next) {
3137 const declaration_t *label = label_statement->label;
3139 if (! label->used) {
3140 print_in_function();
3141 warningf(label_statement->base.source_position,
3142 "label '%Y' defined but not used", label->symbol);
3146 label_first = label_last = NULL;
3150 * Check declarations of current_function for unused entities.
3152 static void check_declarations(void)
3154 if (warning.unused_parameter) {
3155 const scope_t *scope = ¤t_function->scope;
3157 const declaration_t *parameter = scope->declarations;
3158 for (; parameter != NULL; parameter = parameter->next) {
3159 if (! parameter->used) {
3160 print_in_function();
3161 warningf(parameter->source_position,
3162 "unused parameter '%Y'", parameter->symbol);
3166 if (warning.unused_variable) {
3170 static void parse_external_declaration(void)
3172 /* function-definitions and declarations both start with declaration
3174 declaration_specifiers_t specifiers;
3175 memset(&specifiers, 0, sizeof(specifiers));
3176 parse_declaration_specifiers(&specifiers);
3178 /* must be a declaration */
3179 if(token.type == ';') {
3180 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3184 /* declarator is common to both function-definitions and declarations */
3185 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3187 /* must be a declaration */
3188 if(token.type == ',' || token.type == '=' || token.type == ';') {
3189 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3193 /* must be a function definition */
3194 parse_kr_declaration_list(ndeclaration);
3196 if(token.type != '{') {
3197 parse_error_expected("while parsing function definition", '{', 0);
3202 type_t *type = ndeclaration->type;
3204 /* note that we don't skip typerefs: the standard doesn't allow them here
3205 * (so we can't use is_type_function here) */
3206 if(type->kind != TYPE_FUNCTION) {
3207 if (is_type_valid(type)) {
3208 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3209 type, ndeclaration->symbol);
3215 /* § 6.7.5.3 (14) a function definition with () means no
3216 * parameters (and not unspecified parameters) */
3217 if(type->function.unspecified_parameters) {
3218 type_t *duplicate = duplicate_type(type);
3219 duplicate->function.unspecified_parameters = false;
3221 type = typehash_insert(duplicate);
3222 if(type != duplicate) {
3223 obstack_free(type_obst, duplicate);
3225 ndeclaration->type = type;
3228 declaration_t *const declaration = record_function_definition(ndeclaration);
3229 if(ndeclaration != declaration) {
3230 declaration->scope = ndeclaration->scope;
3232 type = skip_typeref(declaration->type);
3234 /* push function parameters and switch scope */
3235 int top = environment_top();
3236 scope_t *last_scope = scope;
3237 set_scope(&declaration->scope);
3239 declaration_t *parameter = declaration->scope.declarations;
3240 for( ; parameter != NULL; parameter = parameter->next) {
3241 if(parameter->parent_scope == &ndeclaration->scope) {
3242 parameter->parent_scope = scope;
3244 assert(parameter->parent_scope == NULL
3245 || parameter->parent_scope == scope);
3246 parameter->parent_scope = scope;
3247 environment_push(parameter);
3250 if(declaration->init.statement != NULL) {
3251 parser_error_multiple_definition(declaration, token.source_position);
3253 goto end_of_parse_external_declaration;
3255 /* parse function body */
3256 int label_stack_top = label_top();
3257 declaration_t *old_current_function = current_function;
3258 current_function = declaration;
3260 declaration->init.statement = parse_compound_statement();
3263 check_declarations();
3265 assert(current_function == declaration);
3266 current_function = old_current_function;
3267 label_pop_to(label_stack_top);
3270 end_of_parse_external_declaration:
3271 assert(scope == &declaration->scope);
3272 set_scope(last_scope);
3273 environment_pop_to(top);
3276 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3277 source_position_t source_position)
3279 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3280 type->bitfield.base = base;
3281 type->bitfield.size = size;
3286 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3289 declaration_t *iter = compound_declaration->scope.declarations;
3290 for( ; iter != NULL; iter = iter->next) {
3291 if(iter->namespc != NAMESPACE_NORMAL)
3294 if(iter->symbol == NULL) {
3295 type_t *type = skip_typeref(iter->type);
3296 if(is_type_compound(type)) {
3297 declaration_t *result
3298 = find_compound_entry(type->compound.declaration, symbol);
3305 if(iter->symbol == symbol) {
3313 static void parse_compound_declarators(declaration_t *struct_declaration,
3314 const declaration_specifiers_t *specifiers)
3316 declaration_t *last_declaration = struct_declaration->scope.declarations;
3317 if(last_declaration != NULL) {
3318 while(last_declaration->next != NULL) {
3319 last_declaration = last_declaration->next;
3324 declaration_t *declaration;
3326 if(token.type == ':') {
3327 source_position_t source_position = HERE;
3330 type_t *base_type = specifiers->type;
3331 expression_t *size = parse_constant_expression();
3333 if(!is_type_integer(skip_typeref(base_type))) {
3334 errorf(HERE, "bitfield base type '%T' is not an integer type",
3338 type_t *type = make_bitfield_type(base_type, size, source_position);
3340 declaration = allocate_declaration_zero();
3341 declaration->namespc = NAMESPACE_NORMAL;
3342 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3343 declaration->storage_class = STORAGE_CLASS_NONE;
3344 declaration->source_position = source_position;
3345 declaration->modifiers = specifiers->decl_modifiers;
3346 declaration->type = type;
3348 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3350 type_t *orig_type = declaration->type;
3351 type_t *type = skip_typeref(orig_type);
3353 if(token.type == ':') {
3354 source_position_t source_position = HERE;
3356 expression_t *size = parse_constant_expression();
3358 if(!is_type_integer(type)) {
3359 errorf(HERE, "bitfield base type '%T' is not an "
3360 "integer type", orig_type);
3363 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3364 declaration->type = bitfield_type;
3366 /* TODO we ignore arrays for now... what is missing is a check
3367 * that they're at the end of the struct */
3368 if(is_type_incomplete(type) && !is_type_array(type)) {
3370 "compound member '%Y' has incomplete type '%T'",
3371 declaration->symbol, orig_type);
3372 } else if(is_type_function(type)) {
3373 errorf(HERE, "compound member '%Y' must not have function "
3374 "type '%T'", declaration->symbol, orig_type);
3379 /* make sure we don't define a symbol multiple times */
3380 symbol_t *symbol = declaration->symbol;
3381 if(symbol != NULL) {
3382 declaration_t *prev_decl
3383 = find_compound_entry(struct_declaration, symbol);
3385 if(prev_decl != NULL) {
3386 assert(prev_decl->symbol == symbol);
3387 errorf(declaration->source_position,
3388 "multiple declarations of symbol '%Y'", symbol);
3389 errorf(prev_decl->source_position,
3390 "previous declaration of '%Y' was here", symbol);
3394 /* append declaration */
3395 if(last_declaration != NULL) {
3396 last_declaration->next = declaration;
3398 struct_declaration->scope.declarations = declaration;
3400 last_declaration = declaration;
3402 if(token.type != ',')
3409 static void parse_compound_type_entries(declaration_t *compound_declaration)
3413 while(token.type != '}' && token.type != T_EOF) {
3414 declaration_specifiers_t specifiers;
3415 memset(&specifiers, 0, sizeof(specifiers));
3416 parse_declaration_specifiers(&specifiers);
3418 parse_compound_declarators(compound_declaration, &specifiers);
3420 if(token.type == T_EOF) {
3421 errorf(HERE, "EOF while parsing struct");
3426 static type_t *parse_typename(void)
3428 declaration_specifiers_t specifiers;
3429 memset(&specifiers, 0, sizeof(specifiers));
3430 parse_declaration_specifiers(&specifiers);
3431 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3432 /* TODO: improve error message, user does probably not know what a
3433 * storage class is...
3435 errorf(HERE, "typename may not have a storage class");
3438 type_t *result = parse_abstract_declarator(specifiers.type);
3446 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3447 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3448 expression_t *left);
3450 typedef struct expression_parser_function_t expression_parser_function_t;
3451 struct expression_parser_function_t {
3452 unsigned precedence;
3453 parse_expression_function parser;
3454 unsigned infix_precedence;
3455 parse_expression_infix_function infix_parser;
3458 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3461 * Creates a new invalid expression.
3463 static expression_t *create_invalid_expression(void)
3465 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3466 expression->base.source_position = token.source_position;
3471 * Prints an error message if an expression was expected but not read
3473 static expression_t *expected_expression_error(void)
3475 /* skip the error message if the error token was read */
3476 if (token.type != T_ERROR) {
3477 errorf(HERE, "expected expression, got token '%K'", &token);
3481 return create_invalid_expression();
3485 * Parse a string constant.
3487 static expression_t *parse_string_const(void)
3490 if (token.type == T_STRING_LITERAL) {
3491 string_t res = token.v.string;
3493 while (token.type == T_STRING_LITERAL) {
3494 res = concat_strings(&res, &token.v.string);
3497 if (token.type != T_WIDE_STRING_LITERAL) {
3498 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3499 /* note: that we use type_char_ptr here, which is already the
3500 * automatic converted type. revert_automatic_type_conversion
3501 * will construct the array type */
3502 cnst->base.type = type_char_ptr;
3503 cnst->string.value = res;
3507 wres = concat_string_wide_string(&res, &token.v.wide_string);
3509 wres = token.v.wide_string;
3514 switch (token.type) {
3515 case T_WIDE_STRING_LITERAL:
3516 wres = concat_wide_strings(&wres, &token.v.wide_string);
3519 case T_STRING_LITERAL:
3520 wres = concat_wide_string_string(&wres, &token.v.string);
3524 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3525 cnst->base.type = type_wchar_t_ptr;
3526 cnst->wide_string.value = wres;
3535 * Parse an integer constant.
3537 static expression_t *parse_int_const(void)
3539 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3540 cnst->base.source_position = HERE;
3541 cnst->base.type = token.datatype;
3542 cnst->conste.v.int_value = token.v.intvalue;
3550 * Parse a character constant.
3552 static expression_t *parse_character_constant(void)
3554 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
3556 cnst->base.source_position = HERE;
3557 cnst->base.type = token.datatype;
3558 cnst->conste.v.character = token.v.string;
3560 if (cnst->conste.v.character.size != 1) {
3561 if (warning.multichar && (c_mode & _GNUC)) {
3563 warningf(HERE, "multi-character character constant");
3565 errorf(HERE, "more than 1 characters in character constant");
3574 * Parse a wide character constant.
3576 static expression_t *parse_wide_character_constant(void)
3578 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
3580 cnst->base.source_position = HERE;
3581 cnst->base.type = token.datatype;
3582 cnst->conste.v.wide_character = token.v.wide_string;
3584 if (cnst->conste.v.wide_character.size != 1) {
3585 if (warning.multichar && (c_mode & _GNUC)) {
3587 warningf(HERE, "multi-character character constant");
3589 errorf(HERE, "more than 1 characters in character constant");
3598 * Parse a float constant.
3600 static expression_t *parse_float_const(void)
3602 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3603 cnst->base.type = token.datatype;
3604 cnst->conste.v.float_value = token.v.floatvalue;
3611 static declaration_t *create_implicit_function(symbol_t *symbol,
3612 const source_position_t source_position)
3614 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3615 ntype->function.return_type = type_int;
3616 ntype->function.unspecified_parameters = true;
3618 type_t *type = typehash_insert(ntype);
3623 declaration_t *const declaration = allocate_declaration_zero();
3624 declaration->storage_class = STORAGE_CLASS_EXTERN;
3625 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
3626 declaration->type = type;
3627 declaration->symbol = symbol;
3628 declaration->source_position = source_position;
3629 declaration->parent_scope = global_scope;
3631 scope_t *old_scope = scope;
3632 set_scope(global_scope);
3634 environment_push(declaration);
3635 /* prepends the declaration to the global declarations list */
3636 declaration->next = scope->declarations;
3637 scope->declarations = declaration;
3639 assert(scope == global_scope);
3640 set_scope(old_scope);
3646 * Creates a return_type (func)(argument_type) function type if not
3649 * @param return_type the return type
3650 * @param argument_type the argument type
3652 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3654 function_parameter_t *parameter
3655 = obstack_alloc(type_obst, sizeof(parameter[0]));
3656 memset(parameter, 0, sizeof(parameter[0]));
3657 parameter->type = argument_type;
3659 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3660 type->function.return_type = return_type;
3661 type->function.parameters = parameter;
3663 type_t *result = typehash_insert(type);
3664 if(result != type) {
3672 * Creates a function type for some function like builtins.
3674 * @param symbol the symbol describing the builtin
3676 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3678 switch(symbol->ID) {
3679 case T___builtin_alloca:
3680 return make_function_1_type(type_void_ptr, type_size_t);
3681 case T___builtin_nan:
3682 return make_function_1_type(type_double, type_char_ptr);
3683 case T___builtin_nanf:
3684 return make_function_1_type(type_float, type_char_ptr);
3685 case T___builtin_nand:
3686 return make_function_1_type(type_long_double, type_char_ptr);
3687 case T___builtin_va_end:
3688 return make_function_1_type(type_void, type_valist);
3690 panic("not implemented builtin symbol found");
3695 * Performs automatic type cast as described in § 6.3.2.1.
3697 * @param orig_type the original type
3699 static type_t *automatic_type_conversion(type_t *orig_type)
3701 type_t *type = skip_typeref(orig_type);
3702 if(is_type_array(type)) {
3703 array_type_t *array_type = &type->array;
3704 type_t *element_type = array_type->element_type;
3705 unsigned qualifiers = array_type->type.qualifiers;
3707 return make_pointer_type(element_type, qualifiers);
3710 if(is_type_function(type)) {
3711 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3718 * reverts the automatic casts of array to pointer types and function
3719 * to function-pointer types as defined § 6.3.2.1
3721 type_t *revert_automatic_type_conversion(const expression_t *expression)
3723 switch (expression->kind) {
3724 case EXPR_REFERENCE: return expression->reference.declaration->type;
3725 case EXPR_SELECT: return expression->select.compound_entry->type;
3727 case EXPR_UNARY_DEREFERENCE: {
3728 const expression_t *const value = expression->unary.value;
3729 type_t *const type = skip_typeref(value->base.type);
3730 assert(is_type_pointer(type));
3731 return type->pointer.points_to;
3734 case EXPR_BUILTIN_SYMBOL:
3735 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3737 case EXPR_ARRAY_ACCESS: {
3738 const expression_t *array_ref = expression->array_access.array_ref;
3739 type_t *type_left = skip_typeref(array_ref->base.type);
3740 if (!is_type_valid(type_left))
3742 assert(is_type_pointer(type_left));
3743 return type_left->pointer.points_to;
3746 case EXPR_STRING_LITERAL: {
3747 size_t size = expression->string.value.size;
3748 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
3751 case EXPR_WIDE_STRING_LITERAL: {
3752 size_t size = expression->wide_string.value.size;
3753 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
3756 case EXPR_COMPOUND_LITERAL:
3757 return expression->compound_literal.type;
3762 return expression->base.type;
3765 static expression_t *parse_reference(void)
3767 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3769 reference_expression_t *ref = &expression->reference;
3770 ref->symbol = token.v.symbol;
3772 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3774 source_position_t source_position = token.source_position;
3777 if(declaration == NULL) {
3778 if (! strict_mode && token.type == '(') {
3779 /* an implicitly defined function */
3780 if (warning.implicit_function_declaration) {
3781 warningf(HERE, "implicit declaration of function '%Y'",
3785 declaration = create_implicit_function(ref->symbol,
3788 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3789 return create_invalid_expression();
3793 type_t *type = declaration->type;
3795 /* we always do the auto-type conversions; the & and sizeof parser contains
3796 * code to revert this! */
3797 type = automatic_type_conversion(type);
3799 ref->declaration = declaration;
3800 ref->base.type = type;
3802 /* this declaration is used */
3803 declaration->used = true;
3808 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3812 /* TODO check if explicit cast is allowed and issue warnings/errors */
3815 static expression_t *parse_compound_literal(type_t *type)
3817 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
3819 parse_initializer_env_t env;
3821 env.must_be_constant = false;
3822 parse_initializer(&env);
3825 expression->compound_literal.type = type;
3826 expression->compound_literal.initializer = env.initializer;
3827 expression->base.type = automatic_type_conversion(type);
3832 static expression_t *parse_cast(void)
3834 source_position_t source_position = token.source_position;
3836 type_t *type = parse_typename();
3840 if(token.type == '{') {
3841 return parse_compound_literal(type);
3844 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3845 cast->base.source_position = source_position;
3847 expression_t *value = parse_sub_expression(20);
3849 check_cast_allowed(value, type);
3851 cast->base.type = type;
3852 cast->unary.value = value;
3857 static expression_t *parse_statement_expression(void)
3859 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3861 statement_t *statement = parse_compound_statement();
3862 expression->statement.statement = statement;
3863 expression->base.source_position = statement->base.source_position;
3865 /* find last statement and use its type */
3866 type_t *type = type_void;
3867 const statement_t *stmt = statement->compound.statements;
3869 while (stmt->base.next != NULL)
3870 stmt = stmt->base.next;
3872 if (stmt->kind == STATEMENT_EXPRESSION) {
3873 type = stmt->expression.expression->base.type;
3876 warningf(expression->base.source_position, "empty statement expression ({})");
3878 expression->base.type = type;
3885 static expression_t *parse_brace_expression(void)
3889 switch(token.type) {
3891 /* gcc extension: a statement expression */
3892 return parse_statement_expression();
3896 return parse_cast();
3898 if(is_typedef_symbol(token.v.symbol)) {
3899 return parse_cast();
3903 expression_t *result = parse_expression();
3909 static expression_t *parse_function_keyword(void)
3914 if (current_function == NULL) {
3915 errorf(HERE, "'__func__' used outside of a function");
3918 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3919 expression->base.type = type_char_ptr;
3924 static expression_t *parse_pretty_function_keyword(void)
3926 eat(T___PRETTY_FUNCTION__);
3929 if (current_function == NULL) {
3930 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3933 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3934 expression->base.type = type_char_ptr;
3939 static designator_t *parse_designator(void)
3941 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3942 result->source_position = HERE;
3944 if(token.type != T_IDENTIFIER) {
3945 parse_error_expected("while parsing member designator",
3950 result->symbol = token.v.symbol;
3953 designator_t *last_designator = result;
3955 if(token.type == '.') {
3957 if(token.type != T_IDENTIFIER) {
3958 parse_error_expected("while parsing member designator",
3963 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3964 designator->source_position = HERE;
3965 designator->symbol = token.v.symbol;
3968 last_designator->next = designator;
3969 last_designator = designator;
3972 if(token.type == '[') {
3974 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3975 designator->source_position = HERE;
3976 designator->array_index = parse_expression();
3977 if(designator->array_index == NULL) {
3983 last_designator->next = designator;
3984 last_designator = designator;
3993 static expression_t *parse_offsetof(void)
3995 eat(T___builtin_offsetof);
3997 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3998 expression->base.type = type_size_t;
4001 type_t *type = parse_typename();
4003 designator_t *designator = parse_designator();
4006 expression->offsetofe.type = type;
4007 expression->offsetofe.designator = designator;
4010 memset(&path, 0, sizeof(path));
4011 path.top_type = type;
4012 path.path = NEW_ARR_F(type_path_entry_t, 0);
4014 descend_into_subtype(&path);
4016 if(!walk_designator(&path, designator, true)) {
4017 return create_invalid_expression();
4020 DEL_ARR_F(path.path);
4025 static expression_t *parse_va_start(void)
4027 eat(T___builtin_va_start);
4029 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4032 expression->va_starte.ap = parse_assignment_expression();
4034 expression_t *const expr = parse_assignment_expression();
4035 if (expr->kind == EXPR_REFERENCE) {
4036 declaration_t *const decl = expr->reference.declaration;
4038 return create_invalid_expression();
4039 if (decl->parent_scope == ¤t_function->scope &&
4040 decl->next == NULL) {
4041 expression->va_starte.parameter = decl;
4046 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4048 return create_invalid_expression();
4051 static expression_t *parse_va_arg(void)
4053 eat(T___builtin_va_arg);
4055 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4058 expression->va_arge.ap = parse_assignment_expression();
4060 expression->base.type = parse_typename();
4066 static expression_t *parse_builtin_symbol(void)
4068 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4070 symbol_t *symbol = token.v.symbol;
4072 expression->builtin_symbol.symbol = symbol;
4075 type_t *type = get_builtin_symbol_type(symbol);
4076 type = automatic_type_conversion(type);
4078 expression->base.type = type;
4082 static expression_t *parse_builtin_constant(void)
4084 eat(T___builtin_constant_p);
4086 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4089 expression->builtin_constant.value = parse_assignment_expression();
4091 expression->base.type = type_int;
4096 static expression_t *parse_builtin_prefetch(void)
4098 eat(T___builtin_prefetch);
4100 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4103 expression->builtin_prefetch.adr = parse_assignment_expression();
4104 if (token.type == ',') {
4106 expression->builtin_prefetch.rw = parse_assignment_expression();
4108 if (token.type == ',') {
4110 expression->builtin_prefetch.locality = parse_assignment_expression();
4113 expression->base.type = type_void;
4118 static expression_t *parse_compare_builtin(void)
4120 expression_t *expression;
4122 switch(token.type) {
4123 case T___builtin_isgreater:
4124 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4126 case T___builtin_isgreaterequal:
4127 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4129 case T___builtin_isless:
4130 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4132 case T___builtin_islessequal:
4133 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4135 case T___builtin_islessgreater:
4136 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4138 case T___builtin_isunordered:
4139 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4142 panic("invalid compare builtin found");
4145 expression->base.source_position = HERE;
4149 expression->binary.left = parse_assignment_expression();
4151 expression->binary.right = parse_assignment_expression();
4154 type_t *const orig_type_left = expression->binary.left->base.type;
4155 type_t *const orig_type_right = expression->binary.right->base.type;
4157 type_t *const type_left = skip_typeref(orig_type_left);
4158 type_t *const type_right = skip_typeref(orig_type_right);
4159 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4160 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4161 type_error_incompatible("invalid operands in comparison",
4162 expression->base.source_position, orig_type_left, orig_type_right);
4165 semantic_comparison(&expression->binary);
4171 static expression_t *parse_builtin_expect(void)
4173 eat(T___builtin_expect);
4175 expression_t *expression
4176 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4179 expression->binary.left = parse_assignment_expression();
4181 expression->binary.right = parse_constant_expression();
4184 expression->base.type = expression->binary.left->base.type;
4189 static expression_t *parse_assume(void) {
4192 expression_t *expression
4193 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4196 expression->unary.value = parse_assignment_expression();
4199 expression->base.type = type_void;
4203 static expression_t *parse_primary_expression(void)
4205 switch (token.type) {
4206 case T_INTEGER: return parse_int_const();
4207 case T_CHARACTER_CONSTANT: return parse_character_constant();
4208 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4209 case T_FLOATINGPOINT: return parse_float_const();
4210 case T_STRING_LITERAL:
4211 case T_WIDE_STRING_LITERAL: return parse_string_const();
4212 case T_IDENTIFIER: return parse_reference();
4213 case T___FUNCTION__:
4214 case T___func__: return parse_function_keyword();
4215 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4216 case T___builtin_offsetof: return parse_offsetof();
4217 case T___builtin_va_start: return parse_va_start();
4218 case T___builtin_va_arg: return parse_va_arg();
4219 case T___builtin_expect: return parse_builtin_expect();
4220 case T___builtin_alloca:
4221 case T___builtin_nan:
4222 case T___builtin_nand:
4223 case T___builtin_nanf:
4224 case T___builtin_va_end: return parse_builtin_symbol();
4225 case T___builtin_isgreater:
4226 case T___builtin_isgreaterequal:
4227 case T___builtin_isless:
4228 case T___builtin_islessequal:
4229 case T___builtin_islessgreater:
4230 case T___builtin_isunordered: return parse_compare_builtin();
4231 case T___builtin_constant_p: return parse_builtin_constant();
4232 case T___builtin_prefetch: return parse_builtin_prefetch();
4233 case T_assume: return parse_assume();
4235 case '(': return parse_brace_expression();
4238 errorf(HERE, "unexpected token %K, expected an expression", &token);
4241 return create_invalid_expression();
4245 * Check if the expression has the character type and issue a warning then.
4247 static void check_for_char_index_type(const expression_t *expression) {
4248 type_t *const type = expression->base.type;
4249 const type_t *const base_type = skip_typeref(type);
4251 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4252 warning.char_subscripts) {
4253 warningf(expression->base.source_position,
4254 "array subscript has type '%T'", type);
4258 static expression_t *parse_array_expression(unsigned precedence,
4265 expression_t *inside = parse_expression();
4267 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4269 array_access_expression_t *array_access = &expression->array_access;
4271 type_t *const orig_type_left = left->base.type;
4272 type_t *const orig_type_inside = inside->base.type;
4274 type_t *const type_left = skip_typeref(orig_type_left);
4275 type_t *const type_inside = skip_typeref(orig_type_inside);
4277 type_t *return_type;
4278 if (is_type_pointer(type_left)) {
4279 return_type = type_left->pointer.points_to;
4280 array_access->array_ref = left;
4281 array_access->index = inside;
4282 check_for_char_index_type(inside);
4283 } else if (is_type_pointer(type_inside)) {
4284 return_type = type_inside->pointer.points_to;
4285 array_access->array_ref = inside;
4286 array_access->index = left;
4287 array_access->flipped = true;
4288 check_for_char_index_type(left);
4290 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4292 "array access on object with non-pointer types '%T', '%T'",
4293 orig_type_left, orig_type_inside);
4295 return_type = type_error_type;
4296 array_access->array_ref = create_invalid_expression();
4299 if(token.type != ']') {
4300 parse_error_expected("Problem while parsing array access", ']', 0);
4305 return_type = automatic_type_conversion(return_type);
4306 expression->base.type = return_type;
4311 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4313 expression_t *tp_expression = allocate_expression_zero(kind);
4314 tp_expression->base.type = type_size_t;
4316 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4318 tp_expression->typeprop.type = parse_typename();
4321 expression_t *expression = parse_sub_expression(precedence);
4322 expression->base.type = revert_automatic_type_conversion(expression);
4324 tp_expression->typeprop.type = expression->base.type;
4325 tp_expression->typeprop.tp_expression = expression;
4328 return tp_expression;
4331 static expression_t *parse_sizeof(unsigned precedence)
4334 return parse_typeprop(EXPR_SIZEOF, precedence);
4337 static expression_t *parse_alignof(unsigned precedence)
4340 return parse_typeprop(EXPR_SIZEOF, precedence);
4343 static expression_t *parse_select_expression(unsigned precedence,
4344 expression_t *compound)
4347 assert(token.type == '.' || token.type == T_MINUSGREATER);
4349 bool is_pointer = (token.type == T_MINUSGREATER);
4352 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4353 select->select.compound = compound;
4355 if(token.type != T_IDENTIFIER) {
4356 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4359 symbol_t *symbol = token.v.symbol;
4360 select->select.symbol = symbol;
4363 type_t *const orig_type = compound->base.type;
4364 type_t *const type = skip_typeref(orig_type);
4366 type_t *type_left = type;
4368 if (!is_type_pointer(type)) {
4369 if (is_type_valid(type)) {
4370 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4372 return create_invalid_expression();
4374 type_left = type->pointer.points_to;
4376 type_left = skip_typeref(type_left);
4378 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4379 type_left->kind != TYPE_COMPOUND_UNION) {
4380 if (is_type_valid(type_left)) {
4381 errorf(HERE, "request for member '%Y' in something not a struct or "
4382 "union, but '%T'", symbol, type_left);
4384 return create_invalid_expression();
4387 declaration_t *const declaration = type_left->compound.declaration;
4389 if(!declaration->init.is_defined) {
4390 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4392 return create_invalid_expression();
4395 declaration_t *iter = find_compound_entry(declaration, symbol);
4397 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4398 return create_invalid_expression();
4401 /* we always do the auto-type conversions; the & and sizeof parser contains
4402 * code to revert this! */
4403 type_t *expression_type = automatic_type_conversion(iter->type);
4405 select->select.compound_entry = iter;
4406 select->base.type = expression_type;
4408 if(expression_type->kind == TYPE_BITFIELD) {
4409 expression_t *extract
4410 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4411 extract->unary.value = select;
4412 extract->base.type = expression_type->bitfield.base;
4421 * Parse a call expression, ie. expression '( ... )'.
4423 * @param expression the function address
4425 static expression_t *parse_call_expression(unsigned precedence,
4426 expression_t *expression)
4429 expression_t *result = allocate_expression_zero(EXPR_CALL);
4431 call_expression_t *call = &result->call;
4432 call->function = expression;
4434 type_t *const orig_type = expression->base.type;
4435 type_t *const type = skip_typeref(orig_type);
4437 function_type_t *function_type = NULL;
4438 if (is_type_pointer(type)) {
4439 type_t *const to_type = skip_typeref(type->pointer.points_to);
4441 if (is_type_function(to_type)) {
4442 function_type = &to_type->function;
4443 call->base.type = function_type->return_type;
4447 if (function_type == NULL && is_type_valid(type)) {
4448 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4451 /* parse arguments */
4454 if(token.type != ')') {
4455 call_argument_t *last_argument = NULL;
4458 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4460 argument->expression = parse_assignment_expression();
4461 if(last_argument == NULL) {
4462 call->arguments = argument;
4464 last_argument->next = argument;
4466 last_argument = argument;
4468 if(token.type != ',')
4475 if(function_type != NULL) {
4476 function_parameter_t *parameter = function_type->parameters;
4477 call_argument_t *argument = call->arguments;
4478 for( ; parameter != NULL && argument != NULL;
4479 parameter = parameter->next, argument = argument->next) {
4480 type_t *expected_type = parameter->type;
4481 /* TODO report scope in error messages */
4482 expression_t *const arg_expr = argument->expression;
4483 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4484 if (res_type == NULL) {
4485 /* TODO improve error message */
4486 errorf(arg_expr->base.source_position,
4487 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4488 arg_expr, arg_expr->base.type, expected_type);
4490 argument->expression = create_implicit_cast(argument->expression, expected_type);
4493 /* too few parameters */
4494 if(parameter != NULL) {
4495 errorf(HERE, "too few arguments to function '%E'", expression);
4496 } else if(argument != NULL) {
4497 /* too many parameters */
4498 if(!function_type->variadic
4499 && !function_type->unspecified_parameters) {
4500 errorf(HERE, "too many arguments to function '%E'", expression);
4502 /* do default promotion */
4503 for( ; argument != NULL; argument = argument->next) {
4504 type_t *type = argument->expression->base.type;
4506 type = skip_typeref(type);
4507 if(is_type_integer(type)) {
4508 type = promote_integer(type);
4509 } else if(type == type_float) {
4513 argument->expression
4514 = create_implicit_cast(argument->expression, type);
4517 check_format(&result->call);
4520 check_format(&result->call);
4527 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4529 static bool same_compound_type(const type_t *type1, const type_t *type2)
4532 is_type_compound(type1) &&
4533 type1->kind == type2->kind &&
4534 type1->compound.declaration == type2->compound.declaration;
4538 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4540 * @param expression the conditional expression
4542 static expression_t *parse_conditional_expression(unsigned precedence,
4543 expression_t *expression)
4547 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4549 conditional_expression_t *conditional = &result->conditional;
4550 conditional->condition = expression;
4553 type_t *const condition_type_orig = expression->base.type;
4554 type_t *const condition_type = skip_typeref(condition_type_orig);
4555 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4556 type_error("expected a scalar type in conditional condition",
4557 expression->base.source_position, condition_type_orig);
4560 expression_t *true_expression = parse_expression();
4562 expression_t *false_expression = parse_sub_expression(precedence);
4564 type_t *const orig_true_type = true_expression->base.type;
4565 type_t *const orig_false_type = false_expression->base.type;
4566 type_t *const true_type = skip_typeref(orig_true_type);
4567 type_t *const false_type = skip_typeref(orig_false_type);
4570 type_t *result_type;
4571 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4572 result_type = semantic_arithmetic(true_type, false_type);
4574 true_expression = create_implicit_cast(true_expression, result_type);
4575 false_expression = create_implicit_cast(false_expression, result_type);
4577 conditional->true_expression = true_expression;
4578 conditional->false_expression = false_expression;
4579 conditional->base.type = result_type;
4580 } else if (same_compound_type(true_type, false_type) || (
4581 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4582 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4584 /* just take 1 of the 2 types */
4585 result_type = true_type;
4586 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4587 && pointers_compatible(true_type, false_type)) {
4589 result_type = true_type;
4590 } else if (is_type_pointer(true_type)
4591 && is_null_pointer_constant(false_expression)) {
4592 result_type = true_type;
4593 } else if (is_type_pointer(false_type)
4594 && is_null_pointer_constant(true_expression)) {
4595 result_type = false_type;
4597 /* TODO: one pointer to void*, other some pointer */
4599 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4600 type_error_incompatible("while parsing conditional",
4601 expression->base.source_position, true_type,
4604 result_type = type_error_type;
4607 conditional->true_expression
4608 = create_implicit_cast(true_expression, result_type);
4609 conditional->false_expression
4610 = create_implicit_cast(false_expression, result_type);
4611 conditional->base.type = result_type;
4616 * Parse an extension expression.
4618 static expression_t *parse_extension(unsigned precedence)
4620 eat(T___extension__);
4622 /* TODO enable extensions */
4623 expression_t *expression = parse_sub_expression(precedence);
4624 /* TODO disable extensions */
4628 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4630 eat(T___builtin_classify_type);
4632 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4633 result->base.type = type_int;
4636 expression_t *expression = parse_sub_expression(precedence);
4638 result->classify_type.type_expression = expression;
4643 static void semantic_incdec(unary_expression_t *expression)
4645 type_t *const orig_type = expression->value->base.type;
4646 type_t *const type = skip_typeref(orig_type);
4647 /* TODO !is_type_real && !is_type_pointer */
4648 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4649 if (is_type_valid(type)) {
4650 /* TODO: improve error message */
4651 errorf(HERE, "operation needs an arithmetic or pointer type");
4656 expression->base.type = orig_type;
4659 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4661 type_t *const orig_type = expression->value->base.type;
4662 type_t *const type = skip_typeref(orig_type);
4663 if(!is_type_arithmetic(type)) {
4664 if (is_type_valid(type)) {
4665 /* TODO: improve error message */
4666 errorf(HERE, "operation needs an arithmetic type");
4671 expression->base.type = orig_type;
4674 static void semantic_unexpr_scalar(unary_expression_t *expression)
4676 type_t *const orig_type = expression->value->base.type;
4677 type_t *const type = skip_typeref(orig_type);
4678 if (!is_type_scalar(type)) {
4679 if (is_type_valid(type)) {
4680 errorf(HERE, "operand of ! must be of scalar type");
4685 expression->base.type = orig_type;
4688 static void semantic_unexpr_integer(unary_expression_t *expression)
4690 type_t *const orig_type = expression->value->base.type;
4691 type_t *const type = skip_typeref(orig_type);
4692 if (!is_type_integer(type)) {
4693 if (is_type_valid(type)) {
4694 errorf(HERE, "operand of ~ must be of integer type");
4699 expression->base.type = orig_type;
4702 static void semantic_dereference(unary_expression_t *expression)
4704 type_t *const orig_type = expression->value->base.type;
4705 type_t *const type = skip_typeref(orig_type);
4706 if(!is_type_pointer(type)) {
4707 if (is_type_valid(type)) {
4708 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4713 type_t *result_type = type->pointer.points_to;
4714 result_type = automatic_type_conversion(result_type);
4715 expression->base.type = result_type;
4719 * Check the semantic of the address taken expression.
4721 static void semantic_take_addr(unary_expression_t *expression)
4723 expression_t *value = expression->value;
4724 value->base.type = revert_automatic_type_conversion(value);
4726 type_t *orig_type = value->base.type;
4727 if(!is_type_valid(orig_type))
4730 if(value->kind == EXPR_REFERENCE) {
4731 declaration_t *const declaration = value->reference.declaration;
4732 if(declaration != NULL) {
4733 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4734 errorf(expression->base.source_position,
4735 "address of register variable '%Y' requested",
4736 declaration->symbol);
4738 declaration->address_taken = 1;
4742 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4745 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4746 static expression_t *parse_##unexpression_type(unsigned precedence) \
4750 expression_t *unary_expression \
4751 = allocate_expression_zero(unexpression_type); \
4752 unary_expression->base.source_position = HERE; \
4753 unary_expression->unary.value = parse_sub_expression(precedence); \
4755 sfunc(&unary_expression->unary); \
4757 return unary_expression; \
4760 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4761 semantic_unexpr_arithmetic)
4762 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4763 semantic_unexpr_arithmetic)
4764 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4765 semantic_unexpr_scalar)
4766 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4767 semantic_dereference)
4768 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4770 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4771 semantic_unexpr_integer)
4772 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4774 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4777 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4779 static expression_t *parse_##unexpression_type(unsigned precedence, \
4780 expression_t *left) \
4782 (void) precedence; \
4785 expression_t *unary_expression \
4786 = allocate_expression_zero(unexpression_type); \
4787 unary_expression->unary.value = left; \
4789 sfunc(&unary_expression->unary); \
4791 return unary_expression; \
4794 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4795 EXPR_UNARY_POSTFIX_INCREMENT,
4797 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4798 EXPR_UNARY_POSTFIX_DECREMENT,
4801 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4803 /* TODO: handle complex + imaginary types */
4805 /* § 6.3.1.8 Usual arithmetic conversions */
4806 if(type_left == type_long_double || type_right == type_long_double) {
4807 return type_long_double;
4808 } else if(type_left == type_double || type_right == type_double) {
4810 } else if(type_left == type_float || type_right == type_float) {
4814 type_right = promote_integer(type_right);
4815 type_left = promote_integer(type_left);
4817 if(type_left == type_right)
4820 bool signed_left = is_type_signed(type_left);
4821 bool signed_right = is_type_signed(type_right);
4822 int rank_left = get_rank(type_left);
4823 int rank_right = get_rank(type_right);
4824 if(rank_left < rank_right) {
4825 if(signed_left == signed_right || !signed_right) {
4831 if(signed_left == signed_right || !signed_left) {
4840 * Check the semantic restrictions for a binary expression.
4842 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4844 expression_t *const left = expression->left;
4845 expression_t *const right = expression->right;
4846 type_t *const orig_type_left = left->base.type;
4847 type_t *const orig_type_right = right->base.type;
4848 type_t *const type_left = skip_typeref(orig_type_left);
4849 type_t *const type_right = skip_typeref(orig_type_right);
4851 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4852 /* TODO: improve error message */
4853 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4854 errorf(HERE, "operation needs arithmetic types");
4859 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4860 expression->left = create_implicit_cast(left, arithmetic_type);
4861 expression->right = create_implicit_cast(right, arithmetic_type);
4862 expression->base.type = arithmetic_type;
4865 static void semantic_shift_op(binary_expression_t *expression)
4867 expression_t *const left = expression->left;
4868 expression_t *const right = expression->right;
4869 type_t *const orig_type_left = left->base.type;
4870 type_t *const orig_type_right = right->base.type;
4871 type_t * type_left = skip_typeref(orig_type_left);
4872 type_t * type_right = skip_typeref(orig_type_right);
4874 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4875 /* TODO: improve error message */
4876 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4877 errorf(HERE, "operation needs integer types");
4882 type_left = promote_integer(type_left);
4883 type_right = promote_integer(type_right);
4885 expression->left = create_implicit_cast(left, type_left);
4886 expression->right = create_implicit_cast(right, type_right);
4887 expression->base.type = type_left;
4890 static void semantic_add(binary_expression_t *expression)
4892 expression_t *const left = expression->left;
4893 expression_t *const right = expression->right;
4894 type_t *const orig_type_left = left->base.type;
4895 type_t *const orig_type_right = right->base.type;
4896 type_t *const type_left = skip_typeref(orig_type_left);
4897 type_t *const type_right = skip_typeref(orig_type_right);
4900 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4901 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4902 expression->left = create_implicit_cast(left, arithmetic_type);
4903 expression->right = create_implicit_cast(right, arithmetic_type);
4904 expression->base.type = arithmetic_type;
4906 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4907 expression->base.type = type_left;
4908 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4909 expression->base.type = type_right;
4910 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4911 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4915 static void semantic_sub(binary_expression_t *expression)
4917 expression_t *const left = expression->left;
4918 expression_t *const right = expression->right;
4919 type_t *const orig_type_left = left->base.type;
4920 type_t *const orig_type_right = right->base.type;
4921 type_t *const type_left = skip_typeref(orig_type_left);
4922 type_t *const type_right = skip_typeref(orig_type_right);
4925 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4926 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4927 expression->left = create_implicit_cast(left, arithmetic_type);
4928 expression->right = create_implicit_cast(right, arithmetic_type);
4929 expression->base.type = arithmetic_type;
4931 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4932 expression->base.type = type_left;
4933 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4934 if(!pointers_compatible(type_left, type_right)) {
4936 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4937 orig_type_left, orig_type_right);
4939 expression->base.type = type_ptrdiff_t;
4941 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4942 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4943 orig_type_left, orig_type_right);
4948 * Check the semantics of comparison expressions.
4950 * @param expression The expression to check.
4952 static void semantic_comparison(binary_expression_t *expression)
4954 expression_t *left = expression->left;
4955 expression_t *right = expression->right;
4956 type_t *orig_type_left = left->base.type;
4957 type_t *orig_type_right = right->base.type;
4959 type_t *type_left = skip_typeref(orig_type_left);
4960 type_t *type_right = skip_typeref(orig_type_right);
4962 /* TODO non-arithmetic types */
4963 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4964 if (warning.sign_compare &&
4965 (expression->base.kind != EXPR_BINARY_EQUAL &&
4966 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4967 (is_type_signed(type_left) != is_type_signed(type_right))) {
4968 warningf(expression->base.source_position,
4969 "comparison between signed and unsigned");
4971 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4972 expression->left = create_implicit_cast(left, arithmetic_type);
4973 expression->right = create_implicit_cast(right, arithmetic_type);
4974 expression->base.type = arithmetic_type;
4975 if (warning.float_equal &&
4976 (expression->base.kind == EXPR_BINARY_EQUAL ||
4977 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4978 is_type_float(arithmetic_type)) {
4979 warningf(expression->base.source_position,
4980 "comparing floating point with == or != is unsafe");
4982 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4983 /* TODO check compatibility */
4984 } else if (is_type_pointer(type_left)) {
4985 expression->right = create_implicit_cast(right, type_left);
4986 } else if (is_type_pointer(type_right)) {
4987 expression->left = create_implicit_cast(left, type_right);
4988 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4989 type_error_incompatible("invalid operands in comparison",
4990 expression->base.source_position,
4991 type_left, type_right);
4993 expression->base.type = type_int;
4996 static void semantic_arithmetic_assign(binary_expression_t *expression)
4998 expression_t *left = expression->left;
4999 expression_t *right = expression->right;
5000 type_t *orig_type_left = left->base.type;
5001 type_t *orig_type_right = right->base.type;
5003 type_t *type_left = skip_typeref(orig_type_left);
5004 type_t *type_right = skip_typeref(orig_type_right);
5006 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5007 /* TODO: improve error message */
5008 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5009 errorf(HERE, "operation needs arithmetic types");
5014 /* combined instructions are tricky. We can't create an implicit cast on
5015 * the left side, because we need the uncasted form for the store.
5016 * The ast2firm pass has to know that left_type must be right_type
5017 * for the arithmetic operation and create a cast by itself */
5018 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5019 expression->right = create_implicit_cast(right, arithmetic_type);
5020 expression->base.type = type_left;
5023 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5025 expression_t *const left = expression->left;
5026 expression_t *const right = expression->right;
5027 type_t *const orig_type_left = left->base.type;
5028 type_t *const orig_type_right = right->base.type;
5029 type_t *const type_left = skip_typeref(orig_type_left);
5030 type_t *const type_right = skip_typeref(orig_type_right);
5032 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5033 /* combined instructions are tricky. We can't create an implicit cast on
5034 * the left side, because we need the uncasted form for the store.
5035 * The ast2firm pass has to know that left_type must be right_type
5036 * for the arithmetic operation and create a cast by itself */
5037 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5038 expression->right = create_implicit_cast(right, arithmetic_type);
5039 expression->base.type = type_left;
5040 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5041 expression->base.type = type_left;
5042 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5043 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5048 * Check the semantic restrictions of a logical expression.
5050 static void semantic_logical_op(binary_expression_t *expression)
5052 expression_t *const left = expression->left;
5053 expression_t *const right = expression->right;
5054 type_t *const orig_type_left = left->base.type;
5055 type_t *const orig_type_right = right->base.type;
5056 type_t *const type_left = skip_typeref(orig_type_left);
5057 type_t *const type_right = skip_typeref(orig_type_right);
5059 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5060 /* TODO: improve error message */
5061 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5062 errorf(HERE, "operation needs scalar types");
5067 expression->base.type = type_int;
5071 * Checks if a compound type has constant fields.
5073 static bool has_const_fields(const compound_type_t *type)
5075 const scope_t *scope = &type->declaration->scope;
5076 const declaration_t *declaration = scope->declarations;
5078 for (; declaration != NULL; declaration = declaration->next) {
5079 if (declaration->namespc != NAMESPACE_NORMAL)
5082 const type_t *decl_type = skip_typeref(declaration->type);
5083 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5091 * Check the semantic restrictions of a binary assign expression.
5093 static void semantic_binexpr_assign(binary_expression_t *expression)
5095 expression_t *left = expression->left;
5096 type_t *orig_type_left = left->base.type;
5098 type_t *type_left = revert_automatic_type_conversion(left);
5099 type_left = skip_typeref(orig_type_left);
5101 /* must be a modifiable lvalue */
5102 if (is_type_array(type_left)) {
5103 errorf(HERE, "cannot assign to arrays ('%E')", left);
5106 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5107 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5111 if(is_type_incomplete(type_left)) {
5113 "left-hand side of assignment '%E' has incomplete type '%T'",
5114 left, orig_type_left);
5117 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5118 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5119 left, orig_type_left);
5123 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5125 if (res_type == NULL) {
5126 errorf(expression->base.source_position,
5127 "cannot assign to '%T' from '%T'",
5128 orig_type_left, expression->right->base.type);
5130 expression->right = create_implicit_cast(expression->right, res_type);
5133 expression->base.type = orig_type_left;
5137 * Determine if the outermost operation (or parts thereof) of the given
5138 * expression has no effect in order to generate a warning about this fact.
5139 * Therefore in some cases this only examines some of the operands of the
5140 * expression (see comments in the function and examples below).
5142 * f() + 23; // warning, because + has no effect
5143 * x || f(); // no warning, because x controls execution of f()
5144 * x ? y : f(); // warning, because y has no effect
5145 * (void)x; // no warning to be able to suppress the warning
5146 * This function can NOT be used for an "expression has definitely no effect"-
5148 static bool expression_has_effect(const expression_t *const expr)
5150 switch (expr->kind) {
5151 case EXPR_UNKNOWN: break;
5152 case EXPR_INVALID: break;
5153 case EXPR_REFERENCE: return false;
5154 case EXPR_CONST: return false;
5155 case EXPR_CHARACTER_CONSTANT: return false;
5156 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5157 case EXPR_STRING_LITERAL: return false;
5158 case EXPR_WIDE_STRING_LITERAL: return false;
5161 const call_expression_t *const call = &expr->call;
5162 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5165 switch (call->function->builtin_symbol.symbol->ID) {
5166 case T___builtin_va_end: return true;
5167 default: return false;
5171 /* Generate the warning if either the left or right hand side of a
5172 * conditional expression has no effect */
5173 case EXPR_CONDITIONAL: {
5174 const conditional_expression_t *const cond = &expr->conditional;
5176 expression_has_effect(cond->true_expression) &&
5177 expression_has_effect(cond->false_expression);
5180 case EXPR_SELECT: return false;
5181 case EXPR_ARRAY_ACCESS: return false;
5182 case EXPR_SIZEOF: return false;
5183 case EXPR_CLASSIFY_TYPE: return false;
5184 case EXPR_ALIGNOF: return false;
5186 case EXPR_FUNCTION: return false;
5187 case EXPR_PRETTY_FUNCTION: return false;
5188 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5189 case EXPR_BUILTIN_CONSTANT_P: return false;
5190 case EXPR_BUILTIN_PREFETCH: return true;
5191 case EXPR_OFFSETOF: return false;
5192 case EXPR_VA_START: return true;
5193 case EXPR_VA_ARG: return true;
5194 case EXPR_STATEMENT: return true; // TODO
5195 case EXPR_COMPOUND_LITERAL: return false;
5197 case EXPR_UNARY_NEGATE: return false;
5198 case EXPR_UNARY_PLUS: return false;
5199 case EXPR_UNARY_BITWISE_NEGATE: return false;
5200 case EXPR_UNARY_NOT: return false;
5201 case EXPR_UNARY_DEREFERENCE: return false;
5202 case EXPR_UNARY_TAKE_ADDRESS: return false;
5203 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5204 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5205 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5206 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5208 /* Treat void casts as if they have an effect in order to being able to
5209 * suppress the warning */
5210 case EXPR_UNARY_CAST: {
5211 type_t *const type = skip_typeref(expr->base.type);
5212 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5215 case EXPR_UNARY_CAST_IMPLICIT: return true;
5216 case EXPR_UNARY_ASSUME: return true;
5217 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5219 case EXPR_BINARY_ADD: return false;
5220 case EXPR_BINARY_SUB: return false;
5221 case EXPR_BINARY_MUL: return false;
5222 case EXPR_BINARY_DIV: return false;
5223 case EXPR_BINARY_MOD: return false;
5224 case EXPR_BINARY_EQUAL: return false;
5225 case EXPR_BINARY_NOTEQUAL: return false;
5226 case EXPR_BINARY_LESS: return false;
5227 case EXPR_BINARY_LESSEQUAL: return false;
5228 case EXPR_BINARY_GREATER: return false;
5229 case EXPR_BINARY_GREATEREQUAL: return false;
5230 case EXPR_BINARY_BITWISE_AND: return false;
5231 case EXPR_BINARY_BITWISE_OR: return false;
5232 case EXPR_BINARY_BITWISE_XOR: return false;
5233 case EXPR_BINARY_SHIFTLEFT: return false;
5234 case EXPR_BINARY_SHIFTRIGHT: return false;
5235 case EXPR_BINARY_ASSIGN: return true;
5236 case EXPR_BINARY_MUL_ASSIGN: return true;
5237 case EXPR_BINARY_DIV_ASSIGN: return true;
5238 case EXPR_BINARY_MOD_ASSIGN: return true;
5239 case EXPR_BINARY_ADD_ASSIGN: return true;
5240 case EXPR_BINARY_SUB_ASSIGN: return true;
5241 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5242 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5243 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5244 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5245 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5247 /* Only examine the right hand side of && and ||, because the left hand
5248 * side already has the effect of controlling the execution of the right
5250 case EXPR_BINARY_LOGICAL_AND:
5251 case EXPR_BINARY_LOGICAL_OR:
5252 /* Only examine the right hand side of a comma expression, because the left
5253 * hand side has a separate warning */
5254 case EXPR_BINARY_COMMA:
5255 return expression_has_effect(expr->binary.right);
5257 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5258 case EXPR_BINARY_ISGREATER: return false;
5259 case EXPR_BINARY_ISGREATEREQUAL: return false;
5260 case EXPR_BINARY_ISLESS: return false;
5261 case EXPR_BINARY_ISLESSEQUAL: return false;
5262 case EXPR_BINARY_ISLESSGREATER: return false;
5263 case EXPR_BINARY_ISUNORDERED: return false;
5266 panic("unexpected statement");
5269 static void semantic_comma(binary_expression_t *expression)
5271 if (warning.unused_value) {
5272 const expression_t *const left = expression->left;
5273 if (!expression_has_effect(left)) {
5274 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5277 expression->base.type = expression->right->base.type;
5280 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5281 static expression_t *parse_##binexpression_type(unsigned precedence, \
5282 expression_t *left) \
5285 source_position_t pos = HERE; \
5287 expression_t *right = parse_sub_expression(precedence + lr); \
5289 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5290 binexpr->base.source_position = pos; \
5291 binexpr->binary.left = left; \
5292 binexpr->binary.right = right; \
5293 sfunc(&binexpr->binary); \
5298 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5299 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5300 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5301 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5302 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5303 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5304 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5305 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5306 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5308 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5309 semantic_comparison, 1)
5310 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5311 semantic_comparison, 1)
5312 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5313 semantic_comparison, 1)
5314 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5315 semantic_comparison, 1)
5317 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5318 semantic_binexpr_arithmetic, 1)
5319 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5320 semantic_binexpr_arithmetic, 1)
5321 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5322 semantic_binexpr_arithmetic, 1)
5323 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5324 semantic_logical_op, 1)
5325 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5326 semantic_logical_op, 1)
5327 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5328 semantic_shift_op, 1)
5329 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5330 semantic_shift_op, 1)
5331 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5332 semantic_arithmetic_addsubb_assign, 0)
5333 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5334 semantic_arithmetic_addsubb_assign, 0)
5335 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5336 semantic_arithmetic_assign, 0)
5337 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5338 semantic_arithmetic_assign, 0)
5339 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5340 semantic_arithmetic_assign, 0)
5341 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5342 semantic_arithmetic_assign, 0)
5343 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5344 semantic_arithmetic_assign, 0)
5345 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5346 semantic_arithmetic_assign, 0)
5347 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5348 semantic_arithmetic_assign, 0)
5349 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5350 semantic_arithmetic_assign, 0)
5352 static expression_t *parse_sub_expression(unsigned precedence)
5354 if(token.type < 0) {
5355 return expected_expression_error();
5358 expression_parser_function_t *parser
5359 = &expression_parsers[token.type];
5360 source_position_t source_position = token.source_position;
5363 if(parser->parser != NULL) {
5364 left = parser->parser(parser->precedence);
5366 left = parse_primary_expression();
5368 assert(left != NULL);
5369 left->base.source_position = source_position;
5372 if(token.type < 0) {
5373 return expected_expression_error();
5376 parser = &expression_parsers[token.type];
5377 if(parser->infix_parser == NULL)
5379 if(parser->infix_precedence < precedence)
5382 left = parser->infix_parser(parser->infix_precedence, left);
5384 assert(left != NULL);
5385 assert(left->kind != EXPR_UNKNOWN);
5386 left->base.source_position = source_position;
5393 * Parse an expression.
5395 static expression_t *parse_expression(void)
5397 return parse_sub_expression(1);
5401 * Register a parser for a prefix-like operator with given precedence.
5403 * @param parser the parser function
5404 * @param token_type the token type of the prefix token
5405 * @param precedence the precedence of the operator
5407 static void register_expression_parser(parse_expression_function parser,
5408 int token_type, unsigned precedence)
5410 expression_parser_function_t *entry = &expression_parsers[token_type];
5412 if(entry->parser != NULL) {
5413 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5414 panic("trying to register multiple expression parsers for a token");
5416 entry->parser = parser;
5417 entry->precedence = precedence;
5421 * Register a parser for an infix operator with given precedence.
5423 * @param parser the parser function
5424 * @param token_type the token type of the infix operator
5425 * @param precedence the precedence of the operator
5427 static void register_infix_parser(parse_expression_infix_function parser,
5428 int token_type, unsigned precedence)
5430 expression_parser_function_t *entry = &expression_parsers[token_type];
5432 if(entry->infix_parser != NULL) {
5433 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5434 panic("trying to register multiple infix expression parsers for a "
5437 entry->infix_parser = parser;
5438 entry->infix_precedence = precedence;
5442 * Initialize the expression parsers.
5444 static void init_expression_parsers(void)
5446 memset(&expression_parsers, 0, sizeof(expression_parsers));
5448 register_infix_parser(parse_array_expression, '[', 30);
5449 register_infix_parser(parse_call_expression, '(', 30);
5450 register_infix_parser(parse_select_expression, '.', 30);
5451 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5452 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5454 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5457 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5458 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5459 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5460 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5461 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5462 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5463 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5464 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5465 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5466 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5467 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5468 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5469 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5470 T_EXCLAMATIONMARKEQUAL, 13);
5471 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5472 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5473 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5474 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5475 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5476 register_infix_parser(parse_conditional_expression, '?', 7);
5477 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5478 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5479 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5480 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5481 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5482 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5483 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5484 T_LESSLESSEQUAL, 2);
5485 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5486 T_GREATERGREATEREQUAL, 2);
5487 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5489 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5491 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5494 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5496 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5497 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5498 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5499 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5500 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5501 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5502 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5504 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5506 register_expression_parser(parse_sizeof, T_sizeof, 25);
5507 register_expression_parser(parse_alignof, T___alignof__, 25);
5508 register_expression_parser(parse_extension, T___extension__, 25);
5509 register_expression_parser(parse_builtin_classify_type,
5510 T___builtin_classify_type, 25);
5514 * Parse a asm statement constraints specification.
5516 static asm_constraint_t *parse_asm_constraints(void)
5518 asm_constraint_t *result = NULL;
5519 asm_constraint_t *last = NULL;
5521 while(token.type == T_STRING_LITERAL || token.type == '[') {
5522 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5523 memset(constraint, 0, sizeof(constraint[0]));
5525 if(token.type == '[') {
5527 if(token.type != T_IDENTIFIER) {
5528 parse_error_expected("while parsing asm constraint",
5532 constraint->symbol = token.v.symbol;
5537 constraint->constraints = parse_string_literals();
5539 constraint->expression = parse_expression();
5543 last->next = constraint;
5545 result = constraint;
5549 if(token.type != ',')
5558 * Parse a asm statement clobber specification.
5560 static asm_clobber_t *parse_asm_clobbers(void)
5562 asm_clobber_t *result = NULL;
5563 asm_clobber_t *last = NULL;
5565 while(token.type == T_STRING_LITERAL) {
5566 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5567 clobber->clobber = parse_string_literals();
5570 last->next = clobber;
5576 if(token.type != ',')
5585 * Parse an asm statement.
5587 static statement_t *parse_asm_statement(void)
5591 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5592 statement->base.source_position = token.source_position;
5594 asm_statement_t *asm_statement = &statement->asms;
5596 if(token.type == T_volatile) {
5598 asm_statement->is_volatile = true;
5602 asm_statement->asm_text = parse_string_literals();
5604 if(token.type != ':')
5608 asm_statement->inputs = parse_asm_constraints();
5609 if(token.type != ':')
5613 asm_statement->outputs = parse_asm_constraints();
5614 if(token.type != ':')
5618 asm_statement->clobbers = parse_asm_clobbers();
5627 * Parse a case statement.
5629 static statement_t *parse_case_statement(void)
5633 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5635 statement->base.source_position = token.source_position;
5636 statement->case_label.expression = parse_expression();
5638 if (c_mode & _GNUC) {
5639 if (token.type == T_DOTDOTDOT) {
5641 statement->case_label.end_range = parse_expression();
5647 if (! is_constant_expression(statement->case_label.expression)) {
5648 errorf(statement->base.source_position,
5649 "case label does not reduce to an integer constant");
5651 /* TODO: check if the case label is already known */
5652 if (current_switch != NULL) {
5653 /* link all cases into the switch statement */
5654 if (current_switch->last_case == NULL) {
5655 current_switch->first_case =
5656 current_switch->last_case = &statement->case_label;
5658 current_switch->last_case->next = &statement->case_label;
5661 errorf(statement->base.source_position,
5662 "case label not within a switch statement");
5665 statement->case_label.statement = parse_statement();
5671 * Finds an existing default label of a switch statement.
5673 static case_label_statement_t *
5674 find_default_label(const switch_statement_t *statement)
5676 case_label_statement_t *label = statement->first_case;
5677 for ( ; label != NULL; label = label->next) {
5678 if (label->expression == NULL)
5685 * Parse a default statement.
5687 static statement_t *parse_default_statement(void)
5691 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5693 statement->base.source_position = token.source_position;
5696 if (current_switch != NULL) {
5697 const case_label_statement_t *def_label = find_default_label(current_switch);
5698 if (def_label != NULL) {
5699 errorf(HERE, "multiple default labels in one switch");
5700 errorf(def_label->base.source_position,
5701 "this is the first default label");
5703 /* link all cases into the switch statement */
5704 if (current_switch->last_case == NULL) {
5705 current_switch->first_case =
5706 current_switch->last_case = &statement->case_label;
5708 current_switch->last_case->next = &statement->case_label;
5712 errorf(statement->base.source_position,
5713 "'default' label not within a switch statement");
5715 statement->case_label.statement = parse_statement();
5721 * Return the declaration for a given label symbol or create a new one.
5723 static declaration_t *get_label(symbol_t *symbol)
5725 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5726 assert(current_function != NULL);
5727 /* if we found a label in the same function, then we already created the
5729 if(candidate != NULL
5730 && candidate->parent_scope == ¤t_function->scope) {
5734 /* otherwise we need to create a new one */
5735 declaration_t *const declaration = allocate_declaration_zero();
5736 declaration->namespc = NAMESPACE_LABEL;
5737 declaration->symbol = symbol;
5739 label_push(declaration);
5745 * Parse a label statement.
5747 static statement_t *parse_label_statement(void)
5749 assert(token.type == T_IDENTIFIER);
5750 symbol_t *symbol = token.v.symbol;
5753 declaration_t *label = get_label(symbol);
5755 /* if source position is already set then the label is defined twice,
5756 * otherwise it was just mentioned in a goto so far */
5757 if(label->source_position.input_name != NULL) {
5758 errorf(HERE, "duplicate label '%Y'", symbol);
5759 errorf(label->source_position, "previous definition of '%Y' was here",
5762 label->source_position = token.source_position;
5765 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5767 statement->base.source_position = token.source_position;
5768 statement->label.label = label;
5772 if(token.type == '}') {
5773 /* TODO only warn? */
5774 errorf(HERE, "label at end of compound statement");
5777 if (token.type == ';') {
5778 /* eat an empty statement here, to avoid the warning about an empty
5779 * after a label. label:; is commonly used to have a label before
5783 statement->label.statement = parse_statement();
5787 /* remember the labels's in a list for later checking */
5788 if (label_last == NULL) {
5789 label_first = &statement->label;
5791 label_last->next = &statement->label;
5793 label_last = &statement->label;
5799 * Parse an if statement.
5801 static statement_t *parse_if(void)
5805 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5806 statement->base.source_position = token.source_position;
5809 statement->ifs.condition = parse_expression();
5812 statement->ifs.true_statement = parse_statement();
5813 if(token.type == T_else) {
5815 statement->ifs.false_statement = parse_statement();
5822 * Parse a switch statement.
5824 static statement_t *parse_switch(void)
5828 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5829 statement->base.source_position = token.source_position;
5832 expression_t *const expr = parse_expression();
5833 type_t * type = skip_typeref(expr->base.type);
5834 if (is_type_integer(type)) {
5835 type = promote_integer(type);
5836 } else if (is_type_valid(type)) {
5837 errorf(expr->base.source_position,
5838 "switch quantity is not an integer, but '%T'", type);
5839 type = type_error_type;
5841 statement->switchs.expression = create_implicit_cast(expr, type);
5844 switch_statement_t *rem = current_switch;
5845 current_switch = &statement->switchs;
5846 statement->switchs.body = parse_statement();
5847 current_switch = rem;
5849 if (warning.switch_default
5850 && find_default_label(&statement->switchs) == NULL) {
5851 warningf(statement->base.source_position, "switch has no default case");
5857 static statement_t *parse_loop_body(statement_t *const loop)
5859 statement_t *const rem = current_loop;
5860 current_loop = loop;
5862 statement_t *const body = parse_statement();
5869 * Parse a while statement.
5871 static statement_t *parse_while(void)
5875 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5876 statement->base.source_position = token.source_position;
5879 statement->whiles.condition = parse_expression();
5882 statement->whiles.body = parse_loop_body(statement);
5888 * Parse a do statement.
5890 static statement_t *parse_do(void)
5894 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5896 statement->base.source_position = token.source_position;
5898 statement->do_while.body = parse_loop_body(statement);
5902 statement->do_while.condition = parse_expression();
5910 * Parse a for statement.
5912 static statement_t *parse_for(void)
5916 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5917 statement->base.source_position = token.source_position;
5921 int top = environment_top();
5922 scope_t *last_scope = scope;
5923 set_scope(&statement->fors.scope);
5925 if(token.type != ';') {
5926 if(is_declaration_specifier(&token, false)) {
5927 parse_declaration(record_declaration);
5929 expression_t *const init = parse_expression();
5930 statement->fors.initialisation = init;
5931 if (warning.unused_value && !expression_has_effect(init)) {
5932 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5940 if(token.type != ';') {
5941 statement->fors.condition = parse_expression();
5944 if(token.type != ')') {
5945 expression_t *const step = parse_expression();
5946 statement->fors.step = step;
5947 if (warning.unused_value && !expression_has_effect(step)) {
5948 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5952 statement->fors.body = parse_loop_body(statement);
5954 assert(scope == &statement->fors.scope);
5955 set_scope(last_scope);
5956 environment_pop_to(top);
5962 * Parse a goto statement.
5964 static statement_t *parse_goto(void)
5968 if(token.type != T_IDENTIFIER) {
5969 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5973 symbol_t *symbol = token.v.symbol;
5976 declaration_t *label = get_label(symbol);
5978 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5979 statement->base.source_position = token.source_position;
5981 statement->gotos.label = label;
5983 /* remember the goto's in a list for later checking */
5984 if (goto_last == NULL) {
5985 goto_first = &statement->gotos;
5987 goto_last->next = &statement->gotos;
5989 goto_last = &statement->gotos;
5997 * Parse a continue statement.
5999 static statement_t *parse_continue(void)
6001 statement_t *statement;
6002 if (current_loop == NULL) {
6003 errorf(HERE, "continue statement not within loop");
6006 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6008 statement->base.source_position = token.source_position;
6018 * Parse a break statement.
6020 static statement_t *parse_break(void)
6022 statement_t *statement;
6023 if (current_switch == NULL && current_loop == NULL) {
6024 errorf(HERE, "break statement not within loop or switch");
6027 statement = allocate_statement_zero(STATEMENT_BREAK);
6029 statement->base.source_position = token.source_position;
6039 * Check if a given declaration represents a local variable.
6041 static bool is_local_var_declaration(const declaration_t *declaration) {
6042 switch ((storage_class_tag_t) declaration->storage_class) {
6043 case STORAGE_CLASS_AUTO:
6044 case STORAGE_CLASS_REGISTER: {
6045 const type_t *type = skip_typeref(declaration->type);
6046 if(is_type_function(type)) {
6058 * Check if a given declaration represents a variable.
6060 static bool is_var_declaration(const declaration_t *declaration) {
6061 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6064 const type_t *type = skip_typeref(declaration->type);
6065 return !is_type_function(type);
6069 * Check if a given expression represents a local variable.
6071 static bool is_local_variable(const expression_t *expression)
6073 if (expression->base.kind != EXPR_REFERENCE) {
6076 const declaration_t *declaration = expression->reference.declaration;
6077 return is_local_var_declaration(declaration);
6081 * Check if a given expression represents a local variable and
6082 * return its declaration then, else return NULL.
6084 declaration_t *expr_is_variable(const expression_t *expression)
6086 if (expression->base.kind != EXPR_REFERENCE) {
6089 declaration_t *declaration = expression->reference.declaration;
6090 if (is_var_declaration(declaration))
6096 * Parse a return statement.
6098 static statement_t *parse_return(void)
6102 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6103 statement->base.source_position = token.source_position;
6105 expression_t *return_value = NULL;
6106 if(token.type != ';') {
6107 return_value = parse_expression();
6111 const type_t *const func_type = current_function->type;
6112 assert(is_type_function(func_type));
6113 type_t *const return_type = skip_typeref(func_type->function.return_type);
6115 if(return_value != NULL) {
6116 type_t *return_value_type = skip_typeref(return_value->base.type);
6118 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6119 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6120 warningf(statement->base.source_position,
6121 "'return' with a value, in function returning void");
6122 return_value = NULL;
6124 type_t *const res_type = semantic_assign(return_type,
6125 return_value, "'return'");
6126 if (res_type == NULL) {
6127 errorf(statement->base.source_position,
6128 "cannot return something of type '%T' in function returning '%T'",
6129 return_value->base.type, return_type);
6131 return_value = create_implicit_cast(return_value, res_type);
6134 /* check for returning address of a local var */
6135 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6136 const expression_t *expression = return_value->unary.value;
6137 if (is_local_variable(expression)) {
6138 warningf(statement->base.source_position,
6139 "function returns address of local variable");
6143 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6144 warningf(statement->base.source_position,
6145 "'return' without value, in function returning non-void");
6148 statement->returns.value = return_value;
6154 * Parse a declaration statement.
6156 static statement_t *parse_declaration_statement(void)
6158 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6160 statement->base.source_position = token.source_position;
6162 declaration_t *before = last_declaration;
6163 parse_declaration(record_declaration);
6165 if(before == NULL) {
6166 statement->declaration.declarations_begin = scope->declarations;
6168 statement->declaration.declarations_begin = before->next;
6170 statement->declaration.declarations_end = last_declaration;
6176 * Parse an expression statement, ie. expr ';'.
6178 static statement_t *parse_expression_statement(void)
6180 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6182 statement->base.source_position = token.source_position;
6183 expression_t *const expr = parse_expression();
6184 statement->expression.expression = expr;
6186 if (warning.unused_value && !expression_has_effect(expr)) {
6187 warningf(expr->base.source_position, "statement has no effect");
6196 * Parse a statement.
6198 static statement_t *parse_statement(void)
6200 statement_t *statement = NULL;
6202 /* declaration or statement */
6203 switch(token.type) {
6205 statement = parse_asm_statement();
6209 statement = parse_case_statement();
6213 statement = parse_default_statement();
6217 statement = parse_compound_statement();
6221 statement = parse_if();
6225 statement = parse_switch();
6229 statement = parse_while();
6233 statement = parse_do();
6237 statement = parse_for();
6241 statement = parse_goto();
6245 statement = parse_continue();
6249 statement = parse_break();
6253 statement = parse_return();
6257 if (warning.empty_statement) {
6258 warningf(HERE, "statement is empty");
6265 if(look_ahead(1)->type == ':') {
6266 statement = parse_label_statement();
6270 if(is_typedef_symbol(token.v.symbol)) {
6271 statement = parse_declaration_statement();
6275 statement = parse_expression_statement();
6278 case T___extension__:
6279 /* this can be a prefix to a declaration or an expression statement */
6280 /* we simply eat it now and parse the rest with tail recursion */
6283 } while(token.type == T___extension__);
6284 statement = parse_statement();
6288 statement = parse_declaration_statement();
6292 statement = parse_expression_statement();
6296 assert(statement == NULL
6297 || statement->base.source_position.input_name != NULL);
6303 * Parse a compound statement.
6305 static statement_t *parse_compound_statement(void)
6307 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6309 statement->base.source_position = token.source_position;
6313 int top = environment_top();
6314 scope_t *last_scope = scope;
6315 set_scope(&statement->compound.scope);
6317 statement_t *last_statement = NULL;
6319 while(token.type != '}' && token.type != T_EOF) {
6320 statement_t *sub_statement = parse_statement();
6321 if(sub_statement == NULL)
6324 if(last_statement != NULL) {
6325 last_statement->base.next = sub_statement;
6327 statement->compound.statements = sub_statement;
6330 while(sub_statement->base.next != NULL)
6331 sub_statement = sub_statement->base.next;
6333 last_statement = sub_statement;
6336 if(token.type == '}') {
6339 errorf(statement->base.source_position,
6340 "end of file while looking for closing '}'");
6343 assert(scope == &statement->compound.scope);
6344 set_scope(last_scope);
6345 environment_pop_to(top);
6351 * Initialize builtin types.
6353 static void initialize_builtin_types(void)
6355 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6356 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6357 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6358 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6359 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6360 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6361 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6362 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6364 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6365 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6366 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6367 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6371 * Check for unused global static functions and variables
6373 static void check_unused_globals(void)
6375 if (!warning.unused_function && !warning.unused_variable)
6378 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6379 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6382 type_t *const type = decl->type;
6384 if (is_type_function(skip_typeref(type))) {
6385 if (!warning.unused_function || decl->is_inline)
6388 s = (decl->init.statement != NULL ? "defined" : "declared");
6390 if (!warning.unused_variable)
6396 warningf(decl->source_position, "'%#T' %s but not used",
6397 type, decl->symbol, s);
6402 * Parse a translation unit.
6404 static translation_unit_t *parse_translation_unit(void)
6406 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6408 assert(global_scope == NULL);
6409 global_scope = &unit->scope;
6411 assert(scope == NULL);
6412 set_scope(&unit->scope);
6414 initialize_builtin_types();
6416 while(token.type != T_EOF) {
6417 if (token.type == ';') {
6418 /* TODO error in strict mode */
6419 warningf(HERE, "stray ';' outside of function");
6422 parse_external_declaration();
6426 assert(scope == &unit->scope);
6428 last_declaration = NULL;
6430 assert(global_scope == &unit->scope);
6431 check_unused_globals();
6432 global_scope = NULL;
6440 * @return the translation unit or NULL if errors occurred.
6442 translation_unit_t *parse(void)
6444 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6445 label_stack = NEW_ARR_F(stack_entry_t, 0);
6446 diagnostic_count = 0;
6450 type_set_output(stderr);
6451 ast_set_output(stderr);
6453 lookahead_bufpos = 0;
6454 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6457 translation_unit_t *unit = parse_translation_unit();
6459 DEL_ARR_F(environment_stack);
6460 DEL_ARR_F(label_stack);
6469 * Initialize the parser.
6471 void init_parser(void)
6473 init_expression_parsers();
6474 obstack_init(&temp_obst);
6476 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6477 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6481 * Terminate the parser.
6483 void exit_parser(void)
6485 obstack_free(&temp_obst, NULL);