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 == '{') {
498 * Expect the the current token is the expected token.
499 * If not, generate an error, eat the current statement,
500 * and goto the end_error label.
502 #define expect(expected) \
504 if(UNLIKELY(token.type != (expected))) { \
505 parse_error_expected(NULL, (expected), 0); \
512 #define expect_block(expected) \
514 if(UNLIKELY(token.type != (expected))) { \
515 parse_error_expected(NULL, (expected), 0); \
522 #define expect_void(expected) \
524 if(UNLIKELY(token.type != (expected))) { \
525 parse_error_expected(NULL, (expected), 0); \
532 static void set_scope(scope_t *new_scope)
535 scope->last_declaration = last_declaration;
539 last_declaration = new_scope->last_declaration;
543 * Search a symbol in a given namespace and returns its declaration or
544 * NULL if this symbol was not found.
546 static declaration_t *get_declaration(const symbol_t *const symbol,
547 const namespace_t namespc)
549 declaration_t *declaration = symbol->declaration;
550 for( ; declaration != NULL; declaration = declaration->symbol_next) {
551 if(declaration->namespc == namespc)
559 * pushs an environment_entry on the environment stack and links the
560 * corresponding symbol to the new entry
562 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
564 symbol_t *symbol = declaration->symbol;
565 namespace_t namespc = (namespace_t) declaration->namespc;
567 /* replace/add declaration into declaration list of the symbol */
568 declaration_t *iter = symbol->declaration;
570 symbol->declaration = declaration;
572 declaration_t *iter_last = NULL;
573 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
574 /* replace an entry? */
575 if(iter->namespc == namespc) {
576 if(iter_last == NULL) {
577 symbol->declaration = declaration;
579 iter_last->symbol_next = declaration;
581 declaration->symbol_next = iter->symbol_next;
586 assert(iter_last->symbol_next == NULL);
587 iter_last->symbol_next = declaration;
591 /* remember old declaration */
593 entry.symbol = symbol;
594 entry.old_declaration = iter;
595 entry.namespc = (unsigned short) namespc;
596 ARR_APP1(stack_entry_t, *stack_ptr, entry);
599 static void environment_push(declaration_t *declaration)
601 assert(declaration->source_position.input_name != NULL);
602 assert(declaration->parent_scope != NULL);
603 stack_push(&environment_stack, declaration);
606 static void label_push(declaration_t *declaration)
608 declaration->parent_scope = ¤t_function->scope;
609 stack_push(&label_stack, declaration);
613 * pops symbols from the environment stack until @p new_top is the top element
615 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
617 stack_entry_t *stack = *stack_ptr;
618 size_t top = ARR_LEN(stack);
621 assert(new_top <= top);
625 for(i = top; i > new_top; --i) {
626 stack_entry_t *entry = &stack[i - 1];
628 declaration_t *old_declaration = entry->old_declaration;
629 symbol_t *symbol = entry->symbol;
630 namespace_t namespc = (namespace_t)entry->namespc;
632 /* replace/remove declaration */
633 declaration_t *declaration = symbol->declaration;
634 assert(declaration != NULL);
635 if(declaration->namespc == namespc) {
636 if(old_declaration == NULL) {
637 symbol->declaration = declaration->symbol_next;
639 symbol->declaration = old_declaration;
642 declaration_t *iter_last = declaration;
643 declaration_t *iter = declaration->symbol_next;
644 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
645 /* replace an entry? */
646 if(iter->namespc == namespc) {
647 assert(iter_last != NULL);
648 iter_last->symbol_next = old_declaration;
649 if(old_declaration != NULL) {
650 old_declaration->symbol_next = iter->symbol_next;
655 assert(iter != NULL);
659 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
662 static void environment_pop_to(size_t new_top)
664 stack_pop_to(&environment_stack, new_top);
667 static void label_pop_to(size_t new_top)
669 stack_pop_to(&label_stack, new_top);
673 static int get_rank(const type_t *type)
675 assert(!is_typeref(type));
676 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
677 * and esp. footnote 108). However we can't fold constants (yet), so we
678 * can't decide whether unsigned int is possible, while int always works.
679 * (unsigned int would be preferable when possible... for stuff like
680 * struct { enum { ... } bla : 4; } ) */
681 if(type->kind == TYPE_ENUM)
682 return ATOMIC_TYPE_INT;
684 assert(type->kind == TYPE_ATOMIC);
685 return type->atomic.akind;
688 static type_t *promote_integer(type_t *type)
690 if(type->kind == TYPE_BITFIELD)
691 type = type->bitfield.base;
693 if(get_rank(type) < ATOMIC_TYPE_INT)
700 * Create a cast expression.
702 * @param expression the expression to cast
703 * @param dest_type the destination type
705 static expression_t *create_cast_expression(expression_t *expression,
708 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
710 cast->unary.value = expression;
711 cast->base.type = dest_type;
717 * Check if a given expression represents the 0 pointer constant.
719 static bool is_null_pointer_constant(const expression_t *expression)
721 /* skip void* cast */
722 if(expression->kind == EXPR_UNARY_CAST
723 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
724 expression = expression->unary.value;
727 /* TODO: not correct yet, should be any constant integer expression
728 * which evaluates to 0 */
729 if (expression->kind != EXPR_CONST)
732 type_t *const type = skip_typeref(expression->base.type);
733 if (!is_type_integer(type))
736 return expression->conste.v.int_value == 0;
740 * Create an implicit cast expression.
742 * @param expression the expression to cast
743 * @param dest_type the destination type
745 static expression_t *create_implicit_cast(expression_t *expression,
748 type_t *const source_type = expression->base.type;
750 if (source_type == dest_type)
753 return create_cast_expression(expression, dest_type);
756 /** Implements the rules from § 6.5.16.1 */
757 static type_t *semantic_assign(type_t *orig_type_left,
758 const expression_t *const right,
761 type_t *const orig_type_right = right->base.type;
762 type_t *const type_left = skip_typeref(orig_type_left);
763 type_t *const type_right = skip_typeref(orig_type_right);
765 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
766 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
767 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
768 && is_type_pointer(type_right))) {
769 return orig_type_left;
772 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
773 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
774 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
776 /* the left type has all qualifiers from the right type */
777 unsigned missing_qualifiers
778 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
779 if(missing_qualifiers != 0) {
780 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
781 return orig_type_left;
784 points_to_left = get_unqualified_type(points_to_left);
785 points_to_right = get_unqualified_type(points_to_right);
787 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
788 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
789 return orig_type_left;
792 if (!types_compatible(points_to_left, points_to_right)) {
793 warningf(right->base.source_position,
794 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
795 orig_type_left, context, right, orig_type_right);
798 return orig_type_left;
801 if ((is_type_compound(type_left) && is_type_compound(type_right))
802 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
803 type_t *const unqual_type_left = get_unqualified_type(type_left);
804 type_t *const unqual_type_right = get_unqualified_type(type_right);
805 if (types_compatible(unqual_type_left, unqual_type_right)) {
806 return orig_type_left;
810 if (!is_type_valid(type_left))
813 if (!is_type_valid(type_right))
814 return orig_type_right;
819 static expression_t *parse_constant_expression(void)
821 /* start parsing at precedence 7 (conditional expression) */
822 expression_t *result = parse_sub_expression(7);
824 if(!is_constant_expression(result)) {
825 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
831 static expression_t *parse_assignment_expression(void)
833 /* start parsing at precedence 2 (assignment expression) */
834 return parse_sub_expression(2);
837 static type_t *make_global_typedef(const char *name, type_t *type)
839 symbol_t *const symbol = symbol_table_insert(name);
841 declaration_t *const declaration = allocate_declaration_zero();
842 declaration->namespc = NAMESPACE_NORMAL;
843 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
844 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
845 declaration->type = type;
846 declaration->symbol = symbol;
847 declaration->source_position = builtin_source_position;
849 record_declaration(declaration);
851 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
852 typedef_type->typedeft.declaration = declaration;
857 static string_t parse_string_literals(void)
859 assert(token.type == T_STRING_LITERAL);
860 string_t result = token.v.string;
864 while (token.type == T_STRING_LITERAL) {
865 result = concat_strings(&result, &token.v.string);
872 static void parse_attributes(void)
876 case T___attribute__: {
884 errorf(HERE, "EOF while parsing attribute");
903 if(token.type != T_STRING_LITERAL) {
904 parse_error_expected("while parsing assembler attribute",
909 parse_string_literals();
914 goto attributes_finished;
922 static designator_t *parse_designation(void)
924 designator_t *result = NULL;
925 designator_t *last = NULL;
928 designator_t *designator;
931 designator = allocate_ast_zero(sizeof(designator[0]));
932 designator->source_position = token.source_position;
934 designator->array_index = parse_constant_expression();
938 designator = allocate_ast_zero(sizeof(designator[0]));
939 designator->source_position = token.source_position;
941 if(token.type != T_IDENTIFIER) {
942 parse_error_expected("while parsing designator",
946 designator->symbol = token.v.symbol;
954 assert(designator != NULL);
956 last->next = designator;
966 static initializer_t *initializer_from_string(array_type_t *type,
967 const string_t *const string)
969 /* TODO: check len vs. size of array type */
972 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
973 initializer->string.string = *string;
978 static initializer_t *initializer_from_wide_string(array_type_t *const type,
979 wide_string_t *const string)
981 /* TODO: check len vs. size of array type */
984 initializer_t *const initializer =
985 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
986 initializer->wide_string.string = *string;
991 static initializer_t *initializer_from_expression(type_t *orig_type,
992 expression_t *expression)
994 /* TODO check that expression is a constant expression */
996 /* § 6.7.8.14/15 char array may be initialized by string literals */
997 type_t *type = skip_typeref(orig_type);
998 type_t *expr_type_orig = expression->base.type;
999 type_t *expr_type = skip_typeref(expr_type_orig);
1000 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1001 array_type_t *const array_type = &type->array;
1002 type_t *const element_type = skip_typeref(array_type->element_type);
1004 if (element_type->kind == TYPE_ATOMIC) {
1005 atomic_type_kind_t akind = element_type->atomic.akind;
1006 switch (expression->kind) {
1007 case EXPR_STRING_LITERAL:
1008 if (akind == ATOMIC_TYPE_CHAR
1009 || akind == ATOMIC_TYPE_SCHAR
1010 || akind == ATOMIC_TYPE_UCHAR) {
1011 return initializer_from_string(array_type,
1012 &expression->string.value);
1015 case EXPR_WIDE_STRING_LITERAL: {
1016 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1017 if (get_unqualified_type(element_type) == bare_wchar_type) {
1018 return initializer_from_wide_string(array_type,
1019 &expression->wide_string.value);
1029 type_t *const res_type = semantic_assign(type, expression, "initializer");
1030 if (res_type == NULL)
1033 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1034 result->value.value = create_implicit_cast(expression, res_type);
1039 static bool is_initializer_constant(const expression_t *expression)
1041 return is_constant_expression(expression)
1042 || is_address_constant(expression);
1045 static initializer_t *parse_scalar_initializer(type_t *type,
1046 bool must_be_constant)
1048 /* there might be extra {} hierarchies */
1050 while(token.type == '{') {
1053 warningf(HERE, "extra curly braces around scalar initializer");
1058 expression_t *expression = parse_assignment_expression();
1059 if(must_be_constant && !is_initializer_constant(expression)) {
1060 errorf(expression->base.source_position,
1061 "Initialisation expression '%E' is not constant\n",
1065 initializer_t *initializer = initializer_from_expression(type, expression);
1067 if(initializer == NULL) {
1068 errorf(expression->base.source_position,
1069 "expression '%E' doesn't match expected type '%T'",
1075 bool additional_warning_displayed = false;
1077 if(token.type == ',') {
1080 if(token.type != '}') {
1081 if(!additional_warning_displayed) {
1082 warningf(HERE, "additional elements in scalar initializer");
1083 additional_warning_displayed = true;
1093 typedef struct type_path_entry_t type_path_entry_t;
1094 struct type_path_entry_t {
1098 declaration_t *compound_entry;
1102 typedef struct type_path_t type_path_t;
1103 struct type_path_t {
1104 type_path_entry_t *path;
1105 type_t *top_type; /**< type of the element the path points */
1106 size_t max_index; /**< largest index in outermost array */
1110 static __attribute__((unused)) void debug_print_type_path(
1111 const type_path_t *path)
1113 size_t len = ARR_LEN(path->path);
1116 fprintf(stderr, "invalid path");
1120 for(size_t i = 0; i < len; ++i) {
1121 const type_path_entry_t *entry = & path->path[i];
1123 type_t *type = skip_typeref(entry->type);
1124 if(is_type_compound(type)) {
1125 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1126 } else if(is_type_array(type)) {
1127 fprintf(stderr, "[%u]", entry->v.index);
1129 fprintf(stderr, "-INVALID-");
1132 fprintf(stderr, " (");
1133 print_type(path->top_type);
1134 fprintf(stderr, ")");
1137 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1139 size_t len = ARR_LEN(path->path);
1141 return & path->path[len-1];
1144 static type_path_entry_t *append_to_type_path(type_path_t *path)
1146 size_t len = ARR_LEN(path->path);
1147 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1149 type_path_entry_t *result = & path->path[len];
1150 memset(result, 0, sizeof(result[0]));
1154 static void descend_into_subtype(type_path_t *path)
1156 type_t *orig_top_type = path->top_type;
1157 type_t *top_type = skip_typeref(orig_top_type);
1159 assert(is_type_compound(top_type) || is_type_array(top_type));
1161 type_path_entry_t *top = append_to_type_path(path);
1162 top->type = top_type;
1164 if(is_type_compound(top_type)) {
1165 declaration_t *declaration = top_type->compound.declaration;
1166 declaration_t *entry = declaration->scope.declarations;
1168 top->v.compound_entry = entry;
1169 path->top_type = entry->type;
1171 assert(is_type_array(top_type));
1174 path->top_type = top_type->array.element_type;
1178 static void ascend_from_subtype(type_path_t *path)
1180 type_path_entry_t *top = get_type_path_top(path);
1182 path->top_type = top->type;
1184 size_t len = ARR_LEN(path->path);
1185 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1188 static void ascend_to(type_path_t *path, size_t top_path_level)
1190 size_t len = ARR_LEN(path->path);
1191 assert(len >= top_path_level);
1193 while(len > top_path_level) {
1194 ascend_from_subtype(path);
1195 len = ARR_LEN(path->path);
1199 static bool walk_designator(type_path_t *path, const designator_t *designator,
1200 bool used_in_offsetof)
1202 for( ; designator != NULL; designator = designator->next) {
1203 type_path_entry_t *top = get_type_path_top(path);
1204 type_t *orig_type = top->type;
1206 type_t *type = skip_typeref(orig_type);
1208 if(designator->symbol != NULL) {
1209 symbol_t *symbol = designator->symbol;
1210 if(!is_type_compound(type)) {
1211 if(is_type_valid(type)) {
1212 errorf(designator->source_position,
1213 "'.%Y' designator used for non-compound type '%T'",
1219 declaration_t *declaration = type->compound.declaration;
1220 declaration_t *iter = declaration->scope.declarations;
1221 for( ; iter != NULL; iter = iter->next) {
1222 if(iter->symbol == symbol) {
1227 errorf(designator->source_position,
1228 "'%T' has no member named '%Y'", orig_type, symbol);
1231 if(used_in_offsetof) {
1232 type_t *real_type = skip_typeref(iter->type);
1233 if(real_type->kind == TYPE_BITFIELD) {
1234 errorf(designator->source_position,
1235 "offsetof designator '%Y' may not specify bitfield",
1241 top->type = orig_type;
1242 top->v.compound_entry = iter;
1243 orig_type = iter->type;
1245 expression_t *array_index = designator->array_index;
1246 assert(designator->array_index != NULL);
1248 if(!is_type_array(type)) {
1249 if(is_type_valid(type)) {
1250 errorf(designator->source_position,
1251 "[%E] designator used for non-array type '%T'",
1252 array_index, orig_type);
1256 if(!is_type_valid(array_index->base.type)) {
1260 long index = fold_constant(array_index);
1261 if(!used_in_offsetof) {
1263 errorf(designator->source_position,
1264 "array index [%E] must be positive", array_index);
1267 if(type->array.size_constant == true) {
1268 long array_size = type->array.size;
1269 if(index >= array_size) {
1270 errorf(designator->source_position,
1271 "designator [%E] (%d) exceeds array size %d",
1272 array_index, index, array_size);
1278 top->type = orig_type;
1279 top->v.index = (size_t) index;
1280 orig_type = type->array.element_type;
1282 path->top_type = orig_type;
1284 if(designator->next != NULL) {
1285 descend_into_subtype(path);
1289 path->invalid = false;
1296 static void advance_current_object(type_path_t *path, size_t top_path_level)
1301 type_path_entry_t *top = get_type_path_top(path);
1303 type_t *type = skip_typeref(top->type);
1304 if(is_type_union(type)) {
1305 /* in unions only the first element is initialized */
1306 top->v.compound_entry = NULL;
1307 } else if(is_type_struct(type)) {
1308 declaration_t *entry = top->v.compound_entry;
1310 entry = entry->next;
1311 top->v.compound_entry = entry;
1313 path->top_type = entry->type;
1317 assert(is_type_array(type));
1321 if(!type->array.size_constant || top->v.index < type->array.size) {
1326 /* we're past the last member of the current sub-aggregate, try if we
1327 * can ascend in the type hierarchy and continue with another subobject */
1328 size_t len = ARR_LEN(path->path);
1330 if(len > top_path_level) {
1331 ascend_from_subtype(path);
1332 advance_current_object(path, top_path_level);
1334 path->invalid = true;
1338 static void skip_initializers(void)
1340 if(token.type == '{')
1343 while(token.type != '}') {
1344 if(token.type == T_EOF)
1346 if(token.type == '{') {
1354 static initializer_t *parse_sub_initializer(type_path_t *path,
1355 type_t *outer_type, size_t top_path_level, bool must_be_constant)
1357 type_t *orig_type = path->top_type;
1358 type_t *type = skip_typeref(orig_type);
1360 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1361 * initializers in this case. */
1362 if(!is_type_valid(type)) {
1363 skip_initializers();
1367 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1370 designator_t *designator = NULL;
1371 if(token.type == '.' || token.type == '[') {
1372 designator = parse_designation();
1374 /* reset path to toplevel, evaluate designator from there */
1375 ascend_to(path, top_path_level);
1376 if(!walk_designator(path, designator, false)) {
1377 /* can't continue after designation error */
1381 initializer_t *designator_initializer
1382 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1383 designator_initializer->designator.designator = designator;
1384 ARR_APP1(initializer_t*, initializers, designator_initializer);
1389 if(token.type == '{') {
1390 if(is_type_scalar(type)) {
1391 sub = parse_scalar_initializer(type, must_be_constant);
1394 descend_into_subtype(path);
1396 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1399 ascend_from_subtype(path);
1404 /* must be an expression */
1405 expression_t *expression = parse_assignment_expression();
1407 if(must_be_constant && !is_initializer_constant(expression)) {
1408 errorf(expression->base.source_position,
1409 "Initialisation expression '%E' is not constant\n",
1413 /* handle { "string" } special case */
1414 if((expression->kind == EXPR_STRING_LITERAL
1415 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1416 && outer_type != NULL) {
1417 sub = initializer_from_expression(outer_type, expression);
1419 if(token.type == ',') {
1422 if(token.type != '}') {
1423 warningf(HERE, "excessive elements in initializer for type '%T'",
1426 /* TODO: eat , ... */
1431 /* descend into subtypes until expression matches type */
1433 orig_type = path->top_type;
1434 type = skip_typeref(orig_type);
1436 sub = initializer_from_expression(orig_type, expression);
1440 if(!is_type_valid(type)) {
1443 if(is_type_scalar(type)) {
1444 errorf(expression->base.source_position,
1445 "expression '%E' doesn't match expected type '%T'",
1446 expression, orig_type);
1450 descend_into_subtype(path);
1454 /* update largest index of top array */
1455 const type_path_entry_t *first = &path->path[0];
1456 type_t *first_type = first->type;
1457 first_type = skip_typeref(first_type);
1458 if(is_type_array(first_type)) {
1459 size_t index = first->v.index;
1460 if(index > path->max_index)
1461 path->max_index = index;
1464 /* append to initializers list */
1465 ARR_APP1(initializer_t*, initializers, sub);
1467 if(token.type == '}') {
1471 if(token.type == '}') {
1475 advance_current_object(path, top_path_level);
1476 orig_type = path->top_type;
1477 type = skip_typeref(orig_type);
1480 size_t len = ARR_LEN(initializers);
1481 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1482 initializer_t *result = allocate_ast_zero(size);
1483 result->kind = INITIALIZER_LIST;
1484 result->list.len = len;
1485 memcpy(&result->list.initializers, initializers,
1486 len * sizeof(initializers[0]));
1488 ascend_to(path, top_path_level);
1493 skip_initializers();
1494 DEL_ARR_F(initializers);
1495 ascend_to(path, top_path_level);
1499 typedef struct parse_initializer_env_t {
1500 type_t *type; /* the type of the initializer. In case of an
1501 array type with unspecified size this gets
1502 adjusted to the actual size. */
1503 initializer_t *initializer; /* initializer will be filled in here */
1504 bool must_be_constant;
1505 } parse_initializer_env_t;
1507 static void parse_initializer(parse_initializer_env_t *env)
1509 type_t *type = skip_typeref(env->type);
1510 initializer_t *result = NULL;
1513 if(is_type_scalar(type)) {
1514 /* TODO: § 6.7.8.11; eat {} without warning */
1515 result = parse_scalar_initializer(type, env->must_be_constant);
1516 } else if(token.type == '{') {
1520 memset(&path, 0, sizeof(path));
1521 path.top_type = env->type;
1522 path.path = NEW_ARR_F(type_path_entry_t, 0);
1524 descend_into_subtype(&path);
1526 result = parse_sub_initializer(&path, env->type, 1,
1527 env->must_be_constant);
1529 max_index = path.max_index;
1530 DEL_ARR_F(path.path);
1534 /* parse_scalar_initializer also works in this case: we simply
1535 * have an expression without {} around it */
1536 result = parse_scalar_initializer(type, env->must_be_constant);
1539 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1540 * the array type size */
1541 if(is_type_array(type) && type->array.size_expression == NULL
1542 && result != NULL) {
1544 switch (result->kind) {
1545 case INITIALIZER_LIST:
1546 size = max_index + 1;
1549 case INITIALIZER_STRING:
1550 size = result->string.string.size;
1553 case INITIALIZER_WIDE_STRING:
1554 size = result->wide_string.string.size;
1558 panic("invalid initializer type");
1561 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1562 cnst->base.type = type_size_t;
1563 cnst->conste.v.int_value = size;
1565 type_t *new_type = duplicate_type(type);
1567 new_type->array.size_expression = cnst;
1568 new_type->array.size_constant = true;
1569 new_type->array.size = size;
1570 env->type = new_type;
1573 env->initializer = result;
1576 static declaration_t *append_declaration(declaration_t *declaration);
1578 static declaration_t *parse_compound_type_specifier(bool is_struct)
1586 symbol_t *symbol = NULL;
1587 declaration_t *declaration = NULL;
1589 if (token.type == T___attribute__) {
1594 if(token.type == T_IDENTIFIER) {
1595 symbol = token.v.symbol;
1599 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1601 declaration = get_declaration(symbol, NAMESPACE_UNION);
1603 } else if(token.type != '{') {
1605 parse_error_expected("while parsing struct type specifier",
1606 T_IDENTIFIER, '{', 0);
1608 parse_error_expected("while parsing union type specifier",
1609 T_IDENTIFIER, '{', 0);
1615 if(declaration == NULL) {
1616 declaration = allocate_declaration_zero();
1617 declaration->namespc =
1618 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1619 declaration->source_position = token.source_position;
1620 declaration->symbol = symbol;
1621 declaration->parent_scope = scope;
1622 if (symbol != NULL) {
1623 environment_push(declaration);
1625 append_declaration(declaration);
1628 if(token.type == '{') {
1629 if(declaration->init.is_defined) {
1630 assert(symbol != NULL);
1631 errorf(HERE, "multiple definitions of '%s %Y'",
1632 is_struct ? "struct" : "union", symbol);
1633 declaration->scope.declarations = NULL;
1635 declaration->init.is_defined = true;
1637 parse_compound_type_entries(declaration);
1644 static void parse_enum_entries(type_t *const enum_type)
1648 if(token.type == '}') {
1650 errorf(HERE, "empty enum not allowed");
1655 if(token.type != T_IDENTIFIER) {
1656 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1661 declaration_t *const entry = allocate_declaration_zero();
1662 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1663 entry->type = enum_type;
1664 entry->symbol = token.v.symbol;
1665 entry->source_position = token.source_position;
1668 if(token.type == '=') {
1670 expression_t *value = parse_constant_expression();
1672 value = create_implicit_cast(value, enum_type);
1673 entry->init.enum_value = value;
1678 record_declaration(entry);
1680 if(token.type != ',')
1683 } while(token.type != '}');
1688 static type_t *parse_enum_specifier(void)
1692 declaration_t *declaration;
1695 if(token.type == T_IDENTIFIER) {
1696 symbol = token.v.symbol;
1699 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1700 } else if(token.type != '{') {
1701 parse_error_expected("while parsing enum type specifier",
1702 T_IDENTIFIER, '{', 0);
1709 if(declaration == NULL) {
1710 declaration = allocate_declaration_zero();
1711 declaration->namespc = NAMESPACE_ENUM;
1712 declaration->source_position = token.source_position;
1713 declaration->symbol = symbol;
1714 declaration->parent_scope = scope;
1717 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1718 type->enumt.declaration = declaration;
1720 if(token.type == '{') {
1721 if(declaration->init.is_defined) {
1722 errorf(HERE, "multiple definitions of enum %Y", symbol);
1724 if (symbol != NULL) {
1725 environment_push(declaration);
1727 append_declaration(declaration);
1728 declaration->init.is_defined = 1;
1730 parse_enum_entries(type);
1738 * if a symbol is a typedef to another type, return true
1740 static bool is_typedef_symbol(symbol_t *symbol)
1742 const declaration_t *const declaration =
1743 get_declaration(symbol, NAMESPACE_NORMAL);
1745 declaration != NULL &&
1746 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1749 static type_t *parse_typeof(void)
1757 expression_t *expression = NULL;
1760 switch(token.type) {
1761 case T___extension__:
1762 /* this can be a prefix to a typename or an expression */
1763 /* we simply eat it now. */
1766 } while(token.type == T___extension__);
1770 if(is_typedef_symbol(token.v.symbol)) {
1771 type = parse_typename();
1773 expression = parse_expression();
1774 type = expression->base.type;
1779 type = parse_typename();
1783 expression = parse_expression();
1784 type = expression->base.type;
1790 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1791 typeof_type->typeoft.expression = expression;
1792 typeof_type->typeoft.typeof_type = type;
1800 SPECIFIER_SIGNED = 1 << 0,
1801 SPECIFIER_UNSIGNED = 1 << 1,
1802 SPECIFIER_LONG = 1 << 2,
1803 SPECIFIER_INT = 1 << 3,
1804 SPECIFIER_DOUBLE = 1 << 4,
1805 SPECIFIER_CHAR = 1 << 5,
1806 SPECIFIER_SHORT = 1 << 6,
1807 SPECIFIER_LONG_LONG = 1 << 7,
1808 SPECIFIER_FLOAT = 1 << 8,
1809 SPECIFIER_BOOL = 1 << 9,
1810 SPECIFIER_VOID = 1 << 10,
1811 #ifdef PROVIDE_COMPLEX
1812 SPECIFIER_COMPLEX = 1 << 11,
1813 SPECIFIER_IMAGINARY = 1 << 12,
1817 static type_t *create_builtin_type(symbol_t *const symbol,
1818 type_t *const real_type)
1820 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1821 type->builtin.symbol = symbol;
1822 type->builtin.real_type = real_type;
1824 type_t *result = typehash_insert(type);
1825 if (type != result) {
1832 static type_t *get_typedef_type(symbol_t *symbol)
1834 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1835 if(declaration == NULL
1836 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1839 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1840 type->typedeft.declaration = declaration;
1845 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1847 type_t *type = NULL;
1848 unsigned type_qualifiers = 0;
1849 unsigned type_specifiers = 0;
1852 specifiers->source_position = token.source_position;
1855 switch(token.type) {
1858 #define MATCH_STORAGE_CLASS(token, class) \
1860 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
1861 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1863 specifiers->declared_storage_class = class; \
1867 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1868 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1869 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1870 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1871 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1874 switch (specifiers->declared_storage_class) {
1875 case STORAGE_CLASS_NONE:
1876 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
1879 case STORAGE_CLASS_EXTERN:
1880 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
1883 case STORAGE_CLASS_STATIC:
1884 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
1888 errorf(HERE, "multiple storage classes in declaration specifiers");
1894 /* type qualifiers */
1895 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1897 type_qualifiers |= qualifier; \
1901 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1902 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1903 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1905 case T___extension__:
1910 /* type specifiers */
1911 #define MATCH_SPECIFIER(token, specifier, name) \
1914 if(type_specifiers & specifier) { \
1915 errorf(HERE, "multiple " name " type specifiers given"); \
1917 type_specifiers |= specifier; \
1921 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1922 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1923 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1924 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1925 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1926 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1927 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1928 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1929 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1930 #ifdef PROVIDE_COMPLEX
1931 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1932 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1935 /* only in microsoft mode */
1936 specifiers->decl_modifiers |= DM_FORCEINLINE;
1940 specifiers->is_inline = true;
1945 if(type_specifiers & SPECIFIER_LONG_LONG) {
1946 errorf(HERE, "multiple type specifiers given");
1947 } else if(type_specifiers & SPECIFIER_LONG) {
1948 type_specifiers |= SPECIFIER_LONG_LONG;
1950 type_specifiers |= SPECIFIER_LONG;
1955 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1957 type->compound.declaration = parse_compound_type_specifier(true);
1961 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1963 type->compound.declaration = parse_compound_type_specifier(false);
1967 type = parse_enum_specifier();
1970 type = parse_typeof();
1972 case T___builtin_va_list:
1973 type = duplicate_type(type_valist);
1977 case T___attribute__:
1981 case T_IDENTIFIER: {
1982 /* only parse identifier if we haven't found a type yet */
1983 if(type != NULL || type_specifiers != 0)
1984 goto finish_specifiers;
1986 type_t *typedef_type = get_typedef_type(token.v.symbol);
1988 if(typedef_type == NULL)
1989 goto finish_specifiers;
1992 type = typedef_type;
1996 /* function specifier */
1998 goto finish_specifiers;
2005 atomic_type_kind_t atomic_type;
2007 /* match valid basic types */
2008 switch(type_specifiers) {
2009 case SPECIFIER_VOID:
2010 atomic_type = ATOMIC_TYPE_VOID;
2012 case SPECIFIER_CHAR:
2013 atomic_type = ATOMIC_TYPE_CHAR;
2015 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2016 atomic_type = ATOMIC_TYPE_SCHAR;
2018 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2019 atomic_type = ATOMIC_TYPE_UCHAR;
2021 case SPECIFIER_SHORT:
2022 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2023 case SPECIFIER_SHORT | SPECIFIER_INT:
2024 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2025 atomic_type = ATOMIC_TYPE_SHORT;
2027 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2028 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2029 atomic_type = ATOMIC_TYPE_USHORT;
2032 case SPECIFIER_SIGNED:
2033 case SPECIFIER_SIGNED | SPECIFIER_INT:
2034 atomic_type = ATOMIC_TYPE_INT;
2036 case SPECIFIER_UNSIGNED:
2037 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2038 atomic_type = ATOMIC_TYPE_UINT;
2040 case SPECIFIER_LONG:
2041 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2042 case SPECIFIER_LONG | SPECIFIER_INT:
2043 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2044 atomic_type = ATOMIC_TYPE_LONG;
2046 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2047 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2048 atomic_type = ATOMIC_TYPE_ULONG;
2050 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2051 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2052 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2053 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2055 atomic_type = ATOMIC_TYPE_LONGLONG;
2057 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2058 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2060 atomic_type = ATOMIC_TYPE_ULONGLONG;
2062 case SPECIFIER_FLOAT:
2063 atomic_type = ATOMIC_TYPE_FLOAT;
2065 case SPECIFIER_DOUBLE:
2066 atomic_type = ATOMIC_TYPE_DOUBLE;
2068 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2069 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2071 case SPECIFIER_BOOL:
2072 atomic_type = ATOMIC_TYPE_BOOL;
2074 #ifdef PROVIDE_COMPLEX
2075 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2076 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2078 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2079 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2081 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2082 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2084 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2085 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2087 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2088 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2090 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2091 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2095 /* invalid specifier combination, give an error message */
2096 if(type_specifiers == 0) {
2097 if (! strict_mode) {
2098 if (warning.implicit_int) {
2099 warningf(HERE, "no type specifiers in declaration, using 'int'");
2101 atomic_type = ATOMIC_TYPE_INT;
2104 errorf(HERE, "no type specifiers given in declaration");
2106 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2107 (type_specifiers & SPECIFIER_UNSIGNED)) {
2108 errorf(HERE, "signed and unsigned specifiers gives");
2109 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2110 errorf(HERE, "only integer types can be signed or unsigned");
2112 errorf(HERE, "multiple datatypes in declaration");
2114 atomic_type = ATOMIC_TYPE_INVALID;
2117 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2118 type->atomic.akind = atomic_type;
2121 if(type_specifiers != 0) {
2122 errorf(HERE, "multiple datatypes in declaration");
2126 type->base.qualifiers = type_qualifiers;
2128 type_t *result = typehash_insert(type);
2129 if(newtype && result != type) {
2133 specifiers->type = result;
2136 static type_qualifiers_t parse_type_qualifiers(void)
2138 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2141 switch(token.type) {
2142 /* type qualifiers */
2143 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2144 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2145 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2148 return type_qualifiers;
2153 static declaration_t *parse_identifier_list(void)
2155 declaration_t *declarations = NULL;
2156 declaration_t *last_declaration = NULL;
2158 declaration_t *const declaration = allocate_declaration_zero();
2159 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2160 declaration->source_position = token.source_position;
2161 declaration->symbol = token.v.symbol;
2164 if(last_declaration != NULL) {
2165 last_declaration->next = declaration;
2167 declarations = declaration;
2169 last_declaration = declaration;
2171 if(token.type != ',')
2174 } while(token.type == T_IDENTIFIER);
2176 return declarations;
2179 static void semantic_parameter(declaration_t *declaration)
2181 /* TODO: improve error messages */
2183 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2184 errorf(HERE, "typedef not allowed in parameter list");
2185 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2186 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2187 errorf(HERE, "parameter may only have none or register storage class");
2190 type_t *const orig_type = declaration->type;
2191 type_t * type = skip_typeref(orig_type);
2193 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2194 * into a pointer. § 6.7.5.3 (7) */
2195 if (is_type_array(type)) {
2196 type_t *const element_type = type->array.element_type;
2198 type = make_pointer_type(element_type, type->base.qualifiers);
2200 declaration->type = type;
2203 if(is_type_incomplete(type)) {
2204 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2205 orig_type, declaration->symbol);
2209 static declaration_t *parse_parameter(void)
2211 declaration_specifiers_t specifiers;
2212 memset(&specifiers, 0, sizeof(specifiers));
2214 parse_declaration_specifiers(&specifiers);
2216 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2218 semantic_parameter(declaration);
2223 static declaration_t *parse_parameters(function_type_t *type)
2225 if(token.type == T_IDENTIFIER) {
2226 symbol_t *symbol = token.v.symbol;
2227 if(!is_typedef_symbol(symbol)) {
2228 type->kr_style_parameters = true;
2229 return parse_identifier_list();
2233 if(token.type == ')') {
2234 type->unspecified_parameters = 1;
2237 if(token.type == T_void && look_ahead(1)->type == ')') {
2242 declaration_t *declarations = NULL;
2243 declaration_t *declaration;
2244 declaration_t *last_declaration = NULL;
2245 function_parameter_t *parameter;
2246 function_parameter_t *last_parameter = NULL;
2249 switch(token.type) {
2253 return declarations;
2256 case T___extension__:
2258 declaration = parse_parameter();
2260 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2261 memset(parameter, 0, sizeof(parameter[0]));
2262 parameter->type = declaration->type;
2264 if(last_parameter != NULL) {
2265 last_declaration->next = declaration;
2266 last_parameter->next = parameter;
2268 type->parameters = parameter;
2269 declarations = declaration;
2271 last_parameter = parameter;
2272 last_declaration = declaration;
2276 return declarations;
2278 if(token.type != ',')
2279 return declarations;
2289 } construct_type_kind_t;
2291 typedef struct construct_type_t construct_type_t;
2292 struct construct_type_t {
2293 construct_type_kind_t kind;
2294 construct_type_t *next;
2297 typedef struct parsed_pointer_t parsed_pointer_t;
2298 struct parsed_pointer_t {
2299 construct_type_t construct_type;
2300 type_qualifiers_t type_qualifiers;
2303 typedef struct construct_function_type_t construct_function_type_t;
2304 struct construct_function_type_t {
2305 construct_type_t construct_type;
2306 type_t *function_type;
2309 typedef struct parsed_array_t parsed_array_t;
2310 struct parsed_array_t {
2311 construct_type_t construct_type;
2312 type_qualifiers_t type_qualifiers;
2318 typedef struct construct_base_type_t construct_base_type_t;
2319 struct construct_base_type_t {
2320 construct_type_t construct_type;
2324 static construct_type_t *parse_pointer_declarator(void)
2328 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2329 memset(pointer, 0, sizeof(pointer[0]));
2330 pointer->construct_type.kind = CONSTRUCT_POINTER;
2331 pointer->type_qualifiers = parse_type_qualifiers();
2333 return (construct_type_t*) pointer;
2336 static construct_type_t *parse_array_declarator(void)
2340 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2341 memset(array, 0, sizeof(array[0]));
2342 array->construct_type.kind = CONSTRUCT_ARRAY;
2344 if(token.type == T_static) {
2345 array->is_static = true;
2349 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2350 if(type_qualifiers != 0) {
2351 if(token.type == T_static) {
2352 array->is_static = true;
2356 array->type_qualifiers = type_qualifiers;
2358 if(token.type == '*' && look_ahead(1)->type == ']') {
2359 array->is_variable = true;
2361 } else if(token.type != ']') {
2362 array->size = parse_assignment_expression();
2367 return (construct_type_t*) array;
2372 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2377 if(declaration != NULL) {
2378 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2380 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2383 declaration_t *parameters = parse_parameters(&type->function);
2384 if(declaration != NULL) {
2385 declaration->scope.declarations = parameters;
2388 construct_function_type_t *construct_function_type =
2389 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2390 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2391 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2392 construct_function_type->function_type = type;
2396 return (construct_type_t*) construct_function_type;
2401 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2402 bool may_be_abstract)
2404 /* construct a single linked list of construct_type_t's which describe
2405 * how to construct the final declarator type */
2406 construct_type_t *first = NULL;
2407 construct_type_t *last = NULL;
2410 while(token.type == '*') {
2411 construct_type_t *type = parse_pointer_declarator();
2422 /* TODO: find out if this is correct */
2425 construct_type_t *inner_types = NULL;
2427 switch(token.type) {
2429 if(declaration == NULL) {
2430 errorf(HERE, "no identifier expected in typename");
2432 declaration->symbol = token.v.symbol;
2433 declaration->source_position = token.source_position;
2439 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2445 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2446 /* avoid a loop in the outermost scope, because eat_statement doesn't
2448 if(token.type == '}' && current_function == NULL) {
2456 construct_type_t *p = last;
2459 construct_type_t *type;
2460 switch(token.type) {
2462 type = parse_function_declarator(declaration);
2465 type = parse_array_declarator();
2468 goto declarator_finished;
2471 /* insert in the middle of the list (behind p) */
2473 type->next = p->next;
2484 declarator_finished:
2487 /* append inner_types at the end of the list, we don't to set last anymore
2488 * as it's not needed anymore */
2490 assert(first == NULL);
2491 first = inner_types;
2493 last->next = inner_types;
2501 static type_t *construct_declarator_type(construct_type_t *construct_list,
2504 construct_type_t *iter = construct_list;
2505 for( ; iter != NULL; iter = iter->next) {
2506 switch(iter->kind) {
2507 case CONSTRUCT_INVALID:
2508 panic("invalid type construction found");
2509 case CONSTRUCT_FUNCTION: {
2510 construct_function_type_t *construct_function_type
2511 = (construct_function_type_t*) iter;
2513 type_t *function_type = construct_function_type->function_type;
2515 function_type->function.return_type = type;
2517 type_t *skipped_return_type = skip_typeref(type);
2518 if (is_type_function(skipped_return_type)) {
2519 errorf(HERE, "function returning function is not allowed");
2520 type = type_error_type;
2521 } else if (is_type_array(skipped_return_type)) {
2522 errorf(HERE, "function returning array is not allowed");
2523 type = type_error_type;
2525 type = function_type;
2530 case CONSTRUCT_POINTER: {
2531 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2532 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2533 pointer_type->pointer.points_to = type;
2534 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2536 type = pointer_type;
2540 case CONSTRUCT_ARRAY: {
2541 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2542 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2544 expression_t *size_expression = parsed_array->size;
2545 if(size_expression != NULL) {
2547 = create_implicit_cast(size_expression, type_size_t);
2550 array_type->base.qualifiers = parsed_array->type_qualifiers;
2551 array_type->array.element_type = type;
2552 array_type->array.is_static = parsed_array->is_static;
2553 array_type->array.is_variable = parsed_array->is_variable;
2554 array_type->array.size_expression = size_expression;
2556 if(size_expression != NULL) {
2557 if(is_constant_expression(size_expression)) {
2558 array_type->array.size_constant = true;
2559 array_type->array.size
2560 = fold_constant(size_expression);
2562 array_type->array.is_vla = true;
2566 type_t *skipped_type = skip_typeref(type);
2567 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2568 errorf(HERE, "array of void is not allowed");
2569 type = type_error_type;
2577 type_t *hashed_type = typehash_insert(type);
2578 if(hashed_type != type) {
2579 /* the function type was constructed earlier freeing it here will
2580 * destroy other types... */
2581 if(iter->kind != CONSTRUCT_FUNCTION) {
2591 static declaration_t *parse_declarator(
2592 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2594 declaration_t *const declaration = allocate_declaration_zero();
2595 declaration->declared_storage_class = specifiers->declared_storage_class;
2596 declaration->modifiers = specifiers->decl_modifiers;
2597 declaration->is_inline = specifiers->is_inline;
2599 declaration->storage_class = specifiers->declared_storage_class;
2600 if(declaration->storage_class == STORAGE_CLASS_NONE
2601 && scope != global_scope) {
2602 declaration->storage_class = STORAGE_CLASS_AUTO;
2605 construct_type_t *construct_type
2606 = parse_inner_declarator(declaration, may_be_abstract);
2607 type_t *const type = specifiers->type;
2608 declaration->type = construct_declarator_type(construct_type, type);
2610 if(construct_type != NULL) {
2611 obstack_free(&temp_obst, construct_type);
2617 static type_t *parse_abstract_declarator(type_t *base_type)
2619 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2621 type_t *result = construct_declarator_type(construct_type, base_type);
2622 if(construct_type != NULL) {
2623 obstack_free(&temp_obst, construct_type);
2629 static declaration_t *append_declaration(declaration_t* const declaration)
2631 if (last_declaration != NULL) {
2632 last_declaration->next = declaration;
2634 scope->declarations = declaration;
2636 last_declaration = declaration;
2641 * Check if the declaration of main is suspicious. main should be a
2642 * function with external linkage, returning int, taking either zero
2643 * arguments, two, or three arguments of appropriate types, ie.
2645 * int main([ int argc, char **argv [, char **env ] ]).
2647 * @param decl the declaration to check
2648 * @param type the function type of the declaration
2650 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2652 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2653 warningf(decl->source_position, "'main' is normally a non-static function");
2655 if (skip_typeref(func_type->return_type) != type_int) {
2656 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2658 const function_parameter_t *parm = func_type->parameters;
2660 type_t *const first_type = parm->type;
2661 if (!types_compatible(skip_typeref(first_type), type_int)) {
2662 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2666 type_t *const second_type = parm->type;
2667 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2668 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2672 type_t *const third_type = parm->type;
2673 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2674 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2678 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2682 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2688 * Check if a symbol is the equal to "main".
2690 static bool is_sym_main(const symbol_t *const sym)
2692 return strcmp(sym->string, "main") == 0;
2695 static declaration_t *internal_record_declaration(
2696 declaration_t *const declaration,
2697 const bool is_function_definition)
2699 const symbol_t *const symbol = declaration->symbol;
2700 const namespace_t namespc = (namespace_t)declaration->namespc;
2702 type_t *const orig_type = declaration->type;
2703 type_t *const type = skip_typeref(orig_type);
2704 if (is_type_function(type) &&
2705 type->function.unspecified_parameters &&
2706 warning.strict_prototypes) {
2707 warningf(declaration->source_position,
2708 "function declaration '%#T' is not a prototype",
2709 orig_type, declaration->symbol);
2712 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2713 check_type_of_main(declaration, &type->function);
2716 assert(declaration->symbol != NULL);
2717 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2719 assert(declaration != previous_declaration);
2720 if (previous_declaration != NULL) {
2721 if (previous_declaration->parent_scope == scope) {
2722 /* can happen for K&R style declarations */
2723 if(previous_declaration->type == NULL) {
2724 previous_declaration->type = declaration->type;
2727 const type_t *prev_type = skip_typeref(previous_declaration->type);
2728 if (!types_compatible(type, prev_type)) {
2729 errorf(declaration->source_position,
2730 "declaration '%#T' is incompatible with "
2731 "previous declaration '%#T'",
2732 orig_type, symbol, previous_declaration->type, symbol);
2733 errorf(previous_declaration->source_position,
2734 "previous declaration of '%Y' was here", symbol);
2736 unsigned old_storage_class
2737 = previous_declaration->storage_class;
2738 unsigned new_storage_class = declaration->storage_class;
2740 if(is_type_incomplete(prev_type)) {
2741 previous_declaration->type = type;
2745 /* pretend no storage class means extern for function
2746 * declarations (except if the previous declaration is neither
2747 * none nor extern) */
2748 if (is_type_function(type)) {
2749 switch (old_storage_class) {
2750 case STORAGE_CLASS_NONE:
2751 old_storage_class = STORAGE_CLASS_EXTERN;
2753 case STORAGE_CLASS_EXTERN:
2754 if (is_function_definition) {
2755 if (warning.missing_prototypes &&
2756 prev_type->function.unspecified_parameters &&
2757 !is_sym_main(symbol)) {
2758 warningf(declaration->source_position,
2759 "no previous prototype for '%#T'",
2762 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2763 new_storage_class = STORAGE_CLASS_EXTERN;
2771 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2772 new_storage_class == STORAGE_CLASS_EXTERN) {
2773 warn_redundant_declaration:
2774 if (warning.redundant_decls) {
2775 warningf(declaration->source_position,
2776 "redundant declaration for '%Y'", symbol);
2777 warningf(previous_declaration->source_position,
2778 "previous declaration of '%Y' was here",
2781 } else if (current_function == NULL) {
2782 if (old_storage_class != STORAGE_CLASS_STATIC &&
2783 new_storage_class == STORAGE_CLASS_STATIC) {
2784 errorf(declaration->source_position,
2785 "static declaration of '%Y' follows non-static declaration",
2787 errorf(previous_declaration->source_position,
2788 "previous declaration of '%Y' was here", symbol);
2790 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2791 goto warn_redundant_declaration;
2793 if (new_storage_class == STORAGE_CLASS_NONE) {
2794 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2795 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
2799 if (old_storage_class == new_storage_class) {
2800 errorf(declaration->source_position,
2801 "redeclaration of '%Y'", symbol);
2803 errorf(declaration->source_position,
2804 "redeclaration of '%Y' with different linkage",
2807 errorf(previous_declaration->source_position,
2808 "previous declaration of '%Y' was here", symbol);
2811 return previous_declaration;
2813 } else if (is_function_definition) {
2814 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2815 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2816 warningf(declaration->source_position,
2817 "no previous prototype for '%#T'", orig_type, symbol);
2818 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2819 warningf(declaration->source_position,
2820 "no previous declaration for '%#T'", orig_type,
2824 } else if (warning.missing_declarations &&
2825 scope == global_scope &&
2826 !is_type_function(type) && (
2827 declaration->storage_class == STORAGE_CLASS_NONE ||
2828 declaration->storage_class == STORAGE_CLASS_THREAD
2830 warningf(declaration->source_position,
2831 "no previous declaration for '%#T'", orig_type, symbol);
2834 assert(declaration->parent_scope == NULL);
2835 assert(scope != NULL);
2837 declaration->parent_scope = scope;
2839 environment_push(declaration);
2840 return append_declaration(declaration);
2843 static declaration_t *record_declaration(declaration_t *declaration)
2845 return internal_record_declaration(declaration, false);
2848 static declaration_t *record_function_definition(declaration_t *declaration)
2850 return internal_record_declaration(declaration, true);
2853 static void parser_error_multiple_definition(declaration_t *declaration,
2854 const source_position_t source_position)
2856 errorf(source_position, "multiple definition of symbol '%Y'",
2857 declaration->symbol);
2858 errorf(declaration->source_position,
2859 "this is the location of the previous definition.");
2862 static bool is_declaration_specifier(const token_t *token,
2863 bool only_type_specifiers)
2865 switch(token->type) {
2869 return is_typedef_symbol(token->v.symbol);
2871 case T___extension__:
2874 return !only_type_specifiers;
2881 static void parse_init_declarator_rest(declaration_t *declaration)
2885 type_t *orig_type = declaration->type;
2886 type_t *type = skip_typeref(orig_type);
2888 if(declaration->init.initializer != NULL) {
2889 parser_error_multiple_definition(declaration, token.source_position);
2892 bool must_be_constant = false;
2893 if(declaration->storage_class == STORAGE_CLASS_STATIC
2894 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
2895 || declaration->parent_scope == global_scope) {
2896 must_be_constant = true;
2899 parse_initializer_env_t env;
2900 env.type = orig_type;
2901 env.must_be_constant = must_be_constant;
2902 parse_initializer(&env);
2904 if(env.type != orig_type) {
2905 orig_type = env.type;
2906 type = skip_typeref(orig_type);
2907 declaration->type = env.type;
2910 if(is_type_function(type)) {
2911 errorf(declaration->source_position,
2912 "initializers not allowed for function types at declator '%Y' (type '%T')",
2913 declaration->symbol, orig_type);
2915 declaration->init.initializer = env.initializer;
2919 /* parse rest of a declaration without any declarator */
2920 static void parse_anonymous_declaration_rest(
2921 const declaration_specifiers_t *specifiers,
2922 parsed_declaration_func finished_declaration)
2926 declaration_t *const declaration = allocate_declaration_zero();
2927 declaration->type = specifiers->type;
2928 declaration->declared_storage_class = specifiers->declared_storage_class;
2929 declaration->source_position = specifiers->source_position;
2931 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
2932 warningf(declaration->source_position, "useless storage class in empty declaration");
2934 declaration->storage_class = STORAGE_CLASS_NONE;
2936 type_t *type = declaration->type;
2937 switch (type->kind) {
2938 case TYPE_COMPOUND_STRUCT:
2939 case TYPE_COMPOUND_UNION: {
2940 if (type->compound.declaration->symbol == NULL) {
2941 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2950 warningf(declaration->source_position, "empty declaration");
2954 finished_declaration(declaration);
2957 static void parse_declaration_rest(declaration_t *ndeclaration,
2958 const declaration_specifiers_t *specifiers,
2959 parsed_declaration_func finished_declaration)
2962 declaration_t *declaration = finished_declaration(ndeclaration);
2964 type_t *orig_type = declaration->type;
2965 type_t *type = skip_typeref(orig_type);
2967 if (type->kind != TYPE_FUNCTION &&
2968 declaration->is_inline &&
2969 is_type_valid(type)) {
2970 warningf(declaration->source_position,
2971 "variable '%Y' declared 'inline'\n", declaration->symbol);
2974 if(token.type == '=') {
2975 parse_init_declarator_rest(declaration);
2978 if(token.type != ',')
2982 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2987 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2989 symbol_t *symbol = declaration->symbol;
2990 if(symbol == NULL) {
2991 errorf(HERE, "anonymous declaration not valid as function parameter");
2994 namespace_t namespc = (namespace_t) declaration->namespc;
2995 if(namespc != NAMESPACE_NORMAL) {
2996 return record_declaration(declaration);
2999 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3000 if(previous_declaration == NULL ||
3001 previous_declaration->parent_scope != scope) {
3002 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
3007 if(previous_declaration->type == NULL) {
3008 previous_declaration->type = declaration->type;
3009 previous_declaration->declared_storage_class = declaration->declared_storage_class;
3010 previous_declaration->storage_class = declaration->storage_class;
3011 previous_declaration->parent_scope = scope;
3012 return previous_declaration;
3014 return record_declaration(declaration);
3018 static void parse_declaration(parsed_declaration_func finished_declaration)
3020 declaration_specifiers_t specifiers;
3021 memset(&specifiers, 0, sizeof(specifiers));
3022 parse_declaration_specifiers(&specifiers);
3024 if(token.type == ';') {
3025 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3027 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3028 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3032 static void parse_kr_declaration_list(declaration_t *declaration)
3034 type_t *type = skip_typeref(declaration->type);
3035 if(!is_type_function(type))
3038 if(!type->function.kr_style_parameters)
3041 /* push function parameters */
3042 int top = environment_top();
3043 scope_t *last_scope = scope;
3044 set_scope(&declaration->scope);
3046 declaration_t *parameter = declaration->scope.declarations;
3047 for( ; parameter != NULL; parameter = parameter->next) {
3048 assert(parameter->parent_scope == NULL);
3049 parameter->parent_scope = scope;
3050 environment_push(parameter);
3053 /* parse declaration list */
3054 while(is_declaration_specifier(&token, false)) {
3055 parse_declaration(finished_kr_declaration);
3058 /* pop function parameters */
3059 assert(scope == &declaration->scope);
3060 set_scope(last_scope);
3061 environment_pop_to(top);
3063 /* update function type */
3064 type_t *new_type = duplicate_type(type);
3065 new_type->function.kr_style_parameters = false;
3067 function_parameter_t *parameters = NULL;
3068 function_parameter_t *last_parameter = NULL;
3070 declaration_t *parameter_declaration = declaration->scope.declarations;
3071 for( ; parameter_declaration != NULL;
3072 parameter_declaration = parameter_declaration->next) {
3073 type_t *parameter_type = parameter_declaration->type;
3074 if(parameter_type == NULL) {
3076 errorf(HERE, "no type specified for function parameter '%Y'",
3077 parameter_declaration->symbol);
3079 if (warning.implicit_int) {
3080 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3081 parameter_declaration->symbol);
3083 parameter_type = type_int;
3084 parameter_declaration->type = parameter_type;
3088 semantic_parameter(parameter_declaration);
3089 parameter_type = parameter_declaration->type;
3091 function_parameter_t *function_parameter
3092 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3093 memset(function_parameter, 0, sizeof(function_parameter[0]));
3095 function_parameter->type = parameter_type;
3096 if(last_parameter != NULL) {
3097 last_parameter->next = function_parameter;
3099 parameters = function_parameter;
3101 last_parameter = function_parameter;
3103 new_type->function.parameters = parameters;
3105 type = typehash_insert(new_type);
3106 if(type != new_type) {
3107 obstack_free(type_obst, new_type);
3110 declaration->type = type;
3113 static bool first_err = true;
3116 * When called with first_err set, prints the name of the current function,
3119 static void print_in_function(void) {
3122 diagnosticf("%s: In function '%Y':\n",
3123 current_function->source_position.input_name,
3124 current_function->symbol);
3129 * Check if all labels are defined in the current function.
3130 * Check if all labels are used in the current function.
3132 static void check_labels(void)
3134 for (const goto_statement_t *goto_statement = goto_first;
3135 goto_statement != NULL;
3136 goto_statement = goto_statement->next) {
3137 declaration_t *label = goto_statement->label;
3140 if (label->source_position.input_name == NULL) {
3141 print_in_function();
3142 errorf(goto_statement->base.source_position,
3143 "label '%Y' used but not defined", label->symbol);
3146 goto_first = goto_last = NULL;
3148 if (warning.unused_label) {
3149 for (const label_statement_t *label_statement = label_first;
3150 label_statement != NULL;
3151 label_statement = label_statement->next) {
3152 const declaration_t *label = label_statement->label;
3154 if (! label->used) {
3155 print_in_function();
3156 warningf(label_statement->base.source_position,
3157 "label '%Y' defined but not used", label->symbol);
3161 label_first = label_last = NULL;
3165 * Check declarations of current_function for unused entities.
3167 static void check_declarations(void)
3169 if (warning.unused_parameter) {
3170 const scope_t *scope = ¤t_function->scope;
3172 const declaration_t *parameter = scope->declarations;
3173 for (; parameter != NULL; parameter = parameter->next) {
3174 if (! parameter->used) {
3175 print_in_function();
3176 warningf(parameter->source_position,
3177 "unused parameter '%Y'", parameter->symbol);
3181 if (warning.unused_variable) {
3185 static void parse_external_declaration(void)
3187 /* function-definitions and declarations both start with declaration
3189 declaration_specifiers_t specifiers;
3190 memset(&specifiers, 0, sizeof(specifiers));
3191 parse_declaration_specifiers(&specifiers);
3193 /* must be a declaration */
3194 if(token.type == ';') {
3195 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3199 /* declarator is common to both function-definitions and declarations */
3200 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3202 /* must be a declaration */
3203 if(token.type == ',' || token.type == '=' || token.type == ';') {
3204 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3208 /* must be a function definition */
3209 parse_kr_declaration_list(ndeclaration);
3211 if(token.type != '{') {
3212 parse_error_expected("while parsing function definition", '{', 0);
3217 type_t *type = ndeclaration->type;
3219 /* note that we don't skip typerefs: the standard doesn't allow them here
3220 * (so we can't use is_type_function here) */
3221 if(type->kind != TYPE_FUNCTION) {
3222 if (is_type_valid(type)) {
3223 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3224 type, ndeclaration->symbol);
3230 /* § 6.7.5.3 (14) a function definition with () means no
3231 * parameters (and not unspecified parameters) */
3232 if(type->function.unspecified_parameters) {
3233 type_t *duplicate = duplicate_type(type);
3234 duplicate->function.unspecified_parameters = false;
3236 type = typehash_insert(duplicate);
3237 if(type != duplicate) {
3238 obstack_free(type_obst, duplicate);
3240 ndeclaration->type = type;
3243 declaration_t *const declaration = record_function_definition(ndeclaration);
3244 if(ndeclaration != declaration) {
3245 declaration->scope = ndeclaration->scope;
3247 type = skip_typeref(declaration->type);
3249 /* push function parameters and switch scope */
3250 int top = environment_top();
3251 scope_t *last_scope = scope;
3252 set_scope(&declaration->scope);
3254 declaration_t *parameter = declaration->scope.declarations;
3255 for( ; parameter != NULL; parameter = parameter->next) {
3256 if(parameter->parent_scope == &ndeclaration->scope) {
3257 parameter->parent_scope = scope;
3259 assert(parameter->parent_scope == NULL
3260 || parameter->parent_scope == scope);
3261 parameter->parent_scope = scope;
3262 environment_push(parameter);
3265 if(declaration->init.statement != NULL) {
3266 parser_error_multiple_definition(declaration, token.source_position);
3268 goto end_of_parse_external_declaration;
3270 /* parse function body */
3271 int label_stack_top = label_top();
3272 declaration_t *old_current_function = current_function;
3273 current_function = declaration;
3275 declaration->init.statement = parse_compound_statement();
3278 check_declarations();
3280 assert(current_function == declaration);
3281 current_function = old_current_function;
3282 label_pop_to(label_stack_top);
3285 end_of_parse_external_declaration:
3286 assert(scope == &declaration->scope);
3287 set_scope(last_scope);
3288 environment_pop_to(top);
3291 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3292 source_position_t source_position)
3294 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3295 type->bitfield.base = base;
3296 type->bitfield.size = size;
3301 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3304 declaration_t *iter = compound_declaration->scope.declarations;
3305 for( ; iter != NULL; iter = iter->next) {
3306 if(iter->namespc != NAMESPACE_NORMAL)
3309 if(iter->symbol == NULL) {
3310 type_t *type = skip_typeref(iter->type);
3311 if(is_type_compound(type)) {
3312 declaration_t *result
3313 = find_compound_entry(type->compound.declaration, symbol);
3320 if(iter->symbol == symbol) {
3328 static void parse_compound_declarators(declaration_t *struct_declaration,
3329 const declaration_specifiers_t *specifiers)
3331 declaration_t *last_declaration = struct_declaration->scope.declarations;
3332 if(last_declaration != NULL) {
3333 while(last_declaration->next != NULL) {
3334 last_declaration = last_declaration->next;
3339 declaration_t *declaration;
3341 if(token.type == ':') {
3342 source_position_t source_position = HERE;
3345 type_t *base_type = specifiers->type;
3346 expression_t *size = parse_constant_expression();
3348 if(!is_type_integer(skip_typeref(base_type))) {
3349 errorf(HERE, "bitfield base type '%T' is not an integer type",
3353 type_t *type = make_bitfield_type(base_type, size, source_position);
3355 declaration = allocate_declaration_zero();
3356 declaration->namespc = NAMESPACE_NORMAL;
3357 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3358 declaration->storage_class = STORAGE_CLASS_NONE;
3359 declaration->source_position = source_position;
3360 declaration->modifiers = specifiers->decl_modifiers;
3361 declaration->type = type;
3363 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3365 type_t *orig_type = declaration->type;
3366 type_t *type = skip_typeref(orig_type);
3368 if(token.type == ':') {
3369 source_position_t source_position = HERE;
3371 expression_t *size = parse_constant_expression();
3373 if(!is_type_integer(type)) {
3374 errorf(HERE, "bitfield base type '%T' is not an "
3375 "integer type", orig_type);
3378 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3379 declaration->type = bitfield_type;
3381 /* TODO we ignore arrays for now... what is missing is a check
3382 * that they're at the end of the struct */
3383 if(is_type_incomplete(type) && !is_type_array(type)) {
3385 "compound member '%Y' has incomplete type '%T'",
3386 declaration->symbol, orig_type);
3387 } else if(is_type_function(type)) {
3388 errorf(HERE, "compound member '%Y' must not have function "
3389 "type '%T'", declaration->symbol, orig_type);
3394 /* make sure we don't define a symbol multiple times */
3395 symbol_t *symbol = declaration->symbol;
3396 if(symbol != NULL) {
3397 declaration_t *prev_decl
3398 = find_compound_entry(struct_declaration, symbol);
3400 if(prev_decl != NULL) {
3401 assert(prev_decl->symbol == symbol);
3402 errorf(declaration->source_position,
3403 "multiple declarations of symbol '%Y'", symbol);
3404 errorf(prev_decl->source_position,
3405 "previous declaration of '%Y' was here", symbol);
3409 /* append declaration */
3410 if(last_declaration != NULL) {
3411 last_declaration->next = declaration;
3413 struct_declaration->scope.declarations = declaration;
3415 last_declaration = declaration;
3417 if(token.type != ',')
3424 static void parse_compound_type_entries(declaration_t *compound_declaration)
3428 while(token.type != '}' && token.type != T_EOF) {
3429 declaration_specifiers_t specifiers;
3430 memset(&specifiers, 0, sizeof(specifiers));
3431 parse_declaration_specifiers(&specifiers);
3433 parse_compound_declarators(compound_declaration, &specifiers);
3435 if(token.type == T_EOF) {
3436 errorf(HERE, "EOF while parsing struct");
3441 static type_t *parse_typename(void)
3443 declaration_specifiers_t specifiers;
3444 memset(&specifiers, 0, sizeof(specifiers));
3445 parse_declaration_specifiers(&specifiers);
3446 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3447 /* TODO: improve error message, user does probably not know what a
3448 * storage class is...
3450 errorf(HERE, "typename may not have a storage class");
3453 type_t *result = parse_abstract_declarator(specifiers.type);
3461 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3462 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3463 expression_t *left);
3465 typedef struct expression_parser_function_t expression_parser_function_t;
3466 struct expression_parser_function_t {
3467 unsigned precedence;
3468 parse_expression_function parser;
3469 unsigned infix_precedence;
3470 parse_expression_infix_function infix_parser;
3473 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3476 * Creates a new invalid expression.
3478 static expression_t *create_invalid_expression(void)
3480 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3481 expression->base.source_position = token.source_position;
3486 * Prints an error message if an expression was expected but not read
3488 static expression_t *expected_expression_error(void)
3490 /* skip the error message if the error token was read */
3491 if (token.type != T_ERROR) {
3492 errorf(HERE, "expected expression, got token '%K'", &token);
3496 return create_invalid_expression();
3500 * Parse a string constant.
3502 static expression_t *parse_string_const(void)
3505 if (token.type == T_STRING_LITERAL) {
3506 string_t res = token.v.string;
3508 while (token.type == T_STRING_LITERAL) {
3509 res = concat_strings(&res, &token.v.string);
3512 if (token.type != T_WIDE_STRING_LITERAL) {
3513 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3514 /* note: that we use type_char_ptr here, which is already the
3515 * automatic converted type. revert_automatic_type_conversion
3516 * will construct the array type */
3517 cnst->base.type = type_char_ptr;
3518 cnst->string.value = res;
3522 wres = concat_string_wide_string(&res, &token.v.wide_string);
3524 wres = token.v.wide_string;
3529 switch (token.type) {
3530 case T_WIDE_STRING_LITERAL:
3531 wres = concat_wide_strings(&wres, &token.v.wide_string);
3534 case T_STRING_LITERAL:
3535 wres = concat_wide_string_string(&wres, &token.v.string);
3539 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3540 cnst->base.type = type_wchar_t_ptr;
3541 cnst->wide_string.value = wres;
3550 * Parse an integer constant.
3552 static expression_t *parse_int_const(void)
3554 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3555 cnst->base.source_position = HERE;
3556 cnst->base.type = token.datatype;
3557 cnst->conste.v.int_value = token.v.intvalue;
3565 * Parse a character constant.
3567 static expression_t *parse_character_constant(void)
3569 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
3571 cnst->base.source_position = HERE;
3572 cnst->base.type = token.datatype;
3573 cnst->conste.v.character = token.v.string;
3575 if (cnst->conste.v.character.size != 1) {
3576 if (warning.multichar && (c_mode & _GNUC)) {
3578 warningf(HERE, "multi-character character constant");
3580 errorf(HERE, "more than 1 characters in character constant");
3589 * Parse a wide character constant.
3591 static expression_t *parse_wide_character_constant(void)
3593 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
3595 cnst->base.source_position = HERE;
3596 cnst->base.type = token.datatype;
3597 cnst->conste.v.wide_character = token.v.wide_string;
3599 if (cnst->conste.v.wide_character.size != 1) {
3600 if (warning.multichar && (c_mode & _GNUC)) {
3602 warningf(HERE, "multi-character character constant");
3604 errorf(HERE, "more than 1 characters in character constant");
3613 * Parse a float constant.
3615 static expression_t *parse_float_const(void)
3617 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3618 cnst->base.type = token.datatype;
3619 cnst->conste.v.float_value = token.v.floatvalue;
3626 static declaration_t *create_implicit_function(symbol_t *symbol,
3627 const source_position_t source_position)
3629 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3630 ntype->function.return_type = type_int;
3631 ntype->function.unspecified_parameters = true;
3633 type_t *type = typehash_insert(ntype);
3638 declaration_t *const declaration = allocate_declaration_zero();
3639 declaration->storage_class = STORAGE_CLASS_EXTERN;
3640 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
3641 declaration->type = type;
3642 declaration->symbol = symbol;
3643 declaration->source_position = source_position;
3644 declaration->parent_scope = global_scope;
3646 scope_t *old_scope = scope;
3647 set_scope(global_scope);
3649 environment_push(declaration);
3650 /* prepends the declaration to the global declarations list */
3651 declaration->next = scope->declarations;
3652 scope->declarations = declaration;
3654 assert(scope == global_scope);
3655 set_scope(old_scope);
3661 * Creates a return_type (func)(argument_type) function type if not
3664 * @param return_type the return type
3665 * @param argument_type the argument type
3667 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3669 function_parameter_t *parameter
3670 = obstack_alloc(type_obst, sizeof(parameter[0]));
3671 memset(parameter, 0, sizeof(parameter[0]));
3672 parameter->type = argument_type;
3674 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3675 type->function.return_type = return_type;
3676 type->function.parameters = parameter;
3678 type_t *result = typehash_insert(type);
3679 if(result != type) {
3687 * Creates a function type for some function like builtins.
3689 * @param symbol the symbol describing the builtin
3691 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3693 switch(symbol->ID) {
3694 case T___builtin_alloca:
3695 return make_function_1_type(type_void_ptr, type_size_t);
3696 case T___builtin_nan:
3697 return make_function_1_type(type_double, type_char_ptr);
3698 case T___builtin_nanf:
3699 return make_function_1_type(type_float, type_char_ptr);
3700 case T___builtin_nand:
3701 return make_function_1_type(type_long_double, type_char_ptr);
3702 case T___builtin_va_end:
3703 return make_function_1_type(type_void, type_valist);
3705 panic("not implemented builtin symbol found");
3710 * Performs automatic type cast as described in § 6.3.2.1.
3712 * @param orig_type the original type
3714 static type_t *automatic_type_conversion(type_t *orig_type)
3716 type_t *type = skip_typeref(orig_type);
3717 if(is_type_array(type)) {
3718 array_type_t *array_type = &type->array;
3719 type_t *element_type = array_type->element_type;
3720 unsigned qualifiers = array_type->type.qualifiers;
3722 return make_pointer_type(element_type, qualifiers);
3725 if(is_type_function(type)) {
3726 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3733 * reverts the automatic casts of array to pointer types and function
3734 * to function-pointer types as defined § 6.3.2.1
3736 type_t *revert_automatic_type_conversion(const expression_t *expression)
3738 switch (expression->kind) {
3739 case EXPR_REFERENCE: return expression->reference.declaration->type;
3740 case EXPR_SELECT: return expression->select.compound_entry->type;
3742 case EXPR_UNARY_DEREFERENCE: {
3743 const expression_t *const value = expression->unary.value;
3744 type_t *const type = skip_typeref(value->base.type);
3745 assert(is_type_pointer(type));
3746 return type->pointer.points_to;
3749 case EXPR_BUILTIN_SYMBOL:
3750 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3752 case EXPR_ARRAY_ACCESS: {
3753 const expression_t *array_ref = expression->array_access.array_ref;
3754 type_t *type_left = skip_typeref(array_ref->base.type);
3755 if (!is_type_valid(type_left))
3757 assert(is_type_pointer(type_left));
3758 return type_left->pointer.points_to;
3761 case EXPR_STRING_LITERAL: {
3762 size_t size = expression->string.value.size;
3763 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
3766 case EXPR_WIDE_STRING_LITERAL: {
3767 size_t size = expression->wide_string.value.size;
3768 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
3771 case EXPR_COMPOUND_LITERAL:
3772 return expression->compound_literal.type;
3777 return expression->base.type;
3780 static expression_t *parse_reference(void)
3782 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3784 reference_expression_t *ref = &expression->reference;
3785 ref->symbol = token.v.symbol;
3787 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3789 source_position_t source_position = token.source_position;
3792 if(declaration == NULL) {
3793 if (! strict_mode && token.type == '(') {
3794 /* an implicitly defined function */
3795 if (warning.implicit_function_declaration) {
3796 warningf(HERE, "implicit declaration of function '%Y'",
3800 declaration = create_implicit_function(ref->symbol,
3803 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3804 return create_invalid_expression();
3808 type_t *type = declaration->type;
3810 /* we always do the auto-type conversions; the & and sizeof parser contains
3811 * code to revert this! */
3812 type = automatic_type_conversion(type);
3814 ref->declaration = declaration;
3815 ref->base.type = type;
3817 /* this declaration is used */
3818 declaration->used = true;
3823 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3827 /* TODO check if explicit cast is allowed and issue warnings/errors */
3830 static expression_t *parse_compound_literal(type_t *type)
3832 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
3834 parse_initializer_env_t env;
3836 env.must_be_constant = false;
3837 parse_initializer(&env);
3840 expression->compound_literal.type = type;
3841 expression->compound_literal.initializer = env.initializer;
3842 expression->base.type = automatic_type_conversion(type);
3848 * Parse a cast expression.
3850 static expression_t *parse_cast(void)
3852 source_position_t source_position = token.source_position;
3854 type_t *type = parse_typename();
3858 if(token.type == '{') {
3859 return parse_compound_literal(type);
3862 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3863 cast->base.source_position = source_position;
3865 expression_t *value = parse_sub_expression(20);
3867 check_cast_allowed(value, type);
3869 cast->base.type = type;
3870 cast->unary.value = value;
3874 return create_invalid_expression();
3878 * Parse a statement expression.
3880 static expression_t *parse_statement_expression(void)
3882 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3884 statement_t *statement = parse_compound_statement();
3885 expression->statement.statement = statement;
3886 expression->base.source_position = statement->base.source_position;
3888 /* find last statement and use its type */
3889 type_t *type = type_void;
3890 const statement_t *stmt = statement->compound.statements;
3892 while (stmt->base.next != NULL)
3893 stmt = stmt->base.next;
3895 if (stmt->kind == STATEMENT_EXPRESSION) {
3896 type = stmt->expression.expression->base.type;
3899 warningf(expression->base.source_position, "empty statement expression ({})");
3901 expression->base.type = type;
3907 return create_invalid_expression();
3911 * Parse a braced expression.
3913 static expression_t *parse_brace_expression(void)
3917 switch(token.type) {
3919 /* gcc extension: a statement expression */
3920 return parse_statement_expression();
3924 return parse_cast();
3926 if(is_typedef_symbol(token.v.symbol)) {
3927 return parse_cast();
3931 expression_t *result = parse_expression();
3936 return create_invalid_expression();
3939 static expression_t *parse_function_keyword(void)
3944 if (current_function == NULL) {
3945 errorf(HERE, "'__func__' used outside of a function");
3948 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3949 expression->base.type = type_char_ptr;
3954 static expression_t *parse_pretty_function_keyword(void)
3956 eat(T___PRETTY_FUNCTION__);
3959 if (current_function == NULL) {
3960 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3963 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3964 expression->base.type = type_char_ptr;
3969 static designator_t *parse_designator(void)
3971 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3972 result->source_position = HERE;
3974 if(token.type != T_IDENTIFIER) {
3975 parse_error_expected("while parsing member designator",
3980 result->symbol = token.v.symbol;
3983 designator_t *last_designator = result;
3985 if(token.type == '.') {
3987 if(token.type != T_IDENTIFIER) {
3988 parse_error_expected("while parsing member designator",
3993 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3994 designator->source_position = HERE;
3995 designator->symbol = token.v.symbol;
3998 last_designator->next = designator;
3999 last_designator = designator;
4002 if(token.type == '[') {
4004 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4005 designator->source_position = HERE;
4006 designator->array_index = parse_expression();
4007 if(designator->array_index == NULL) {
4013 last_designator->next = designator;
4014 last_designator = designator;
4026 * Parse the __builtin_offsetof() expression.
4028 static expression_t *parse_offsetof(void)
4030 eat(T___builtin_offsetof);
4032 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
4033 expression->base.type = type_size_t;
4036 type_t *type = parse_typename();
4038 designator_t *designator = parse_designator();
4041 expression->offsetofe.type = type;
4042 expression->offsetofe.designator = designator;
4045 memset(&path, 0, sizeof(path));
4046 path.top_type = type;
4047 path.path = NEW_ARR_F(type_path_entry_t, 0);
4049 descend_into_subtype(&path);
4051 if(!walk_designator(&path, designator, true)) {
4052 return create_invalid_expression();
4055 DEL_ARR_F(path.path);
4059 return create_invalid_expression();
4063 * Parses a _builtin_va_start() expression.
4065 static expression_t *parse_va_start(void)
4067 eat(T___builtin_va_start);
4069 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4072 expression->va_starte.ap = parse_assignment_expression();
4074 expression_t *const expr = parse_assignment_expression();
4075 if (expr->kind == EXPR_REFERENCE) {
4076 declaration_t *const decl = expr->reference.declaration;
4078 return create_invalid_expression();
4079 if (decl->parent_scope == ¤t_function->scope &&
4080 decl->next == NULL) {
4081 expression->va_starte.parameter = decl;
4086 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4088 return create_invalid_expression();
4092 * Parses a _builtin_va_arg() expression.
4094 static expression_t *parse_va_arg(void)
4096 eat(T___builtin_va_arg);
4098 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4101 expression->va_arge.ap = parse_assignment_expression();
4103 expression->base.type = parse_typename();
4108 return create_invalid_expression();
4111 static expression_t *parse_builtin_symbol(void)
4113 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4115 symbol_t *symbol = token.v.symbol;
4117 expression->builtin_symbol.symbol = symbol;
4120 type_t *type = get_builtin_symbol_type(symbol);
4121 type = automatic_type_conversion(type);
4123 expression->base.type = type;
4128 * Parses a __builtin_constant() expression.
4130 static expression_t *parse_builtin_constant(void)
4132 eat(T___builtin_constant_p);
4134 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4137 expression->builtin_constant.value = parse_assignment_expression();
4139 expression->base.type = type_int;
4143 return create_invalid_expression();
4147 * Parses a __builtin_prefetch() expression.
4149 static expression_t *parse_builtin_prefetch(void)
4151 eat(T___builtin_prefetch);
4153 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4156 expression->builtin_prefetch.adr = parse_assignment_expression();
4157 if (token.type == ',') {
4159 expression->builtin_prefetch.rw = parse_assignment_expression();
4161 if (token.type == ',') {
4163 expression->builtin_prefetch.locality = parse_assignment_expression();
4166 expression->base.type = type_void;
4170 return create_invalid_expression();
4174 * Parses a __builtin_is_*() compare expression.
4176 static expression_t *parse_compare_builtin(void)
4178 expression_t *expression;
4180 switch(token.type) {
4181 case T___builtin_isgreater:
4182 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4184 case T___builtin_isgreaterequal:
4185 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4187 case T___builtin_isless:
4188 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4190 case T___builtin_islessequal:
4191 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4193 case T___builtin_islessgreater:
4194 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4196 case T___builtin_isunordered:
4197 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4200 panic("invalid compare builtin found");
4203 expression->base.source_position = HERE;
4207 expression->binary.left = parse_assignment_expression();
4209 expression->binary.right = parse_assignment_expression();
4212 type_t *const orig_type_left = expression->binary.left->base.type;
4213 type_t *const orig_type_right = expression->binary.right->base.type;
4215 type_t *const type_left = skip_typeref(orig_type_left);
4216 type_t *const type_right = skip_typeref(orig_type_right);
4217 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4218 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4219 type_error_incompatible("invalid operands in comparison",
4220 expression->base.source_position, orig_type_left, orig_type_right);
4223 semantic_comparison(&expression->binary);
4228 return create_invalid_expression();
4232 * Parses a __builtin_expect() expression.
4234 static expression_t *parse_builtin_expect(void)
4236 eat(T___builtin_expect);
4238 expression_t *expression
4239 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4242 expression->binary.left = parse_assignment_expression();
4244 expression->binary.right = parse_constant_expression();
4247 expression->base.type = expression->binary.left->base.type;
4251 return create_invalid_expression();
4255 * Parses a MS assume() expression.
4257 static expression_t *parse_assume(void) {
4260 expression_t *expression
4261 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4264 expression->unary.value = parse_assignment_expression();
4267 expression->base.type = type_void;
4270 return create_invalid_expression();
4274 * Parses a primary expression.
4276 static expression_t *parse_primary_expression(void)
4278 switch (token.type) {
4279 case T_INTEGER: return parse_int_const();
4280 case T_CHARACTER_CONSTANT: return parse_character_constant();
4281 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4282 case T_FLOATINGPOINT: return parse_float_const();
4283 case T_STRING_LITERAL:
4284 case T_WIDE_STRING_LITERAL: return parse_string_const();
4285 case T_IDENTIFIER: return parse_reference();
4286 case T___FUNCTION__:
4287 case T___func__: return parse_function_keyword();
4288 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4289 case T___builtin_offsetof: return parse_offsetof();
4290 case T___builtin_va_start: return parse_va_start();
4291 case T___builtin_va_arg: return parse_va_arg();
4292 case T___builtin_expect: return parse_builtin_expect();
4293 case T___builtin_alloca:
4294 case T___builtin_nan:
4295 case T___builtin_nand:
4296 case T___builtin_nanf:
4297 case T___builtin_va_end: return parse_builtin_symbol();
4298 case T___builtin_isgreater:
4299 case T___builtin_isgreaterequal:
4300 case T___builtin_isless:
4301 case T___builtin_islessequal:
4302 case T___builtin_islessgreater:
4303 case T___builtin_isunordered: return parse_compare_builtin();
4304 case T___builtin_constant_p: return parse_builtin_constant();
4305 case T___builtin_prefetch: return parse_builtin_prefetch();
4306 case T_assume: return parse_assume();
4308 case '(': return parse_brace_expression();
4311 errorf(HERE, "unexpected token %K, expected an expression", &token);
4314 return create_invalid_expression();
4318 * Check if the expression has the character type and issue a warning then.
4320 static void check_for_char_index_type(const expression_t *expression) {
4321 type_t *const type = expression->base.type;
4322 const type_t *const base_type = skip_typeref(type);
4324 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4325 warning.char_subscripts) {
4326 warningf(expression->base.source_position,
4327 "array subscript has type '%T'", type);
4331 static expression_t *parse_array_expression(unsigned precedence,
4338 expression_t *inside = parse_expression();
4340 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4342 array_access_expression_t *array_access = &expression->array_access;
4344 type_t *const orig_type_left = left->base.type;
4345 type_t *const orig_type_inside = inside->base.type;
4347 type_t *const type_left = skip_typeref(orig_type_left);
4348 type_t *const type_inside = skip_typeref(orig_type_inside);
4350 type_t *return_type;
4351 if (is_type_pointer(type_left)) {
4352 return_type = type_left->pointer.points_to;
4353 array_access->array_ref = left;
4354 array_access->index = inside;
4355 check_for_char_index_type(inside);
4356 } else if (is_type_pointer(type_inside)) {
4357 return_type = type_inside->pointer.points_to;
4358 array_access->array_ref = inside;
4359 array_access->index = left;
4360 array_access->flipped = true;
4361 check_for_char_index_type(left);
4363 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4365 "array access on object with non-pointer types '%T', '%T'",
4366 orig_type_left, orig_type_inside);
4368 return_type = type_error_type;
4369 array_access->array_ref = create_invalid_expression();
4372 if(token.type != ']') {
4373 parse_error_expected("Problem while parsing array access", ']', 0);
4378 return_type = automatic_type_conversion(return_type);
4379 expression->base.type = return_type;
4384 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4386 expression_t *tp_expression = allocate_expression_zero(kind);
4387 tp_expression->base.type = type_size_t;
4389 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4391 tp_expression->typeprop.type = parse_typename();
4394 expression_t *expression = parse_sub_expression(precedence);
4395 expression->base.type = revert_automatic_type_conversion(expression);
4397 tp_expression->typeprop.type = expression->base.type;
4398 tp_expression->typeprop.tp_expression = expression;
4401 return tp_expression;
4403 return create_invalid_expression();
4406 static expression_t *parse_sizeof(unsigned precedence)
4409 return parse_typeprop(EXPR_SIZEOF, precedence);
4412 static expression_t *parse_alignof(unsigned precedence)
4415 return parse_typeprop(EXPR_SIZEOF, precedence);
4418 static expression_t *parse_select_expression(unsigned precedence,
4419 expression_t *compound)
4422 assert(token.type == '.' || token.type == T_MINUSGREATER);
4424 bool is_pointer = (token.type == T_MINUSGREATER);
4427 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4428 select->select.compound = compound;
4430 if(token.type != T_IDENTIFIER) {
4431 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4434 symbol_t *symbol = token.v.symbol;
4435 select->select.symbol = symbol;
4438 type_t *const orig_type = compound->base.type;
4439 type_t *const type = skip_typeref(orig_type);
4441 type_t *type_left = type;
4443 if (!is_type_pointer(type)) {
4444 if (is_type_valid(type)) {
4445 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4447 return create_invalid_expression();
4449 type_left = type->pointer.points_to;
4451 type_left = skip_typeref(type_left);
4453 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4454 type_left->kind != TYPE_COMPOUND_UNION) {
4455 if (is_type_valid(type_left)) {
4456 errorf(HERE, "request for member '%Y' in something not a struct or "
4457 "union, but '%T'", symbol, type_left);
4459 return create_invalid_expression();
4462 declaration_t *const declaration = type_left->compound.declaration;
4464 if(!declaration->init.is_defined) {
4465 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4467 return create_invalid_expression();
4470 declaration_t *iter = find_compound_entry(declaration, symbol);
4472 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4473 return create_invalid_expression();
4476 /* we always do the auto-type conversions; the & and sizeof parser contains
4477 * code to revert this! */
4478 type_t *expression_type = automatic_type_conversion(iter->type);
4480 select->select.compound_entry = iter;
4481 select->base.type = expression_type;
4483 if(expression_type->kind == TYPE_BITFIELD) {
4484 expression_t *extract
4485 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4486 extract->unary.value = select;
4487 extract->base.type = expression_type->bitfield.base;
4496 * Parse a call expression, ie. expression '( ... )'.
4498 * @param expression the function address
4500 static expression_t *parse_call_expression(unsigned precedence,
4501 expression_t *expression)
4504 expression_t *result = allocate_expression_zero(EXPR_CALL);
4506 call_expression_t *call = &result->call;
4507 call->function = expression;
4509 type_t *const orig_type = expression->base.type;
4510 type_t *const type = skip_typeref(orig_type);
4512 function_type_t *function_type = NULL;
4513 if (is_type_pointer(type)) {
4514 type_t *const to_type = skip_typeref(type->pointer.points_to);
4516 if (is_type_function(to_type)) {
4517 function_type = &to_type->function;
4518 call->base.type = function_type->return_type;
4522 if (function_type == NULL && is_type_valid(type)) {
4523 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4526 /* parse arguments */
4529 if(token.type != ')') {
4530 call_argument_t *last_argument = NULL;
4533 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4535 argument->expression = parse_assignment_expression();
4536 if(last_argument == NULL) {
4537 call->arguments = argument;
4539 last_argument->next = argument;
4541 last_argument = argument;
4543 if(token.type != ',')
4550 if(function_type != NULL) {
4551 function_parameter_t *parameter = function_type->parameters;
4552 call_argument_t *argument = call->arguments;
4553 for( ; parameter != NULL && argument != NULL;
4554 parameter = parameter->next, argument = argument->next) {
4555 type_t *expected_type = parameter->type;
4556 /* TODO report scope in error messages */
4557 expression_t *const arg_expr = argument->expression;
4558 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4559 if (res_type == NULL) {
4560 /* TODO improve error message */
4561 errorf(arg_expr->base.source_position,
4562 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4563 arg_expr, arg_expr->base.type, expected_type);
4565 argument->expression = create_implicit_cast(argument->expression, expected_type);
4568 /* too few parameters */
4569 if(parameter != NULL) {
4570 errorf(HERE, "too few arguments to function '%E'", expression);
4571 } else if(argument != NULL) {
4572 /* too many parameters */
4573 if(!function_type->variadic
4574 && !function_type->unspecified_parameters) {
4575 errorf(HERE, "too many arguments to function '%E'", expression);
4577 /* do default promotion */
4578 for( ; argument != NULL; argument = argument->next) {
4579 type_t *type = argument->expression->base.type;
4581 type = skip_typeref(type);
4582 if(is_type_integer(type)) {
4583 type = promote_integer(type);
4584 } else if(type == type_float) {
4588 argument->expression
4589 = create_implicit_cast(argument->expression, type);
4592 check_format(&result->call);
4595 check_format(&result->call);
4601 return create_invalid_expression();
4604 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4606 static bool same_compound_type(const type_t *type1, const type_t *type2)
4609 is_type_compound(type1) &&
4610 type1->kind == type2->kind &&
4611 type1->compound.declaration == type2->compound.declaration;
4615 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4617 * @param expression the conditional expression
4619 static expression_t *parse_conditional_expression(unsigned precedence,
4620 expression_t *expression)
4624 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4626 conditional_expression_t *conditional = &result->conditional;
4627 conditional->condition = expression;
4630 type_t *const condition_type_orig = expression->base.type;
4631 type_t *const condition_type = skip_typeref(condition_type_orig);
4632 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4633 type_error("expected a scalar type in conditional condition",
4634 expression->base.source_position, condition_type_orig);
4637 expression_t *true_expression = parse_expression();
4639 expression_t *false_expression = parse_sub_expression(precedence);
4641 type_t *const orig_true_type = true_expression->base.type;
4642 type_t *const orig_false_type = false_expression->base.type;
4643 type_t *const true_type = skip_typeref(orig_true_type);
4644 type_t *const false_type = skip_typeref(orig_false_type);
4647 type_t *result_type;
4648 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4649 result_type = semantic_arithmetic(true_type, false_type);
4651 true_expression = create_implicit_cast(true_expression, result_type);
4652 false_expression = create_implicit_cast(false_expression, result_type);
4654 conditional->true_expression = true_expression;
4655 conditional->false_expression = false_expression;
4656 conditional->base.type = result_type;
4657 } else if (same_compound_type(true_type, false_type) || (
4658 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4659 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4661 /* just take 1 of the 2 types */
4662 result_type = true_type;
4663 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4664 && pointers_compatible(true_type, false_type)) {
4666 result_type = true_type;
4667 } else if (is_type_pointer(true_type)
4668 && is_null_pointer_constant(false_expression)) {
4669 result_type = true_type;
4670 } else if (is_type_pointer(false_type)
4671 && is_null_pointer_constant(true_expression)) {
4672 result_type = false_type;
4674 /* TODO: one pointer to void*, other some pointer */
4676 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4677 type_error_incompatible("while parsing conditional",
4678 expression->base.source_position, true_type,
4681 result_type = type_error_type;
4684 conditional->true_expression
4685 = create_implicit_cast(true_expression, result_type);
4686 conditional->false_expression
4687 = create_implicit_cast(false_expression, result_type);
4688 conditional->base.type = result_type;
4691 return create_invalid_expression();
4695 * Parse an extension expression.
4697 static expression_t *parse_extension(unsigned precedence)
4699 eat(T___extension__);
4701 /* TODO enable extensions */
4702 expression_t *expression = parse_sub_expression(precedence);
4703 /* TODO disable extensions */
4708 * Parse a __builtin_classify_type() expression.
4710 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4712 eat(T___builtin_classify_type);
4714 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4715 result->base.type = type_int;
4718 expression_t *expression = parse_sub_expression(precedence);
4720 result->classify_type.type_expression = expression;
4724 return create_invalid_expression();
4727 static void semantic_incdec(unary_expression_t *expression)
4729 type_t *const orig_type = expression->value->base.type;
4730 type_t *const type = skip_typeref(orig_type);
4731 /* TODO !is_type_real && !is_type_pointer */
4732 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4733 if (is_type_valid(type)) {
4734 /* TODO: improve error message */
4735 errorf(HERE, "operation needs an arithmetic or pointer type");
4740 expression->base.type = orig_type;
4743 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4745 type_t *const orig_type = expression->value->base.type;
4746 type_t *const type = skip_typeref(orig_type);
4747 if(!is_type_arithmetic(type)) {
4748 if (is_type_valid(type)) {
4749 /* TODO: improve error message */
4750 errorf(HERE, "operation needs an arithmetic type");
4755 expression->base.type = orig_type;
4758 static void semantic_unexpr_scalar(unary_expression_t *expression)
4760 type_t *const orig_type = expression->value->base.type;
4761 type_t *const type = skip_typeref(orig_type);
4762 if (!is_type_scalar(type)) {
4763 if (is_type_valid(type)) {
4764 errorf(HERE, "operand of ! must be of scalar type");
4769 expression->base.type = orig_type;
4772 static void semantic_unexpr_integer(unary_expression_t *expression)
4774 type_t *const orig_type = expression->value->base.type;
4775 type_t *const type = skip_typeref(orig_type);
4776 if (!is_type_integer(type)) {
4777 if (is_type_valid(type)) {
4778 errorf(HERE, "operand of ~ must be of integer type");
4783 expression->base.type = orig_type;
4786 static void semantic_dereference(unary_expression_t *expression)
4788 type_t *const orig_type = expression->value->base.type;
4789 type_t *const type = skip_typeref(orig_type);
4790 if(!is_type_pointer(type)) {
4791 if (is_type_valid(type)) {
4792 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4797 type_t *result_type = type->pointer.points_to;
4798 result_type = automatic_type_conversion(result_type);
4799 expression->base.type = result_type;
4803 * Check the semantic of the address taken expression.
4805 static void semantic_take_addr(unary_expression_t *expression)
4807 expression_t *value = expression->value;
4808 value->base.type = revert_automatic_type_conversion(value);
4810 type_t *orig_type = value->base.type;
4811 if(!is_type_valid(orig_type))
4814 if(value->kind == EXPR_REFERENCE) {
4815 declaration_t *const declaration = value->reference.declaration;
4816 if(declaration != NULL) {
4817 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4818 errorf(expression->base.source_position,
4819 "address of register variable '%Y' requested",
4820 declaration->symbol);
4822 declaration->address_taken = 1;
4826 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4829 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4830 static expression_t *parse_##unexpression_type(unsigned precedence) \
4834 expression_t *unary_expression \
4835 = allocate_expression_zero(unexpression_type); \
4836 unary_expression->base.source_position = HERE; \
4837 unary_expression->unary.value = parse_sub_expression(precedence); \
4839 sfunc(&unary_expression->unary); \
4841 return unary_expression; \
4844 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4845 semantic_unexpr_arithmetic)
4846 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4847 semantic_unexpr_arithmetic)
4848 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4849 semantic_unexpr_scalar)
4850 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4851 semantic_dereference)
4852 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4854 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4855 semantic_unexpr_integer)
4856 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4858 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4861 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4863 static expression_t *parse_##unexpression_type(unsigned precedence, \
4864 expression_t *left) \
4866 (void) precedence; \
4869 expression_t *unary_expression \
4870 = allocate_expression_zero(unexpression_type); \
4871 unary_expression->unary.value = left; \
4873 sfunc(&unary_expression->unary); \
4875 return unary_expression; \
4878 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4879 EXPR_UNARY_POSTFIX_INCREMENT,
4881 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4882 EXPR_UNARY_POSTFIX_DECREMENT,
4885 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4887 /* TODO: handle complex + imaginary types */
4889 /* § 6.3.1.8 Usual arithmetic conversions */
4890 if(type_left == type_long_double || type_right == type_long_double) {
4891 return type_long_double;
4892 } else if(type_left == type_double || type_right == type_double) {
4894 } else if(type_left == type_float || type_right == type_float) {
4898 type_right = promote_integer(type_right);
4899 type_left = promote_integer(type_left);
4901 if(type_left == type_right)
4904 bool signed_left = is_type_signed(type_left);
4905 bool signed_right = is_type_signed(type_right);
4906 int rank_left = get_rank(type_left);
4907 int rank_right = get_rank(type_right);
4908 if(rank_left < rank_right) {
4909 if(signed_left == signed_right || !signed_right) {
4915 if(signed_left == signed_right || !signed_left) {
4924 * Check the semantic restrictions for a binary expression.
4926 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4928 expression_t *const left = expression->left;
4929 expression_t *const right = expression->right;
4930 type_t *const orig_type_left = left->base.type;
4931 type_t *const orig_type_right = right->base.type;
4932 type_t *const type_left = skip_typeref(orig_type_left);
4933 type_t *const type_right = skip_typeref(orig_type_right);
4935 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4936 /* TODO: improve error message */
4937 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4938 errorf(HERE, "operation needs arithmetic types");
4943 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4944 expression->left = create_implicit_cast(left, arithmetic_type);
4945 expression->right = create_implicit_cast(right, arithmetic_type);
4946 expression->base.type = arithmetic_type;
4949 static void semantic_shift_op(binary_expression_t *expression)
4951 expression_t *const left = expression->left;
4952 expression_t *const right = expression->right;
4953 type_t *const orig_type_left = left->base.type;
4954 type_t *const orig_type_right = right->base.type;
4955 type_t * type_left = skip_typeref(orig_type_left);
4956 type_t * type_right = skip_typeref(orig_type_right);
4958 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4959 /* TODO: improve error message */
4960 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4961 errorf(HERE, "operation needs integer types");
4966 type_left = promote_integer(type_left);
4967 type_right = promote_integer(type_right);
4969 expression->left = create_implicit_cast(left, type_left);
4970 expression->right = create_implicit_cast(right, type_right);
4971 expression->base.type = type_left;
4974 static void semantic_add(binary_expression_t *expression)
4976 expression_t *const left = expression->left;
4977 expression_t *const right = expression->right;
4978 type_t *const orig_type_left = left->base.type;
4979 type_t *const orig_type_right = right->base.type;
4980 type_t *const type_left = skip_typeref(orig_type_left);
4981 type_t *const type_right = skip_typeref(orig_type_right);
4984 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4985 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4986 expression->left = create_implicit_cast(left, arithmetic_type);
4987 expression->right = create_implicit_cast(right, arithmetic_type);
4988 expression->base.type = arithmetic_type;
4990 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4991 expression->base.type = type_left;
4992 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4993 expression->base.type = type_right;
4994 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4995 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4999 static void semantic_sub(binary_expression_t *expression)
5001 expression_t *const left = expression->left;
5002 expression_t *const right = expression->right;
5003 type_t *const orig_type_left = left->base.type;
5004 type_t *const orig_type_right = right->base.type;
5005 type_t *const type_left = skip_typeref(orig_type_left);
5006 type_t *const type_right = skip_typeref(orig_type_right);
5009 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5010 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5011 expression->left = create_implicit_cast(left, arithmetic_type);
5012 expression->right = create_implicit_cast(right, arithmetic_type);
5013 expression->base.type = arithmetic_type;
5015 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5016 expression->base.type = type_left;
5017 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
5018 if(!pointers_compatible(type_left, type_right)) {
5020 "pointers to incompatible objects to binary '-' ('%T', '%T')",
5021 orig_type_left, orig_type_right);
5023 expression->base.type = type_ptrdiff_t;
5025 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5026 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
5027 orig_type_left, orig_type_right);
5032 * Check the semantics of comparison expressions.
5034 * @param expression The expression to check.
5036 static void semantic_comparison(binary_expression_t *expression)
5038 expression_t *left = expression->left;
5039 expression_t *right = expression->right;
5040 type_t *orig_type_left = left->base.type;
5041 type_t *orig_type_right = right->base.type;
5043 type_t *type_left = skip_typeref(orig_type_left);
5044 type_t *type_right = skip_typeref(orig_type_right);
5046 /* TODO non-arithmetic types */
5047 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5048 if (warning.sign_compare &&
5049 (expression->base.kind != EXPR_BINARY_EQUAL &&
5050 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
5051 (is_type_signed(type_left) != is_type_signed(type_right))) {
5052 warningf(expression->base.source_position,
5053 "comparison between signed and unsigned");
5055 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5056 expression->left = create_implicit_cast(left, arithmetic_type);
5057 expression->right = create_implicit_cast(right, arithmetic_type);
5058 expression->base.type = arithmetic_type;
5059 if (warning.float_equal &&
5060 (expression->base.kind == EXPR_BINARY_EQUAL ||
5061 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
5062 is_type_float(arithmetic_type)) {
5063 warningf(expression->base.source_position,
5064 "comparing floating point with == or != is unsafe");
5066 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
5067 /* TODO check compatibility */
5068 } else if (is_type_pointer(type_left)) {
5069 expression->right = create_implicit_cast(right, type_left);
5070 } else if (is_type_pointer(type_right)) {
5071 expression->left = create_implicit_cast(left, type_right);
5072 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5073 type_error_incompatible("invalid operands in comparison",
5074 expression->base.source_position,
5075 type_left, type_right);
5077 expression->base.type = type_int;
5080 static void semantic_arithmetic_assign(binary_expression_t *expression)
5082 expression_t *left = expression->left;
5083 expression_t *right = expression->right;
5084 type_t *orig_type_left = left->base.type;
5085 type_t *orig_type_right = right->base.type;
5087 type_t *type_left = skip_typeref(orig_type_left);
5088 type_t *type_right = skip_typeref(orig_type_right);
5090 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5091 /* TODO: improve error message */
5092 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5093 errorf(HERE, "operation needs arithmetic types");
5098 /* combined instructions are tricky. We can't create an implicit cast on
5099 * the left side, because we need the uncasted form for the store.
5100 * The ast2firm pass has to know that left_type must be right_type
5101 * for the arithmetic operation and create a cast by itself */
5102 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5103 expression->right = create_implicit_cast(right, arithmetic_type);
5104 expression->base.type = type_left;
5107 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5109 expression_t *const left = expression->left;
5110 expression_t *const right = expression->right;
5111 type_t *const orig_type_left = left->base.type;
5112 type_t *const orig_type_right = right->base.type;
5113 type_t *const type_left = skip_typeref(orig_type_left);
5114 type_t *const type_right = skip_typeref(orig_type_right);
5116 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5117 /* combined instructions are tricky. We can't create an implicit cast on
5118 * the left side, because we need the uncasted form for the store.
5119 * The ast2firm pass has to know that left_type must be right_type
5120 * for the arithmetic operation and create a cast by itself */
5121 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5122 expression->right = create_implicit_cast(right, arithmetic_type);
5123 expression->base.type = type_left;
5124 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5125 expression->base.type = type_left;
5126 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5127 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5132 * Check the semantic restrictions of a logical expression.
5134 static void semantic_logical_op(binary_expression_t *expression)
5136 expression_t *const left = expression->left;
5137 expression_t *const right = expression->right;
5138 type_t *const orig_type_left = left->base.type;
5139 type_t *const orig_type_right = right->base.type;
5140 type_t *const type_left = skip_typeref(orig_type_left);
5141 type_t *const type_right = skip_typeref(orig_type_right);
5143 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5144 /* TODO: improve error message */
5145 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5146 errorf(HERE, "operation needs scalar types");
5151 expression->base.type = type_int;
5155 * Checks if a compound type has constant fields.
5157 static bool has_const_fields(const compound_type_t *type)
5159 const scope_t *scope = &type->declaration->scope;
5160 const declaration_t *declaration = scope->declarations;
5162 for (; declaration != NULL; declaration = declaration->next) {
5163 if (declaration->namespc != NAMESPACE_NORMAL)
5166 const type_t *decl_type = skip_typeref(declaration->type);
5167 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5175 * Check the semantic restrictions of a binary assign expression.
5177 static void semantic_binexpr_assign(binary_expression_t *expression)
5179 expression_t *left = expression->left;
5180 type_t *orig_type_left = left->base.type;
5182 type_t *type_left = revert_automatic_type_conversion(left);
5183 type_left = skip_typeref(orig_type_left);
5185 /* must be a modifiable lvalue */
5186 if (is_type_array(type_left)) {
5187 errorf(HERE, "cannot assign to arrays ('%E')", left);
5190 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5191 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5195 if(is_type_incomplete(type_left)) {
5197 "left-hand side of assignment '%E' has incomplete type '%T'",
5198 left, orig_type_left);
5201 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5202 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5203 left, orig_type_left);
5207 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5209 if (res_type == NULL) {
5210 errorf(expression->base.source_position,
5211 "cannot assign to '%T' from '%T'",
5212 orig_type_left, expression->right->base.type);
5214 expression->right = create_implicit_cast(expression->right, res_type);
5217 expression->base.type = orig_type_left;
5221 * Determine if the outermost operation (or parts thereof) of the given
5222 * expression has no effect in order to generate a warning about this fact.
5223 * Therefore in some cases this only examines some of the operands of the
5224 * expression (see comments in the function and examples below).
5226 * f() + 23; // warning, because + has no effect
5227 * x || f(); // no warning, because x controls execution of f()
5228 * x ? y : f(); // warning, because y has no effect
5229 * (void)x; // no warning to be able to suppress the warning
5230 * This function can NOT be used for an "expression has definitely no effect"-
5232 static bool expression_has_effect(const expression_t *const expr)
5234 switch (expr->kind) {
5235 case EXPR_UNKNOWN: break;
5236 case EXPR_INVALID: return true; /* do NOT warn */
5237 case EXPR_REFERENCE: return false;
5238 case EXPR_CONST: return false;
5239 case EXPR_CHARACTER_CONSTANT: return false;
5240 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5241 case EXPR_STRING_LITERAL: return false;
5242 case EXPR_WIDE_STRING_LITERAL: return false;
5245 const call_expression_t *const call = &expr->call;
5246 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5249 switch (call->function->builtin_symbol.symbol->ID) {
5250 case T___builtin_va_end: return true;
5251 default: return false;
5255 /* Generate the warning if either the left or right hand side of a
5256 * conditional expression has no effect */
5257 case EXPR_CONDITIONAL: {
5258 const conditional_expression_t *const cond = &expr->conditional;
5260 expression_has_effect(cond->true_expression) &&
5261 expression_has_effect(cond->false_expression);
5264 case EXPR_SELECT: return false;
5265 case EXPR_ARRAY_ACCESS: return false;
5266 case EXPR_SIZEOF: return false;
5267 case EXPR_CLASSIFY_TYPE: return false;
5268 case EXPR_ALIGNOF: return false;
5270 case EXPR_FUNCTION: return false;
5271 case EXPR_PRETTY_FUNCTION: return false;
5272 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5273 case EXPR_BUILTIN_CONSTANT_P: return false;
5274 case EXPR_BUILTIN_PREFETCH: return true;
5275 case EXPR_OFFSETOF: return false;
5276 case EXPR_VA_START: return true;
5277 case EXPR_VA_ARG: return true;
5278 case EXPR_STATEMENT: return true; // TODO
5279 case EXPR_COMPOUND_LITERAL: return false;
5281 case EXPR_UNARY_NEGATE: return false;
5282 case EXPR_UNARY_PLUS: return false;
5283 case EXPR_UNARY_BITWISE_NEGATE: return false;
5284 case EXPR_UNARY_NOT: return false;
5285 case EXPR_UNARY_DEREFERENCE: return false;
5286 case EXPR_UNARY_TAKE_ADDRESS: return false;
5287 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5288 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5289 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5290 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5292 /* Treat void casts as if they have an effect in order to being able to
5293 * suppress the warning */
5294 case EXPR_UNARY_CAST: {
5295 type_t *const type = skip_typeref(expr->base.type);
5296 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5299 case EXPR_UNARY_CAST_IMPLICIT: return true;
5300 case EXPR_UNARY_ASSUME: return true;
5301 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5303 case EXPR_BINARY_ADD: return false;
5304 case EXPR_BINARY_SUB: return false;
5305 case EXPR_BINARY_MUL: return false;
5306 case EXPR_BINARY_DIV: return false;
5307 case EXPR_BINARY_MOD: return false;
5308 case EXPR_BINARY_EQUAL: return false;
5309 case EXPR_BINARY_NOTEQUAL: return false;
5310 case EXPR_BINARY_LESS: return false;
5311 case EXPR_BINARY_LESSEQUAL: return false;
5312 case EXPR_BINARY_GREATER: return false;
5313 case EXPR_BINARY_GREATEREQUAL: return false;
5314 case EXPR_BINARY_BITWISE_AND: return false;
5315 case EXPR_BINARY_BITWISE_OR: return false;
5316 case EXPR_BINARY_BITWISE_XOR: return false;
5317 case EXPR_BINARY_SHIFTLEFT: return false;
5318 case EXPR_BINARY_SHIFTRIGHT: return false;
5319 case EXPR_BINARY_ASSIGN: return true;
5320 case EXPR_BINARY_MUL_ASSIGN: return true;
5321 case EXPR_BINARY_DIV_ASSIGN: return true;
5322 case EXPR_BINARY_MOD_ASSIGN: return true;
5323 case EXPR_BINARY_ADD_ASSIGN: return true;
5324 case EXPR_BINARY_SUB_ASSIGN: return true;
5325 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5326 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5327 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5328 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5329 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5331 /* Only examine the right hand side of && and ||, because the left hand
5332 * side already has the effect of controlling the execution of the right
5334 case EXPR_BINARY_LOGICAL_AND:
5335 case EXPR_BINARY_LOGICAL_OR:
5336 /* Only examine the right hand side of a comma expression, because the left
5337 * hand side has a separate warning */
5338 case EXPR_BINARY_COMMA:
5339 return expression_has_effect(expr->binary.right);
5341 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5342 case EXPR_BINARY_ISGREATER: return false;
5343 case EXPR_BINARY_ISGREATEREQUAL: return false;
5344 case EXPR_BINARY_ISLESS: return false;
5345 case EXPR_BINARY_ISLESSEQUAL: return false;
5346 case EXPR_BINARY_ISLESSGREATER: return false;
5347 case EXPR_BINARY_ISUNORDERED: return false;
5350 panic("unexpected expression");
5353 static void semantic_comma(binary_expression_t *expression)
5355 if (warning.unused_value) {
5356 const expression_t *const left = expression->left;
5357 if (!expression_has_effect(left)) {
5358 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5361 expression->base.type = expression->right->base.type;
5364 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5365 static expression_t *parse_##binexpression_type(unsigned precedence, \
5366 expression_t *left) \
5369 source_position_t pos = HERE; \
5371 expression_t *right = parse_sub_expression(precedence + lr); \
5373 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5374 binexpr->base.source_position = pos; \
5375 binexpr->binary.left = left; \
5376 binexpr->binary.right = right; \
5377 sfunc(&binexpr->binary); \
5382 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5383 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5384 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5385 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5386 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5387 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5388 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5389 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5390 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5392 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5393 semantic_comparison, 1)
5394 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5395 semantic_comparison, 1)
5396 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5397 semantic_comparison, 1)
5398 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5399 semantic_comparison, 1)
5401 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5402 semantic_binexpr_arithmetic, 1)
5403 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5404 semantic_binexpr_arithmetic, 1)
5405 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5406 semantic_binexpr_arithmetic, 1)
5407 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5408 semantic_logical_op, 1)
5409 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5410 semantic_logical_op, 1)
5411 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5412 semantic_shift_op, 1)
5413 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5414 semantic_shift_op, 1)
5415 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5416 semantic_arithmetic_addsubb_assign, 0)
5417 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5418 semantic_arithmetic_addsubb_assign, 0)
5419 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5420 semantic_arithmetic_assign, 0)
5421 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5422 semantic_arithmetic_assign, 0)
5423 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5424 semantic_arithmetic_assign, 0)
5425 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5426 semantic_arithmetic_assign, 0)
5427 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5428 semantic_arithmetic_assign, 0)
5429 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5430 semantic_arithmetic_assign, 0)
5431 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5432 semantic_arithmetic_assign, 0)
5433 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5434 semantic_arithmetic_assign, 0)
5436 static expression_t *parse_sub_expression(unsigned precedence)
5438 if(token.type < 0) {
5439 return expected_expression_error();
5442 expression_parser_function_t *parser
5443 = &expression_parsers[token.type];
5444 source_position_t source_position = token.source_position;
5447 if(parser->parser != NULL) {
5448 left = parser->parser(parser->precedence);
5450 left = parse_primary_expression();
5452 assert(left != NULL);
5453 left->base.source_position = source_position;
5456 if(token.type < 0) {
5457 return expected_expression_error();
5460 parser = &expression_parsers[token.type];
5461 if(parser->infix_parser == NULL)
5463 if(parser->infix_precedence < precedence)
5466 left = parser->infix_parser(parser->infix_precedence, left);
5468 assert(left != NULL);
5469 assert(left->kind != EXPR_UNKNOWN);
5470 left->base.source_position = source_position;
5477 * Parse an expression.
5479 static expression_t *parse_expression(void)
5481 return parse_sub_expression(1);
5485 * Register a parser for a prefix-like operator with given precedence.
5487 * @param parser the parser function
5488 * @param token_type the token type of the prefix token
5489 * @param precedence the precedence of the operator
5491 static void register_expression_parser(parse_expression_function parser,
5492 int token_type, unsigned precedence)
5494 expression_parser_function_t *entry = &expression_parsers[token_type];
5496 if(entry->parser != NULL) {
5497 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5498 panic("trying to register multiple expression parsers for a token");
5500 entry->parser = parser;
5501 entry->precedence = precedence;
5505 * Register a parser for an infix operator with given precedence.
5507 * @param parser the parser function
5508 * @param token_type the token type of the infix operator
5509 * @param precedence the precedence of the operator
5511 static void register_infix_parser(parse_expression_infix_function parser,
5512 int token_type, unsigned precedence)
5514 expression_parser_function_t *entry = &expression_parsers[token_type];
5516 if(entry->infix_parser != NULL) {
5517 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5518 panic("trying to register multiple infix expression parsers for a "
5521 entry->infix_parser = parser;
5522 entry->infix_precedence = precedence;
5526 * Initialize the expression parsers.
5528 static void init_expression_parsers(void)
5530 memset(&expression_parsers, 0, sizeof(expression_parsers));
5532 register_infix_parser(parse_array_expression, '[', 30);
5533 register_infix_parser(parse_call_expression, '(', 30);
5534 register_infix_parser(parse_select_expression, '.', 30);
5535 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5536 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5538 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5541 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5542 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5543 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5544 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5545 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5546 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5547 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5548 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5549 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5550 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5551 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5552 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5553 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5554 T_EXCLAMATIONMARKEQUAL, 13);
5555 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5556 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5557 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5558 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5559 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5560 register_infix_parser(parse_conditional_expression, '?', 7);
5561 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5562 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5563 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5564 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5565 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5566 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5567 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5568 T_LESSLESSEQUAL, 2);
5569 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5570 T_GREATERGREATEREQUAL, 2);
5571 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5573 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5575 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5578 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5580 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5581 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5582 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5583 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5584 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5585 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5586 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5588 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5590 register_expression_parser(parse_sizeof, T_sizeof, 25);
5591 register_expression_parser(parse_alignof, T___alignof__, 25);
5592 register_expression_parser(parse_extension, T___extension__, 25);
5593 register_expression_parser(parse_builtin_classify_type,
5594 T___builtin_classify_type, 25);
5598 * Parse a asm statement constraints specification.
5600 static asm_constraint_t *parse_asm_constraints(void)
5602 asm_constraint_t *result = NULL;
5603 asm_constraint_t *last = NULL;
5605 while(token.type == T_STRING_LITERAL || token.type == '[') {
5606 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5607 memset(constraint, 0, sizeof(constraint[0]));
5609 if(token.type == '[') {
5611 if(token.type != T_IDENTIFIER) {
5612 parse_error_expected("while parsing asm constraint",
5616 constraint->symbol = token.v.symbol;
5621 constraint->constraints = parse_string_literals();
5623 constraint->expression = parse_expression();
5627 last->next = constraint;
5629 result = constraint;
5633 if(token.type != ',')
5644 * Parse a asm statement clobber specification.
5646 static asm_clobber_t *parse_asm_clobbers(void)
5648 asm_clobber_t *result = NULL;
5649 asm_clobber_t *last = NULL;
5651 while(token.type == T_STRING_LITERAL) {
5652 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5653 clobber->clobber = parse_string_literals();
5656 last->next = clobber;
5662 if(token.type != ',')
5671 * Parse an asm statement.
5673 static statement_t *parse_asm_statement(void)
5677 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5678 statement->base.source_position = token.source_position;
5680 asm_statement_t *asm_statement = &statement->asms;
5682 if(token.type == T_volatile) {
5684 asm_statement->is_volatile = true;
5688 asm_statement->asm_text = parse_string_literals();
5690 if(token.type != ':')
5694 asm_statement->inputs = parse_asm_constraints();
5695 if(token.type != ':')
5699 asm_statement->outputs = parse_asm_constraints();
5700 if(token.type != ':')
5704 asm_statement->clobbers = parse_asm_clobbers();
5715 * Parse a case statement.
5717 static statement_t *parse_case_statement(void)
5721 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5723 statement->base.source_position = token.source_position;
5724 statement->case_label.expression = parse_expression();
5726 if (c_mode & _GNUC) {
5727 if (token.type == T_DOTDOTDOT) {
5729 statement->case_label.end_range = parse_expression();
5735 if (! is_constant_expression(statement->case_label.expression)) {
5736 errorf(statement->base.source_position,
5737 "case label does not reduce to an integer constant");
5739 /* TODO: check if the case label is already known */
5740 if (current_switch != NULL) {
5741 /* link all cases into the switch statement */
5742 if (current_switch->last_case == NULL) {
5743 current_switch->first_case =
5744 current_switch->last_case = &statement->case_label;
5746 current_switch->last_case->next = &statement->case_label;
5749 errorf(statement->base.source_position,
5750 "case label not within a switch statement");
5753 statement->case_label.statement = parse_statement();
5761 * Finds an existing default label of a switch statement.
5763 static case_label_statement_t *
5764 find_default_label(const switch_statement_t *statement)
5766 case_label_statement_t *label = statement->first_case;
5767 for ( ; label != NULL; label = label->next) {
5768 if (label->expression == NULL)
5775 * Parse a default statement.
5777 static statement_t *parse_default_statement(void)
5781 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5783 statement->base.source_position = token.source_position;
5786 if (current_switch != NULL) {
5787 const case_label_statement_t *def_label = find_default_label(current_switch);
5788 if (def_label != NULL) {
5789 errorf(HERE, "multiple default labels in one switch");
5790 errorf(def_label->base.source_position,
5791 "this is the first default label");
5793 /* link all cases into the switch statement */
5794 if (current_switch->last_case == NULL) {
5795 current_switch->first_case =
5796 current_switch->last_case = &statement->case_label;
5798 current_switch->last_case->next = &statement->case_label;
5802 errorf(statement->base.source_position,
5803 "'default' label not within a switch statement");
5805 statement->case_label.statement = parse_statement();
5813 * Return the declaration for a given label symbol or create a new one.
5815 static declaration_t *get_label(symbol_t *symbol)
5817 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5818 assert(current_function != NULL);
5819 /* if we found a label in the same function, then we already created the
5821 if(candidate != NULL
5822 && candidate->parent_scope == ¤t_function->scope) {
5826 /* otherwise we need to create a new one */
5827 declaration_t *const declaration = allocate_declaration_zero();
5828 declaration->namespc = NAMESPACE_LABEL;
5829 declaration->symbol = symbol;
5831 label_push(declaration);
5837 * Parse a label statement.
5839 static statement_t *parse_label_statement(void)
5841 assert(token.type == T_IDENTIFIER);
5842 symbol_t *symbol = token.v.symbol;
5845 declaration_t *label = get_label(symbol);
5847 /* if source position is already set then the label is defined twice,
5848 * otherwise it was just mentioned in a goto so far */
5849 if(label->source_position.input_name != NULL) {
5850 errorf(HERE, "duplicate label '%Y'", symbol);
5851 errorf(label->source_position, "previous definition of '%Y' was here",
5854 label->source_position = token.source_position;
5857 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5859 statement->base.source_position = token.source_position;
5860 statement->label.label = label;
5864 if(token.type == '}') {
5865 /* TODO only warn? */
5866 errorf(HERE, "label at end of compound statement");
5869 if (token.type == ';') {
5870 /* eat an empty statement here, to avoid the warning about an empty
5871 * after a label. label:; is commonly used to have a label before
5875 statement->label.statement = parse_statement();
5879 /* remember the labels's in a list for later checking */
5880 if (label_last == NULL) {
5881 label_first = &statement->label;
5883 label_last->next = &statement->label;
5885 label_last = &statement->label;
5891 * Parse an if statement.
5893 static statement_t *parse_if(void)
5897 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5898 statement->base.source_position = token.source_position;
5901 statement->ifs.condition = parse_expression();
5904 statement->ifs.true_statement = parse_statement();
5905 if(token.type == T_else) {
5907 statement->ifs.false_statement = parse_statement();
5916 * Parse a switch statement.
5918 static statement_t *parse_switch(void)
5922 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5923 statement->base.source_position = token.source_position;
5926 expression_t *const expr = parse_expression();
5927 type_t * type = skip_typeref(expr->base.type);
5928 if (is_type_integer(type)) {
5929 type = promote_integer(type);
5930 } else if (is_type_valid(type)) {
5931 errorf(expr->base.source_position,
5932 "switch quantity is not an integer, but '%T'", type);
5933 type = type_error_type;
5935 statement->switchs.expression = create_implicit_cast(expr, type);
5938 switch_statement_t *rem = current_switch;
5939 current_switch = &statement->switchs;
5940 statement->switchs.body = parse_statement();
5941 current_switch = rem;
5943 if (warning.switch_default
5944 && find_default_label(&statement->switchs) == NULL) {
5945 warningf(statement->base.source_position, "switch has no default case");
5953 static statement_t *parse_loop_body(statement_t *const loop)
5955 statement_t *const rem = current_loop;
5956 current_loop = loop;
5958 statement_t *const body = parse_statement();
5965 * Parse a while statement.
5967 static statement_t *parse_while(void)
5971 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5972 statement->base.source_position = token.source_position;
5975 statement->whiles.condition = parse_expression();
5978 statement->whiles.body = parse_loop_body(statement);
5986 * Parse a do statement.
5988 static statement_t *parse_do(void)
5992 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5994 statement->base.source_position = token.source_position;
5996 statement->do_while.body = parse_loop_body(statement);
6000 statement->do_while.condition = parse_expression();
6010 * Parse a for statement.
6012 static statement_t *parse_for(void)
6016 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
6017 statement->base.source_position = token.source_position;
6021 int top = environment_top();
6022 scope_t *last_scope = scope;
6023 set_scope(&statement->fors.scope);
6025 if(token.type != ';') {
6026 if(is_declaration_specifier(&token, false)) {
6027 parse_declaration(record_declaration);
6029 expression_t *const init = parse_expression();
6030 statement->fors.initialisation = init;
6031 if (warning.unused_value && !expression_has_effect(init)) {
6032 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
6040 if(token.type != ';') {
6041 statement->fors.condition = parse_expression();
6044 if(token.type != ')') {
6045 expression_t *const step = parse_expression();
6046 statement->fors.step = step;
6047 if (warning.unused_value && !expression_has_effect(step)) {
6048 warningf(step->base.source_position, "step of 'for'-statement has no effect");
6052 statement->fors.body = parse_loop_body(statement);
6054 assert(scope == &statement->fors.scope);
6055 set_scope(last_scope);
6056 environment_pop_to(top);
6064 * Parse a goto statement.
6066 static statement_t *parse_goto(void)
6070 if(token.type != T_IDENTIFIER) {
6071 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
6075 symbol_t *symbol = token.v.symbol;
6078 declaration_t *label = get_label(symbol);
6080 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
6081 statement->base.source_position = token.source_position;
6083 statement->gotos.label = label;
6085 /* remember the goto's in a list for later checking */
6086 if (goto_last == NULL) {
6087 goto_first = &statement->gotos;
6089 goto_last->next = &statement->gotos;
6091 goto_last = &statement->gotos;
6101 * Parse a continue statement.
6103 static statement_t *parse_continue(void)
6105 statement_t *statement;
6106 if (current_loop == NULL) {
6107 errorf(HERE, "continue statement not within loop");
6110 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6112 statement->base.source_position = token.source_position;
6124 * Parse a break statement.
6126 static statement_t *parse_break(void)
6128 statement_t *statement;
6129 if (current_switch == NULL && current_loop == NULL) {
6130 errorf(HERE, "break statement not within loop or switch");
6133 statement = allocate_statement_zero(STATEMENT_BREAK);
6135 statement->base.source_position = token.source_position;
6147 * Check if a given declaration represents a local variable.
6149 static bool is_local_var_declaration(const declaration_t *declaration) {
6150 switch ((storage_class_tag_t) declaration->storage_class) {
6151 case STORAGE_CLASS_AUTO:
6152 case STORAGE_CLASS_REGISTER: {
6153 const type_t *type = skip_typeref(declaration->type);
6154 if(is_type_function(type)) {
6166 * Check if a given declaration represents a variable.
6168 static bool is_var_declaration(const declaration_t *declaration) {
6169 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6172 const type_t *type = skip_typeref(declaration->type);
6173 return !is_type_function(type);
6177 * Check if a given expression represents a local variable.
6179 static bool is_local_variable(const expression_t *expression)
6181 if (expression->base.kind != EXPR_REFERENCE) {
6184 const declaration_t *declaration = expression->reference.declaration;
6185 return is_local_var_declaration(declaration);
6189 * Check if a given expression represents a local variable and
6190 * return its declaration then, else return NULL.
6192 declaration_t *expr_is_variable(const expression_t *expression)
6194 if (expression->base.kind != EXPR_REFERENCE) {
6197 declaration_t *declaration = expression->reference.declaration;
6198 if (is_var_declaration(declaration))
6204 * Parse a return statement.
6206 static statement_t *parse_return(void)
6210 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6211 statement->base.source_position = token.source_position;
6213 expression_t *return_value = NULL;
6214 if(token.type != ';') {
6215 return_value = parse_expression();
6219 const type_t *const func_type = current_function->type;
6220 assert(is_type_function(func_type));
6221 type_t *const return_type = skip_typeref(func_type->function.return_type);
6223 if(return_value != NULL) {
6224 type_t *return_value_type = skip_typeref(return_value->base.type);
6226 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6227 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6228 warningf(statement->base.source_position,
6229 "'return' with a value, in function returning void");
6230 return_value = NULL;
6232 type_t *const res_type = semantic_assign(return_type,
6233 return_value, "'return'");
6234 if (res_type == NULL) {
6235 errorf(statement->base.source_position,
6236 "cannot return something of type '%T' in function returning '%T'",
6237 return_value->base.type, return_type);
6239 return_value = create_implicit_cast(return_value, res_type);
6242 /* check for returning address of a local var */
6243 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6244 const expression_t *expression = return_value->unary.value;
6245 if (is_local_variable(expression)) {
6246 warningf(statement->base.source_position,
6247 "function returns address of local variable");
6251 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6252 warningf(statement->base.source_position,
6253 "'return' without value, in function returning non-void");
6256 statement->returns.value = return_value;
6264 * Parse a declaration statement.
6266 static statement_t *parse_declaration_statement(void)
6268 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6270 statement->base.source_position = token.source_position;
6272 declaration_t *before = last_declaration;
6273 parse_declaration(record_declaration);
6275 if(before == NULL) {
6276 statement->declaration.declarations_begin = scope->declarations;
6278 statement->declaration.declarations_begin = before->next;
6280 statement->declaration.declarations_end = last_declaration;
6286 * Parse an expression statement, ie. expr ';'.
6288 static statement_t *parse_expression_statement(void)
6290 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6292 statement->base.source_position = token.source_position;
6293 expression_t *const expr = parse_expression();
6294 statement->expression.expression = expr;
6296 if (warning.unused_value && !expression_has_effect(expr)) {
6297 warningf(expr->base.source_position, "statement has no effect");
6308 * Parse a statement.
6310 static statement_t *parse_statement(void)
6312 statement_t *statement = NULL;
6314 /* declaration or statement */
6315 switch(token.type) {
6317 statement = parse_asm_statement();
6321 statement = parse_case_statement();
6325 statement = parse_default_statement();
6329 statement = parse_compound_statement();
6333 statement = parse_if();
6337 statement = parse_switch();
6341 statement = parse_while();
6345 statement = parse_do();
6349 statement = parse_for();
6353 statement = parse_goto();
6357 statement = parse_continue();
6361 statement = parse_break();
6365 statement = parse_return();
6369 if (warning.empty_statement) {
6370 warningf(HERE, "statement is empty");
6377 if(look_ahead(1)->type == ':') {
6378 statement = parse_label_statement();
6382 if(is_typedef_symbol(token.v.symbol)) {
6383 statement = parse_declaration_statement();
6387 statement = parse_expression_statement();
6390 case T___extension__:
6391 /* this can be a prefix to a declaration or an expression statement */
6392 /* we simply eat it now and parse the rest with tail recursion */
6395 } while(token.type == T___extension__);
6396 statement = parse_statement();
6400 statement = parse_declaration_statement();
6404 statement = parse_expression_statement();
6408 assert(statement == NULL
6409 || statement->base.source_position.input_name != NULL);
6415 * Parse a compound statement.
6417 static statement_t *parse_compound_statement(void)
6419 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6421 statement->base.source_position = token.source_position;
6425 int top = environment_top();
6426 scope_t *last_scope = scope;
6427 set_scope(&statement->compound.scope);
6429 statement_t *last_statement = NULL;
6431 while(token.type != '}' && token.type != T_EOF) {
6432 statement_t *sub_statement = parse_statement();
6433 if(sub_statement == NULL)
6436 if(last_statement != NULL) {
6437 last_statement->base.next = sub_statement;
6439 statement->compound.statements = sub_statement;
6442 while(sub_statement->base.next != NULL)
6443 sub_statement = sub_statement->base.next;
6445 last_statement = sub_statement;
6448 if(token.type == '}') {
6451 errorf(statement->base.source_position,
6452 "end of file while looking for closing '}'");
6455 assert(scope == &statement->compound.scope);
6456 set_scope(last_scope);
6457 environment_pop_to(top);
6463 * Initialize builtin types.
6465 static void initialize_builtin_types(void)
6467 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6468 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6469 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6470 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6471 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6472 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6473 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6474 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6476 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6477 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6478 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6479 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6483 * Check for unused global static functions and variables
6485 static void check_unused_globals(void)
6487 if (!warning.unused_function && !warning.unused_variable)
6490 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6491 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6494 type_t *const type = decl->type;
6496 if (is_type_function(skip_typeref(type))) {
6497 if (!warning.unused_function || decl->is_inline)
6500 s = (decl->init.statement != NULL ? "defined" : "declared");
6502 if (!warning.unused_variable)
6508 warningf(decl->source_position, "'%#T' %s but not used",
6509 type, decl->symbol, s);
6514 * Parse a translation unit.
6516 static translation_unit_t *parse_translation_unit(void)
6518 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6520 assert(global_scope == NULL);
6521 global_scope = &unit->scope;
6523 assert(scope == NULL);
6524 set_scope(&unit->scope);
6526 initialize_builtin_types();
6528 while(token.type != T_EOF) {
6529 if (token.type == ';') {
6530 /* TODO error in strict mode */
6531 warningf(HERE, "stray ';' outside of function");
6534 parse_external_declaration();
6538 assert(scope == &unit->scope);
6540 last_declaration = NULL;
6542 assert(global_scope == &unit->scope);
6543 check_unused_globals();
6544 global_scope = NULL;
6552 * @return the translation unit or NULL if errors occurred.
6554 translation_unit_t *parse(void)
6556 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6557 label_stack = NEW_ARR_F(stack_entry_t, 0);
6558 diagnostic_count = 0;
6562 type_set_output(stderr);
6563 ast_set_output(stderr);
6565 lookahead_bufpos = 0;
6566 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6569 translation_unit_t *unit = parse_translation_unit();
6571 DEL_ARR_F(environment_stack);
6572 DEL_ARR_F(label_stack);
6581 * Initialize the parser.
6583 void init_parser(void)
6585 init_expression_parsers();
6586 obstack_init(&temp_obst);
6588 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6589 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6593 * Terminate the parser.
6595 void exit_parser(void)
6597 obstack_free(&temp_obst, NULL);