X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=parser.c;h=97252daaca3f21c75a839f65fe76241c2b9f439d;hb=5ee7d5e63a5309c4a6a94050fc55e2883dcc19c4;hp=b949ddabc4e274017427f1a0d7bc864c51a7784d;hpb=718d6a577cf109bc76988187e4d948e0c3bc1954;p=cparser diff --git a/parser.c b/parser.c index b949dda..97252da 100644 --- a/parser.c +++ b/parser.c @@ -140,7 +140,7 @@ static void *allocate_ast_zero(size_t size) static declaration_t *allocate_declaration_zero(void) { - declaration_t *declaration = allocate_ast_zero(sizeof(*allocate_declaration_zero())); + declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t)); declaration->type = type_error_type; return declaration; } @@ -206,7 +206,8 @@ static size_t get_expression_struct_size(expression_kind_t kind) [EXPR_CONDITIONAL] = sizeof(conditional_expression_t), [EXPR_SELECT] = sizeof(select_expression_t), [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t), - [EXPR_SIZEOF] = sizeof(sizeof_expression_t), + [EXPR_SIZEOF] = sizeof(typeprop_expression_t), + [EXPR_ALIGNOF] = sizeof(typeprop_expression_t), [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t), [EXPR_FUNCTION] = sizeof(string_literal_expression_t), [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t), @@ -238,8 +239,8 @@ static expression_t *allocate_expression_zero(expression_kind_t kind) size_t size = get_expression_struct_size(kind); expression_t *res = allocate_ast_zero(size); - res->base.kind = kind; - res->base.datatype = type_error_type; + res->base.kind = kind; + res->base.type = type_error_type; return res; } @@ -666,8 +667,8 @@ static expression_t *create_cast_expression(expression_t *expression, { expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT); - cast->unary.value = expression; - cast->base.datatype = dest_type; + cast->unary.value = expression; + cast->base.type = dest_type; return cast; } @@ -688,7 +689,7 @@ static bool is_null_pointer_constant(const expression_t *expression) if (expression->kind != EXPR_CONST) return false; - type_t *const type = skip_typeref(expression->base.datatype); + type_t *const type = skip_typeref(expression->base.type); if (!is_type_integer(type)) return false; @@ -704,7 +705,7 @@ static bool is_null_pointer_constant(const expression_t *expression) static expression_t *create_implicit_cast(expression_t *expression, type_t *dest_type) { - type_t *const source_type = expression->base.datatype; + type_t *const source_type = expression->base.type; if (source_type == dest_type) return expression; @@ -717,7 +718,7 @@ static type_t *semantic_assign(type_t *orig_type_left, const expression_t *const right, const char *context) { - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_right = right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); @@ -947,7 +948,7 @@ static initializer_t *initializer_from_expression(type_t *type, /* TODO check that expression is a constant expression */ /* § 6.7.8.14/15 char array may be initialized by string literals */ - type_t *const expr_type = expression->base.datatype; + type_t *const expr_type = expression->base.type; if (is_type_array(type) && expr_type->kind == TYPE_POINTER) { array_type_t *const array_type = &type->array; type_t *const element_type = skip_typeref(array_type->element_type); @@ -1195,7 +1196,7 @@ static initializer_t *parse_initializer(type_t *const orig_type) if(initializer == NULL) { errorf(HERE, "initializer expression '%E' of type '%T' is incompatible with type '%T'", - expression, expression->base.datatype, orig_type); + expression, expression->base.type, orig_type); } return initializer; } @@ -1422,7 +1423,7 @@ restart: type = parse_typename(); } else { expression = parse_expression(); - type = expression->base.datatype; + type = expression->base.type; } break; @@ -1432,7 +1433,7 @@ restart: default: expression = parse_expression(); - type = expression->base.datatype; + type = expression->base.type; break; } @@ -1804,6 +1805,7 @@ static declaration_t *parse_identifier_list(void) declaration_t *last_declaration = NULL; do { declaration_t *const declaration = allocate_declaration_zero(); + declaration->type = NULL; /* a K&R parameter list has no types, yet */ declaration->source_position = token.source_position; declaration->symbol = token.v.symbol; next_token(); @@ -2313,8 +2315,8 @@ static declaration_t *internal_record_declaration( const symbol_t *const symbol = declaration->symbol; const namespace_t namespc = (namespace_t)declaration->namespc; - type_t *const orig_type = declaration->type; - const type_t *const type = skip_typeref(orig_type); + type_t *const orig_type = declaration->type; + type_t *const type = skip_typeref(orig_type); if (is_type_function(type) && type->function.unspecified_parameters && warning.strict_prototypes) { @@ -2336,18 +2338,27 @@ static declaration_t *internal_record_declaration( previous_declaration->type = declaration->type; } - const type_t *const prev_type = skip_typeref(previous_declaration->type); + const type_t *prev_type = skip_typeref(previous_declaration->type); if (!types_compatible(type, prev_type)) { errorf(declaration->source_position, - "declaration '%#T' is incompatible with previous declaration '%#T'", - orig_type, symbol, previous_declaration->type, symbol); - errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol); + "declaration '%#T' is incompatible with " + "previous declaration '%#T'", + orig_type, symbol, previous_declaration->type, symbol); + errorf(previous_declaration->source_position, + "previous declaration of '%Y' was here", symbol); } else { - unsigned old_storage_class = previous_declaration->storage_class; + unsigned old_storage_class + = previous_declaration->storage_class; unsigned new_storage_class = declaration->storage_class; - /* pretend no storage class means extern for function declarations - * (except if the previous declaration is neither none nor extern) */ + if(is_type_incomplete(prev_type)) { + previous_declaration->type = type; + prev_type = type; + } + + /* pretend no storage class means extern for function + * declarations (except if the previous declaration is neither + * none nor extern) */ if (is_type_function(type)) { switch (old_storage_class) { case STORAGE_CLASS_NONE: @@ -2358,7 +2369,9 @@ static declaration_t *internal_record_declaration( if (warning.missing_prototypes && prev_type->function.unspecified_parameters && !is_sym_main(symbol)) { - warningf(declaration->source_position, "no previous prototype for '%#T'", orig_type, symbol); + warningf(declaration->source_position, + "no previous prototype for '%#T'", + orig_type, symbol); } } else if (new_storage_class == STORAGE_CLASS_NONE) { new_storage_class = STORAGE_CLASS_EXTERN; @@ -2373,14 +2386,20 @@ static declaration_t *internal_record_declaration( new_storage_class == STORAGE_CLASS_EXTERN) { warn_redundant_declaration: if (warning.redundant_decls) { - warningf(declaration->source_position, "redundant declaration for '%Y'", symbol); - warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol); + warningf(declaration->source_position, + "redundant declaration for '%Y'", symbol); + warningf(previous_declaration->source_position, + "previous declaration of '%Y' was here", + symbol); } } else if (current_function == NULL) { if (old_storage_class != STORAGE_CLASS_STATIC && new_storage_class == STORAGE_CLASS_STATIC) { - errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol); - errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol); + errorf(declaration->source_position, + "static declaration of '%Y' follows non-static declaration", + symbol); + errorf(previous_declaration->source_position, + "previous declaration of '%Y' was here", symbol); } else { if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) { goto warn_redundant_declaration; @@ -2391,11 +2410,15 @@ warn_redundant_declaration: } } else { if (old_storage_class == new_storage_class) { - errorf(declaration->source_position, "redeclaration of '%Y'", symbol); + errorf(declaration->source_position, + "redeclaration of '%Y'", symbol); } else { - errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol); + errorf(declaration->source_position, + "redeclaration of '%Y' with different linkage", + symbol); } - errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol); + errorf(previous_declaration->source_position, + "previous declaration of '%Y' was here", symbol); } } return previous_declaration; @@ -2403,9 +2426,12 @@ warn_redundant_declaration: } else if (is_function_definition) { if (declaration->storage_class != STORAGE_CLASS_STATIC) { if (warning.missing_prototypes && !is_sym_main(symbol)) { - warningf(declaration->source_position, "no previous prototype for '%#T'", orig_type, symbol); + warningf(declaration->source_position, + "no previous prototype for '%#T'", orig_type, symbol); } else if (warning.missing_declarations && !is_sym_main(symbol)) { - warningf(declaration->source_position, "no previous declaration for '%#T'", orig_type, symbol); + warningf(declaration->source_position, + "no previous declaration for '%#T'", orig_type, + symbol); } } } else if (warning.missing_declarations && @@ -2414,7 +2440,8 @@ warn_redundant_declaration: declaration->storage_class == STORAGE_CLASS_NONE || declaration->storage_class == STORAGE_CLASS_THREAD )) { - warningf(declaration->source_position, "no previous declaration for '%#T'", orig_type, symbol); + warningf(declaration->source_position, + "no previous declaration for '%#T'", orig_type, symbol); } assert(declaration->parent_scope == NULL); @@ -2486,7 +2513,7 @@ static void parse_init_declarator_rest(declaration_t *declaration) if(array_type->size == NULL) { expression_t *cnst = allocate_expression_zero(EXPR_CONST); - cnst->base.datatype = type_size_t; + cnst->base.type = type_size_t; switch (initializer->kind) { case INITIALIZER_LIST: { @@ -2742,8 +2769,8 @@ static void check_labels(void) label->used = true; if (label->source_position.input_name == NULL) { print_in_function(); - errorf(goto_statement->statement.source_position, - "label '%Y' used but not defined", label->symbol); + errorf(goto_statement->base.source_position, + "label '%Y' used but not defined", label->symbol); } } goto_first = goto_last = NULL; @@ -2756,7 +2783,7 @@ static void check_labels(void) if (! label->used) { print_in_function(); - warningf(label_statement->statement.source_position, + warningf(label_statement->base.source_position, "label '%Y' defined but not used", label->symbol); } } @@ -3020,9 +3047,9 @@ static expression_t *expected_expression_error(void) */ static expression_t *parse_string_const(void) { - expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL); - cnst->base.datatype = type_string; - cnst->string.value = parse_string_literals(); + expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL); + cnst->base.type = type_char_ptr; + cnst->string.value = parse_string_literals(); return cnst; } @@ -3033,8 +3060,8 @@ static expression_t *parse_string_const(void) static expression_t *parse_wide_string_const(void) { expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL); - cnst->base.datatype = type_wchar_t_ptr; - cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */ + cnst->base.type = type_wchar_t_ptr; + cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */ next_token(); return cnst; } @@ -3045,7 +3072,7 @@ static expression_t *parse_wide_string_const(void) static expression_t *parse_int_const(void) { expression_t *cnst = allocate_expression_zero(EXPR_CONST); - cnst->base.datatype = token.datatype; + cnst->base.type = token.datatype; cnst->conste.v.int_value = token.v.intvalue; next_token(); @@ -3059,7 +3086,7 @@ static expression_t *parse_int_const(void) static expression_t *parse_float_const(void) { expression_t *cnst = allocate_expression_zero(EXPR_CONST); - cnst->base.datatype = token.datatype; + cnst->base.type = token.datatype; cnst->conste.v.float_value = token.v.floatvalue; next_token(); @@ -3137,11 +3164,11 @@ static type_t *get_builtin_symbol_type(symbol_t *symbol) case T___builtin_alloca: return make_function_1_type(type_void_ptr, type_size_t); case T___builtin_nan: - return make_function_1_type(type_double, type_string); + return make_function_1_type(type_double, type_char_ptr); case T___builtin_nanf: - return make_function_1_type(type_float, type_string); + return make_function_1_type(type_float, type_char_ptr); case T___builtin_nand: - return make_function_1_type(type_long_double, type_string); + return make_function_1_type(type_long_double, type_char_ptr); case T___builtin_va_end: return make_function_1_type(type_void, type_valist); default: @@ -3184,7 +3211,7 @@ type_t *revert_automatic_type_conversion(const expression_t *expression) case EXPR_UNARY_DEREFERENCE: { const expression_t *const value = expression->unary.value; - type_t *const type = skip_typeref(value->base.datatype); + type_t *const type = skip_typeref(value->base.type); assert(is_type_pointer(type)); return type->pointer.points_to; } @@ -3193,8 +3220,8 @@ type_t *revert_automatic_type_conversion(const expression_t *expression) return get_builtin_symbol_type(expression->builtin_symbol.symbol); case EXPR_ARRAY_ACCESS: { - const expression_t *const array_ref = expression->array_access.array_ref; - type_t *const type_left = skip_typeref(array_ref->base.datatype); + const expression_t *array_ref = expression->array_access.array_ref; + type_t *type_left = skip_typeref(array_ref->base.type); if (!is_type_valid(type_left)) return type_left; assert(is_type_pointer(type_left)); @@ -3204,7 +3231,7 @@ type_t *revert_automatic_type_conversion(const expression_t *expression) default: break; } - return expression->base.datatype; + return expression->base.type; } static expression_t *parse_reference(void) @@ -3241,8 +3268,8 @@ static expression_t *parse_reference(void) * code to revert this! */ type = automatic_type_conversion(type); - ref->declaration = declaration; - ref->expression.datatype = type; + ref->declaration = declaration; + ref->base.type = type; /* this declaration is used */ declaration->used = true; @@ -3270,8 +3297,8 @@ static expression_t *parse_cast(void) check_cast_allowed(value, type); - cast->base.datatype = type; - cast->unary.value = value; + cast->base.type = type; + cast->unary.value = value; return cast; } @@ -3292,12 +3319,12 @@ static expression_t *parse_statement_expression(void) stmt = stmt->base.next; if (stmt->kind == STATEMENT_EXPRESSION) { - type = stmt->expression.expression->base.datatype; + type = stmt->expression.expression->base.type; } } else { warningf(expression->base.source_position, "empty statement expression ({})"); } - expression->base.datatype = type; + expression->base.type = type; expect(')'); @@ -3337,13 +3364,10 @@ static expression_t *parse_function_keyword(void) errorf(HERE, "'__func__' used outside of a function"); } - string_literal_expression_t *expression - = allocate_ast_zero(sizeof(expression[0])); - - expression->expression.kind = EXPR_FUNCTION; - expression->expression.datatype = type_string; + expression_t *expression = allocate_expression_zero(EXPR_FUNCTION); + expression->base.type = type_char_ptr; - return (expression_t*) expression; + return expression; } static expression_t *parse_pretty_function_keyword(void) @@ -3355,13 +3379,10 @@ static expression_t *parse_pretty_function_keyword(void) errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function"); } - string_literal_expression_t *expression - = allocate_ast_zero(sizeof(expression[0])); - - expression->expression.kind = EXPR_PRETTY_FUNCTION; - expression->expression.datatype = type_string; + expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION); + expression->base.type = type_char_ptr; - return (expression_t*) expression; + return expression; } static designator_t *parse_designator(void) @@ -3419,8 +3440,8 @@ static expression_t *parse_offsetof(void) { eat(T___builtin_offsetof); - expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF); - expression->base.datatype = type_size_t; + expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF); + expression->base.type = type_size_t; expect('('); expression->offsetofe.type = parse_typename(); @@ -3443,6 +3464,8 @@ static expression_t *parse_va_start(void) expression_t *const expr = parse_assignment_expression(); if (expr->kind == EXPR_REFERENCE) { declaration_t *const decl = expr->reference.declaration; + if (decl == NULL) + return create_invalid_expression(); if (decl->parent_scope == ¤t_function->scope && decl->next == NULL) { expression->va_starte.parameter = decl; @@ -3464,7 +3487,7 @@ static expression_t *parse_va_arg(void) expect('('); expression->va_arge.ap = parse_assignment_expression(); expect(','); - expression->base.datatype = parse_typename(); + expression->base.type = parse_typename(); expect(')'); return expression; @@ -3482,7 +3505,7 @@ static expression_t *parse_builtin_symbol(void) type_t *type = get_builtin_symbol_type(symbol); type = automatic_type_conversion(type); - expression->base.datatype = type; + expression->base.type = type; return expression; } @@ -3495,7 +3518,7 @@ static expression_t *parse_builtin_constant(void) expect('('); expression->builtin_constant.value = parse_assignment_expression(); expect(')'); - expression->base.datatype = type_int; + expression->base.type = type_int; return expression; } @@ -3517,7 +3540,7 @@ static expression_t *parse_builtin_prefetch(void) expression->builtin_prefetch.locality = parse_assignment_expression(); } expect(')'); - expression->base.datatype = type_void; + expression->base.type = type_void; return expression; } @@ -3558,12 +3581,12 @@ static expression_t *parse_compare_builtin(void) expression->binary.right = parse_assignment_expression(); expect(')'); - type_t *const orig_type_left = expression->binary.left->base.datatype; - type_t *const orig_type_right = expression->binary.right->base.datatype; + type_t *const orig_type_left = expression->binary.left->base.type; + type_t *const orig_type_right = expression->binary.right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); - if(!is_type_floating(type_left) && !is_type_floating(type_right)) { + if(!is_type_float(type_left) && !is_type_float(type_right)) { if (is_type_valid(type_left) && is_type_valid(type_right)) { type_error_incompatible("invalid operands in comparison", expression->base.source_position, orig_type_left, orig_type_right); @@ -3588,7 +3611,7 @@ static expression_t *parse_builtin_expect(void) expression->binary.right = parse_constant_expression(); expect(')'); - expression->base.datatype = expression->binary.left->base.datatype; + expression->base.type = expression->binary.left->base.type; return expression; } @@ -3603,21 +3626,7 @@ static expression_t *parse_assume(void) { expression->unary.value = parse_assignment_expression(); expect(')'); - expression->base.datatype = type_void; - return expression; -} - -static expression_t *parse_alignof(void) { - eat(T___alignof__); - - expression_t *expression - = allocate_expression_zero(EXPR_ALIGNOF); - - expect('('); - expression->alignofe.type = parse_typename(); - expect(')'); - - expression->base.datatype = type_size_t; + expression->base.type = type_void; return expression; } @@ -3662,8 +3671,6 @@ static expression_t *parse_primary_expression(void) return parse_builtin_constant(); case T___builtin_prefetch: return parse_builtin_prefetch(); - case T___alignof__: - return parse_alignof(); case T_assume: return parse_assume(); @@ -3681,7 +3688,7 @@ static expression_t *parse_primary_expression(void) * Check if the expression has the character type and issue a warning then. */ static void check_for_char_index_type(const expression_t *expression) { - type_t *const type = expression->base.datatype; + type_t *const type = expression->base.type; const type_t *const base_type = skip_typeref(type); if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) && @@ -3700,13 +3707,12 @@ static expression_t *parse_array_expression(unsigned precedence, expression_t *inside = parse_expression(); - array_access_expression_t *array_access - = allocate_ast_zero(sizeof(array_access[0])); + expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS); - array_access->expression.kind = EXPR_ARRAY_ACCESS; + array_access_expression_t *array_access = &expression->array_access; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_inside = inside->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_inside = inside->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_inside = skip_typeref(orig_type_inside); @@ -3735,38 +3741,46 @@ static expression_t *parse_array_expression(unsigned precedence, if(token.type != ']') { parse_error_expected("Problem while parsing array access", ']', 0); - return (expression_t*) array_access; + return expression; } next_token(); - return_type = automatic_type_conversion(return_type); - array_access->expression.datatype = return_type; + return_type = automatic_type_conversion(return_type); + expression->base.type = return_type; - return (expression_t*) array_access; + return expression; } -static expression_t *parse_sizeof(unsigned precedence) +static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence) { - eat(T_sizeof); - - sizeof_expression_t *sizeof_expression - = allocate_ast_zero(sizeof(sizeof_expression[0])); - sizeof_expression->expression.kind = EXPR_SIZEOF; - sizeof_expression->expression.datatype = type_size_t; + expression_t *tp_expression = allocate_expression_zero(kind); + tp_expression->base.type = type_size_t; if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) { next_token(); - sizeof_expression->type = parse_typename(); + tp_expression->typeprop.type = parse_typename(); expect(')'); } else { - expression_t *expression = parse_sub_expression(precedence); - expression->base.datatype = revert_automatic_type_conversion(expression); + expression_t *expression = parse_sub_expression(precedence); + expression->base.type = revert_automatic_type_conversion(expression); - sizeof_expression->type = expression->base.datatype; - sizeof_expression->size_expression = expression; + tp_expression->typeprop.type = expression->base.type; + tp_expression->typeprop.tp_expression = expression; } - return (expression_t*) sizeof_expression; + return tp_expression; +} + +static expression_t *parse_sizeof(unsigned precedence) +{ + eat(T_sizeof); + return parse_typeprop(EXPR_SIZEOF, precedence); +} + +static expression_t *parse_alignof(unsigned precedence) +{ + eat(T___alignof__); + return parse_typeprop(EXPR_SIZEOF, precedence); } static expression_t *parse_select_expression(unsigned precedence, @@ -3789,7 +3803,7 @@ static expression_t *parse_select_expression(unsigned precedence, select->select.symbol = symbol; next_token(); - type_t *const orig_type = compound->base.datatype; + type_t *const orig_type = compound->base.type; type_t *const type = skip_typeref(orig_type); type_t *type_left = type; @@ -3837,13 +3851,13 @@ static expression_t *parse_select_expression(unsigned precedence, type_t *expression_type = automatic_type_conversion(iter->type); select->select.compound_entry = iter; - select->base.datatype = expression_type; + select->base.type = expression_type; if(expression_type->kind == TYPE_BITFIELD) { expression_t *extract = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT); - extract->unary.value = select; - extract->base.datatype = expression_type->bitfield.base; + extract->unary.value = select; + extract->base.type = expression_type->bitfield.base; return extract; } @@ -3865,7 +3879,7 @@ static expression_t *parse_call_expression(unsigned precedence, call_expression_t *call = &result->call; call->function = expression; - type_t *const orig_type = expression->base.datatype; + type_t *const orig_type = expression->base.type; type_t *const type = skip_typeref(orig_type); function_type_t *function_type = NULL; @@ -3873,8 +3887,8 @@ static expression_t *parse_call_expression(unsigned precedence, type_t *const to_type = skip_typeref(type->pointer.points_to); if (is_type_function(to_type)) { - function_type = &to_type->function; - call->expression.datatype = function_type->return_type; + function_type = &to_type->function; + call->base.type = function_type->return_type; } } @@ -3919,7 +3933,7 @@ static expression_t *parse_call_expression(unsigned precedence, /* TODO improve error message */ errorf(arg_expr->base.source_position, "Cannot call function with argument '%E' of type '%T' where type '%T' is expected", - arg_expr, arg_expr->base.datatype, expected_type); + arg_expr, arg_expr->base.type, expected_type); } else { argument->expression = create_implicit_cast(argument->expression, expected_type); } @@ -3935,7 +3949,7 @@ static expression_t *parse_call_expression(unsigned precedence, } else { /* do default promotion */ for( ; argument != NULL; argument = argument->next) { - type_t *type = argument->expression->base.datatype; + type_t *type = argument->expression->base.type; type = skip_typeref(type); if(is_type_integer(type)) { @@ -3984,7 +3998,7 @@ static expression_t *parse_conditional_expression(unsigned precedence, conditional->condition = expression; /* 6.5.15.2 */ - type_t *const condition_type_orig = expression->base.datatype; + type_t *const condition_type_orig = expression->base.type; type_t *const condition_type = skip_typeref(condition_type_orig); if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) { type_error("expected a scalar type in conditional condition", @@ -3998,8 +4012,8 @@ static expression_t *parse_conditional_expression(unsigned precedence, conditional->true_expression = true_expression; conditional->false_expression = false_expression; - type_t *const orig_true_type = true_expression->base.datatype; - type_t *const orig_false_type = false_expression->base.datatype; + type_t *const orig_true_type = true_expression->base.type; + type_t *const orig_false_type = false_expression->base.type; type_t *const true_type = skip_typeref(orig_true_type); type_t *const false_type = skip_typeref(orig_false_type); @@ -4011,9 +4025,9 @@ static expression_t *parse_conditional_expression(unsigned precedence, true_expression = create_implicit_cast(true_expression, result_type); false_expression = create_implicit_cast(false_expression, result_type); - conditional->true_expression = true_expression; - conditional->false_expression = false_expression; - conditional->expression.datatype = result_type; + conditional->true_expression = true_expression; + conditional->false_expression = false_expression; + conditional->base.type = result_type; } else if (same_compound_type(true_type, false_type) || ( is_type_atomic(true_type, ATOMIC_TYPE_VOID) && is_type_atomic(false_type, ATOMIC_TYPE_VOID) @@ -4034,7 +4048,7 @@ static expression_t *parse_conditional_expression(unsigned precedence, result_type = type_error_type; } - conditional->expression.datatype = result_type; + conditional->base.type = result_type; return result; } @@ -4055,8 +4069,8 @@ static expression_t *parse_builtin_classify_type(const unsigned precedence) { eat(T___builtin_classify_type); - expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE); - result->base.datatype = type_int; + expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE); + result->base.type = type_int; expect('('); expression_t *expression = parse_sub_expression(precedence); @@ -4068,7 +4082,7 @@ static expression_t *parse_builtin_classify_type(const unsigned precedence) static void semantic_incdec(unary_expression_t *expression) { - type_t *const orig_type = expression->value->base.datatype; + type_t *const orig_type = expression->value->base.type; type_t *const type = skip_typeref(orig_type); /* TODO !is_type_real && !is_type_pointer */ if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) { @@ -4079,12 +4093,12 @@ static void semantic_incdec(unary_expression_t *expression) return; } - expression->expression.datatype = orig_type; + expression->base.type = orig_type; } static void semantic_unexpr_arithmetic(unary_expression_t *expression) { - type_t *const orig_type = expression->value->base.datatype; + type_t *const orig_type = expression->value->base.type; type_t *const type = skip_typeref(orig_type); if(!is_type_arithmetic(type)) { if (is_type_valid(type)) { @@ -4094,12 +4108,12 @@ static void semantic_unexpr_arithmetic(unary_expression_t *expression) return; } - expression->expression.datatype = orig_type; + expression->base.type = orig_type; } static void semantic_unexpr_scalar(unary_expression_t *expression) { - type_t *const orig_type = expression->value->base.datatype; + type_t *const orig_type = expression->value->base.type; type_t *const type = skip_typeref(orig_type); if (!is_type_scalar(type)) { if (is_type_valid(type)) { @@ -4108,12 +4122,12 @@ static void semantic_unexpr_scalar(unary_expression_t *expression) return; } - expression->expression.datatype = orig_type; + expression->base.type = orig_type; } static void semantic_unexpr_integer(unary_expression_t *expression) { - type_t *const orig_type = expression->value->base.datatype; + type_t *const orig_type = expression->value->base.type; type_t *const type = skip_typeref(orig_type); if (!is_type_integer(type)) { if (is_type_valid(type)) { @@ -4122,12 +4136,12 @@ static void semantic_unexpr_integer(unary_expression_t *expression) return; } - expression->expression.datatype = orig_type; + expression->base.type = orig_type; } static void semantic_dereference(unary_expression_t *expression) { - type_t *const orig_type = expression->value->base.datatype; + type_t *const orig_type = expression->value->base.type; type_t *const type = skip_typeref(orig_type); if(!is_type_pointer(type)) { if (is_type_valid(type)) { @@ -4136,9 +4150,9 @@ static void semantic_dereference(unary_expression_t *expression) return; } - type_t *result_type = type->pointer.points_to; - result_type = automatic_type_conversion(result_type); - expression->expression.datatype = result_type; + type_t *result_type = type->pointer.points_to; + result_type = automatic_type_conversion(result_type); + expression->base.type = result_type; } /** @@ -4146,10 +4160,10 @@ static void semantic_dereference(unary_expression_t *expression) */ static void semantic_take_addr(unary_expression_t *expression) { - expression_t *value = expression->value; - value->base.datatype = revert_automatic_type_conversion(value); + expression_t *value = expression->value; + value->base.type = revert_automatic_type_conversion(value); - type_t *orig_type = value->base.datatype; + type_t *orig_type = value->base.type; if(!is_type_valid(orig_type)) return; @@ -4157,7 +4171,7 @@ static void semantic_take_addr(unary_expression_t *expression) declaration_t *const declaration = value->reference.declaration; if(declaration != NULL) { if (declaration->storage_class == STORAGE_CLASS_REGISTER) { - errorf(expression->expression.source_position, + errorf(expression->base.source_position, "address of register variable '%Y' requested", declaration->symbol); } @@ -4165,7 +4179,7 @@ static void semantic_take_addr(unary_expression_t *expression) } } - expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE); + expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE); } #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \ @@ -4269,8 +4283,8 @@ static void semantic_binexpr_arithmetic(binary_expression_t *expression) { expression_t *const left = expression->left; expression_t *const right = expression->right; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_right = right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); @@ -4283,17 +4297,17 @@ static void semantic_binexpr_arithmetic(binary_expression_t *expression) } type_t *arithmetic_type = semantic_arithmetic(type_left, type_right); - expression->left = create_implicit_cast(left, arithmetic_type); - expression->right = create_implicit_cast(right, arithmetic_type); - expression->expression.datatype = arithmetic_type; + expression->left = create_implicit_cast(left, arithmetic_type); + expression->right = create_implicit_cast(right, arithmetic_type); + expression->base.type = arithmetic_type; } static void semantic_shift_op(binary_expression_t *expression) { expression_t *const left = expression->left; expression_t *const right = expression->right; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_right = right->base.type; type_t * type_left = skip_typeref(orig_type_left); type_t * type_right = skip_typeref(orig_type_right); @@ -4308,17 +4322,17 @@ static void semantic_shift_op(binary_expression_t *expression) type_left = promote_integer(type_left); type_right = promote_integer(type_right); - expression->left = create_implicit_cast(left, type_left); - expression->right = create_implicit_cast(right, type_right); - expression->expression.datatype = type_left; + expression->left = create_implicit_cast(left, type_left); + expression->right = create_implicit_cast(right, type_right); + expression->base.type = type_left; } static void semantic_add(binary_expression_t *expression) { expression_t *const left = expression->left; expression_t *const right = expression->right; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_right = right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); @@ -4327,12 +4341,12 @@ static void semantic_add(binary_expression_t *expression) type_t *arithmetic_type = semantic_arithmetic(type_left, type_right); expression->left = create_implicit_cast(left, arithmetic_type); expression->right = create_implicit_cast(right, arithmetic_type); - expression->expression.datatype = arithmetic_type; + expression->base.type = arithmetic_type; return; } else if(is_type_pointer(type_left) && is_type_integer(type_right)) { - expression->expression.datatype = type_left; + expression->base.type = type_left; } else if(is_type_pointer(type_right) && is_type_integer(type_left)) { - expression->expression.datatype = type_right; + expression->base.type = type_right; } else if (is_type_valid(type_left) && is_type_valid(type_right)) { errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right); } @@ -4342,28 +4356,31 @@ static void semantic_sub(binary_expression_t *expression) { expression_t *const left = expression->left; expression_t *const right = expression->right; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_right = right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); /* § 5.6.5 */ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) { type_t *arithmetic_type = semantic_arithmetic(type_left, type_right); - expression->left = create_implicit_cast(left, arithmetic_type); - expression->right = create_implicit_cast(right, arithmetic_type); - expression->expression.datatype = arithmetic_type; + expression->left = create_implicit_cast(left, arithmetic_type); + expression->right = create_implicit_cast(right, arithmetic_type); + expression->base.type = arithmetic_type; return; } else if(is_type_pointer(type_left) && is_type_integer(type_right)) { - expression->expression.datatype = type_left; + expression->base.type = type_left; } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) { if(!pointers_compatible(type_left, type_right)) { - errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right); + errorf(HERE, + "pointers to incompatible objects to binary '-' ('%T', '%T')", + orig_type_left, orig_type_right); } else { - expression->expression.datatype = type_ptrdiff_t; + expression->base.type = type_ptrdiff_t; } } else if (is_type_valid(type_left) && is_type_valid(type_right)) { - errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right); + errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", + orig_type_left, orig_type_right); } } @@ -4376,23 +4393,30 @@ static void semantic_comparison(binary_expression_t *expression) { expression_t *left = expression->left; expression_t *right = expression->right; - type_t *orig_type_left = left->base.datatype; - type_t *orig_type_right = right->base.datatype; + type_t *orig_type_left = left->base.type; + type_t *orig_type_right = right->base.type; type_t *type_left = skip_typeref(orig_type_left); type_t *type_right = skip_typeref(orig_type_right); /* TODO non-arithmetic types */ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) { + if (warning.sign_compare && + (expression->base.kind != EXPR_BINARY_EQUAL && + expression->base.kind != EXPR_BINARY_NOTEQUAL) && + (is_type_signed(type_left) != is_type_signed(type_right))) { + warningf(expression->base.source_position, + "comparison between signed and unsigned"); + } type_t *arithmetic_type = semantic_arithmetic(type_left, type_right); - expression->left = create_implicit_cast(left, arithmetic_type); - expression->right = create_implicit_cast(right, arithmetic_type); - expression->expression.datatype = arithmetic_type; + expression->left = create_implicit_cast(left, arithmetic_type); + expression->right = create_implicit_cast(right, arithmetic_type); + expression->base.type = arithmetic_type; if (warning.float_equal && - (expression->expression.kind == EXPR_BINARY_EQUAL || - expression->expression.kind == EXPR_BINARY_NOTEQUAL) && - is_type_floating(arithmetic_type)) { - warningf(expression->expression.source_position, + (expression->base.kind == EXPR_BINARY_EQUAL || + expression->base.kind == EXPR_BINARY_NOTEQUAL) && + is_type_float(arithmetic_type)) { + warningf(expression->base.source_position, "comparing floating point with == or != is unsafe"); } } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) { @@ -4403,18 +4427,18 @@ static void semantic_comparison(binary_expression_t *expression) expression->left = create_implicit_cast(left, type_right); } else if (is_type_valid(type_left) && is_type_valid(type_right)) { type_error_incompatible("invalid operands in comparison", - expression->expression.source_position, + expression->base.source_position, type_left, type_right); } - expression->expression.datatype = type_int; + expression->base.type = type_int; } static void semantic_arithmetic_assign(binary_expression_t *expression) { expression_t *left = expression->left; expression_t *right = expression->right; - type_t *orig_type_left = left->base.datatype; - type_t *orig_type_right = right->base.datatype; + type_t *orig_type_left = left->base.type; + type_t *orig_type_right = right->base.type; type_t *type_left = skip_typeref(orig_type_left); type_t *type_right = skip_typeref(orig_type_right); @@ -4433,15 +4457,15 @@ static void semantic_arithmetic_assign(binary_expression_t *expression) * for the arithmetic operation and create a cast by itself */ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right); expression->right = create_implicit_cast(right, arithmetic_type); - expression->expression.datatype = type_left; + expression->base.type = type_left; } static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression) { expression_t *const left = expression->left; expression_t *const right = expression->right; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_right = right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); @@ -4451,10 +4475,10 @@ static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression) * The ast2firm pass has to know that left_type must be right_type * for the arithmetic operation and create a cast by itself */ type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right); - expression->right = create_implicit_cast(right, arithmetic_type); - expression->expression.datatype = type_left; + expression->right = create_implicit_cast(right, arithmetic_type); + expression->base.type = type_left; } else if (is_type_pointer(type_left) && is_type_integer(type_right)) { - expression->expression.datatype = type_left; + expression->base.type = type_left; } else if (is_type_valid(type_left) && is_type_valid(type_right)) { errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right); } @@ -4467,8 +4491,8 @@ static void semantic_logical_op(binary_expression_t *expression) { expression_t *const left = expression->left; expression_t *const right = expression->right; - type_t *const orig_type_left = left->base.datatype; - type_t *const orig_type_right = right->base.datatype; + type_t *const orig_type_left = left->base.type; + type_t *const orig_type_right = right->base.type; type_t *const type_left = skip_typeref(orig_type_left); type_t *const type_right = skip_typeref(orig_type_right); @@ -4480,7 +4504,7 @@ static void semantic_logical_op(binary_expression_t *expression) return; } - expression->expression.datatype = type_int; + expression->base.type = type_int; } /** @@ -4509,7 +4533,7 @@ static bool has_const_fields(const compound_type_t *type) static void semantic_binexpr_assign(binary_expression_t *expression) { expression_t *left = expression->left; - type_t *orig_type_left = left->base.datatype; + type_t *orig_type_left = left->base.type; type_t *type_left = revert_automatic_type_conversion(left); type_left = skip_typeref(orig_type_left); @@ -4539,14 +4563,14 @@ static void semantic_binexpr_assign(binary_expression_t *expression) type_t *const res_type = semantic_assign(orig_type_left, expression->right, "assignment"); if (res_type == NULL) { - errorf(expression->expression.source_position, + errorf(expression->base.source_position, "cannot assign to '%T' from '%T'", - orig_type_left, expression->right->base.datatype); + orig_type_left, expression->right->base.type); } else { expression->right = create_implicit_cast(expression->right, res_type); } - expression->expression.datatype = orig_type_left; + expression->base.type = orig_type_left; } static bool expression_has_effect(const expression_t *const expr) @@ -4601,7 +4625,7 @@ static bool expression_has_effect(const expression_t *const expr) case EXPR_UNARY_PREFIX_INCREMENT: return true; case EXPR_UNARY_PREFIX_DECREMENT: return true; case EXPR_UNARY_CAST: - return is_type_atomic(expr->base.datatype, ATOMIC_TYPE_VOID); + return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID); case EXPR_UNARY_CAST_IMPLICIT: return true; case EXPR_UNARY_ASSUME: return true; case EXPR_UNARY_BITFIELD_EXTRACT: return false; @@ -4658,7 +4682,7 @@ static void semantic_comma(binary_expression_t *expression) warningf(left->base.source_position, "left-hand operand of comma expression has no effect"); } } - expression->expression.datatype = expression->right->base.datatype; + expression->base.type = expression->right->base.type; } #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \ @@ -4887,7 +4911,8 @@ static void init_expression_parsers(void) T_PLUSPLUS, 25); register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT, T_MINUSMINUS, 25); - register_expression_parser(parse_sizeof, T_sizeof, 25); + register_expression_parser(parse_sizeof, T_sizeof, 25); + register_expression_parser(parse_alignof, T___alignof__, 25); register_expression_parser(parse_extension, T___extension__, 25); register_expression_parser(parse_builtin_classify_type, T___builtin_classify_type, 25); @@ -5038,7 +5063,7 @@ static statement_t *parse_case_statement(void) "case label not within a switch statement"); } } - statement->case_label.label_statement = parse_statement(); + statement->case_label.statement = parse_statement(); return statement; } @@ -5074,7 +5099,7 @@ static statement_t *parse_default_statement(void) const case_label_statement_t *def_label = find_default_label(current_switch); if (def_label != NULL) { errorf(HERE, "multiple default labels in one switch"); - errorf(def_label->statement.source_position, + errorf(def_label->base.source_position, "this is the first default label"); } else { /* link all cases into the switch statement */ @@ -5089,7 +5114,7 @@ static statement_t *parse_default_statement(void) errorf(statement->base.source_position, "'default' label not within a switch statement"); } - statement->label.label_statement = parse_statement(); + statement->label.statement = parse_statement(); return statement; } @@ -5139,18 +5164,17 @@ static statement_t *parse_label_statement(void) label->source_position = token.source_position; } - label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0])); + statement_t *statement = allocate_statement_zero(STATEMENT_LABEL); - label_statement->statement.kind = STATEMENT_LABEL; - label_statement->statement.source_position = token.source_position; - label_statement->label = label; + statement->base.source_position = token.source_position; + statement->label.label = label; eat(':'); if(token.type == '}') { /* TODO only warn? */ errorf(HERE, "label at end of compound statement"); - return (statement_t*) label_statement; + return statement; } else { if (token.type == ';') { /* eat an empty statement here, to avoid the warning about an empty @@ -5158,18 +5182,19 @@ static statement_t *parse_label_statement(void) * a }. */ next_token(); } else { - label_statement->label_statement = parse_statement(); + statement->label.statement = parse_statement(); } } /* remember the labels's in a list for later checking */ if (label_last == NULL) { - label_first = label_last = label_statement; + label_first = &statement->label; } else { - label_last->next = label_statement; + label_last->next = &statement->label; } + label_last = &statement->label; - return (statement_t*) label_statement; + return statement; } /** @@ -5179,21 +5204,20 @@ static statement_t *parse_if(void) { eat(T_if); - if_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - statement->statement.kind = STATEMENT_IF; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_IF); + statement->base.source_position = token.source_position; expect('('); - statement->condition = parse_expression(); + statement->ifs.condition = parse_expression(); expect(')'); - statement->true_statement = parse_statement(); + statement->ifs.true_statement = parse_statement(); if(token.type == T_else) { next_token(); - statement->false_statement = parse_statement(); + statement->ifs.false_statement = parse_statement(); } - return (statement_t*) statement; + return statement; } /** @@ -5203,39 +5227,42 @@ static statement_t *parse_switch(void) { eat(T_switch); - switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - statement->statement.kind = STATEMENT_SWITCH; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH); + statement->base.source_position = token.source_position; expect('('); expression_t *const expr = parse_expression(); - type_t * type = skip_typeref(expr->base.datatype); + type_t * type = skip_typeref(expr->base.type); if (is_type_integer(type)) { type = promote_integer(type); } else if (is_type_valid(type)) { - errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type); + errorf(expr->base.source_position, + "switch quantity is not an integer, but '%T'", type); type = type_error_type; } - statement->expression = create_implicit_cast(expr, type); + statement->switchs.expression = create_implicit_cast(expr, type); expect(')'); switch_statement_t *rem = current_switch; - current_switch = statement; - statement->body = parse_statement(); - current_switch = rem; + current_switch = &statement->switchs; + statement->switchs.body = parse_statement(); + current_switch = rem; - if (warning.switch_default && find_default_label(statement) == NULL) { - warningf(statement->statement.source_position, "switch has no default case"); + if (warning.switch_default + && find_default_label(&statement->switchs) == NULL) { + warningf(statement->base.source_position, "switch has no default case"); } - return (statement_t*) statement; + return statement; } static statement_t *parse_loop_body(statement_t *const loop) { statement_t *const rem = current_loop; current_loop = loop; + statement_t *const body = parse_statement(); + current_loop = rem; return body; } @@ -5247,17 +5274,16 @@ static statement_t *parse_while(void) { eat(T_while); - while_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - statement->statement.kind = STATEMENT_WHILE; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_WHILE); + statement->base.source_position = token.source_position; expect('('); - statement->condition = parse_expression(); + statement->whiles.condition = parse_expression(); expect(')'); - statement->body = parse_loop_body((statement_t*)statement); + statement->whiles.body = parse_loop_body(statement); - return (statement_t*) statement; + return statement; } /** @@ -5267,18 +5293,19 @@ static statement_t *parse_do(void) { eat(T_do); - do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - statement->statement.kind = STATEMENT_DO_WHILE; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE); + + statement->base.source_position = token.source_position; + + statement->do_while.body = parse_loop_body(statement); - statement->body = parse_loop_body((statement_t*)statement); expect(T_while); expect('('); - statement->condition = parse_expression(); + statement->do_while.condition = parse_expression(); expect(')'); expect(';'); - return (statement_t*) statement; + return statement; } /** @@ -5288,21 +5315,20 @@ static statement_t *parse_for(void) { eat(T_for); - for_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - statement->statement.kind = STATEMENT_FOR; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_FOR); + statement->base.source_position = token.source_position; expect('('); int top = environment_top(); scope_t *last_scope = scope; - set_scope(&statement->scope); + set_scope(&statement->fors.scope); if(token.type != ';') { if(is_declaration_specifier(&token, false)) { parse_declaration(record_declaration); } else { - statement->initialisation = parse_expression(); + statement->fors.initialisation = parse_expression(); expect(';'); } } else { @@ -5310,20 +5336,20 @@ static statement_t *parse_for(void) } if(token.type != ';') { - statement->condition = parse_expression(); + statement->fors.condition = parse_expression(); } expect(';'); if(token.type != ')') { - statement->step = parse_expression(); + statement->fors.step = parse_expression(); } expect(')'); - statement->body = parse_loop_body((statement_t*)statement); + statement->fors.body = parse_loop_body(statement); - assert(scope == &statement->scope); + assert(scope == &statement->fors.scope); set_scope(last_scope); environment_pop_to(top); - return (statement_t*) statement; + return statement; } /** @@ -5343,23 +5369,22 @@ static statement_t *parse_goto(void) declaration_t *label = get_label(symbol); - goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - - statement->statement.kind = STATEMENT_GOTO; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_GOTO); + statement->base.source_position = token.source_position; - statement->label = label; + statement->gotos.label = label; /* remember the goto's in a list for later checking */ if (goto_last == NULL) { - goto_first = goto_last = statement; + goto_first = &statement->gotos; } else { - goto_last->next = statement; + goto_last->next = &statement->gotos; } + goto_last = &statement->gotos; expect(';'); - return (statement_t*) statement; + return statement; } /** @@ -5424,6 +5449,31 @@ static bool is_local_var_declaration(const declaration_t *declaration) { } } +/** + * Check if a given declaration represents a variable. + */ +static bool is_var_declaration(const declaration_t *declaration) { + switch ((storage_class_tag_t) declaration->storage_class) { + case STORAGE_CLASS_NONE: + case STORAGE_CLASS_EXTERN: + case STORAGE_CLASS_STATIC: + case STORAGE_CLASS_AUTO: + case STORAGE_CLASS_REGISTER: + case STORAGE_CLASS_THREAD: + case STORAGE_CLASS_THREAD_EXTERN: + case STORAGE_CLASS_THREAD_STATIC: { + const type_t *type = skip_typeref(declaration->type); + if(is_type_function(type)) { + return false; + } else { + return true; + } + } + default: + return false; + } +} + /** * Check if a given expression represents a local variable. */ @@ -5436,6 +5486,21 @@ static bool is_local_variable(const expression_t *expression) return is_local_var_declaration(declaration); } +/** + * Check if a given expression represents a local variable and + * return its declaration then, else return NULL. + */ +declaration_t *expr_is_variable(const expression_t *expression) +{ + if (expression->base.kind != EXPR_REFERENCE) { + return NULL; + } + declaration_t *declaration = expression->reference.declaration; + if (is_var_declaration(declaration)) + return declaration; + return NULL; +} + /** * Parse a return statement. */ @@ -5443,10 +5508,8 @@ static statement_t *parse_return(void) { eat(T_return); - return_statement_t *statement = allocate_ast_zero(sizeof(statement[0])); - - statement->statement.kind = STATEMENT_RETURN; - statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_RETURN); + statement->base.source_position = token.source_position; expression_t *return_value = NULL; if(token.type != ';') { @@ -5459,20 +5522,20 @@ static statement_t *parse_return(void) type_t *const return_type = skip_typeref(func_type->function.return_type); if(return_value != NULL) { - type_t *return_value_type = skip_typeref(return_value->base.datatype); + type_t *return_value_type = skip_typeref(return_value->base.type); if(is_type_atomic(return_type, ATOMIC_TYPE_VOID) && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) { - warningf(statement->statement.source_position, - "'return' with a value, in function returning void"); + warningf(statement->base.source_position, + "'return' with a value, in function returning void"); return_value = NULL; } else { type_t *const res_type = semantic_assign(return_type, return_value, "'return'"); if (res_type == NULL) { - errorf(statement->statement.source_position, - "cannot return something of type '%T' in function returning '%T'", - return_value->base.datatype, return_type); + errorf(statement->base.source_position, + "cannot return something of type '%T' in function returning '%T'", + return_value->base.type, return_type); } else { return_value = create_implicit_cast(return_value, res_type); } @@ -5481,19 +5544,19 @@ static statement_t *parse_return(void) if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) { const expression_t *expression = return_value->unary.value; if (is_local_variable(expression)) { - warningf(statement->statement.source_position, - "function returns address of local variable"); + warningf(statement->base.source_position, + "function returns address of local variable"); } } } else { if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) { - warningf(statement->statement.source_position, - "'return' without value, in function returning non-void"); + warningf(statement->base.source_position, + "'return' without value, in function returning non-void"); } } - statement->return_value = return_value; + statement->returns.value = return_value; - return (statement_t*) statement; + return statement; } /** @@ -5650,47 +5713,47 @@ static statement_t *parse_statement(void) */ static statement_t *parse_compound_statement(void) { - compound_statement_t *const compound_statement - = allocate_ast_zero(sizeof(compound_statement[0])); - compound_statement->statement.kind = STATEMENT_COMPOUND; - compound_statement->statement.source_position = token.source_position; + statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND); + + statement->base.source_position = token.source_position; eat('{'); int top = environment_top(); scope_t *last_scope = scope; - set_scope(&compound_statement->scope); + set_scope(&statement->compound.scope); statement_t *last_statement = NULL; while(token.type != '}' && token.type != T_EOF) { - statement_t *statement = parse_statement(); - if(statement == NULL) + statement_t *sub_statement = parse_statement(); + if(sub_statement == NULL) continue; if(last_statement != NULL) { - last_statement->base.next = statement; + last_statement->base.next = sub_statement; } else { - compound_statement->statements = statement; + statement->compound.statements = sub_statement; } - while(statement->base.next != NULL) - statement = statement->base.next; + while(sub_statement->base.next != NULL) + sub_statement = sub_statement->base.next; - last_statement = statement; + last_statement = sub_statement; } if(token.type == '}') { next_token(); } else { - errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'"); + errorf(statement->base.source_position, + "end of file while looking for closing '}'"); } - assert(scope == &compound_statement->scope); + assert(scope == &statement->compound.scope); set_scope(last_scope); environment_pop_to(top); - return (statement_t*) compound_statement; + return statement; } /** @@ -5714,29 +5777,33 @@ static void initialize_builtin_types(void) } /** - * Check for unused functions in the given scope. + * Check for unused global static functions and variables */ -static void check_unused_functions(const scope_t *scope) { - bool first_err = true; - const declaration_t *declaration = scope->declarations; +static void check_unused_globals(void) +{ + if (!warning.unused_function && !warning.unused_variable) + return; - for (; declaration != NULL; declaration = declaration->next) { - if (! declaration->used) { - if (declaration->storage_class == STORAGE_CLASS_STATIC) { - const type_t *type = declaration->type; + for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) { + if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC) + continue; - if (is_type_function(type)) { - if (first_err) { - first_err = false; - diagnosticf("%s: At top level:\n", - declaration->source_position.input_name); - } - warningf(declaration->source_position, - "'%Y' defined but not used", - declaration->symbol); - } - } + type_t *const type = decl->type; + const char *s; + if (is_type_function(skip_typeref(type))) { + if (!warning.unused_function || decl->is_inline) + continue; + + s = (decl->init.statement != NULL ? "defined" : "declared"); + } else { + if (!warning.unused_variable) + continue; + + s = "defined"; } + + warningf(decl->source_position, "'%#T' %s but not used", + type, decl->symbol, s); } } @@ -5770,9 +5837,7 @@ static translation_unit_t *parse_translation_unit(void) last_declaration = NULL; assert(global_scope == &unit->scope); - if (warning.unused_function) { - check_unused_functions(global_scope); - } + check_unused_globals(); global_scope = NULL; return unit;