X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=parser.c;h=d1221c66c9b0ceb9a3fbabc00bae38d131076f19;hb=582d43e9816467c5f2562371c9d59ee2de5154be;hp=c769d34b8da13b6522d5f5ac6d34b534be421b10;hpb=78cc65b14fc5c53a16368fe5975c70c83e969b19;p=cparser diff --git a/parser.c b/parser.c index c769d34..d1221c6 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); @@ -743,10 +744,15 @@ static type_t *semantic_assign(type_t *orig_type_left, points_to_left = get_unqualified_type(points_to_left); points_to_right = get_unqualified_type(points_to_right); - if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) - && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID) - && !types_compatible(points_to_left, points_to_right)) { - return NULL; + if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) || + is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) { + return orig_type_left; + } + + if (!types_compatible(points_to_left, points_to_right)) { + warningf(right->base.source_position, + "destination type '%T' in %s is incompatible with '%E' of type '%T'", + orig_type_left, context, right, orig_type_right); } return orig_type_left; @@ -947,7 +953,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 +1201,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 +1428,7 @@ restart: type = parse_typename(); } else { expression = parse_expression(); - type = expression->base.datatype; + type = expression->base.type; } break; @@ -1432,7 +1438,7 @@ restart: default: expression = parse_expression(); - type = expression->base.datatype; + type = expression->base.type; break; } @@ -1609,7 +1615,7 @@ static void parse_declaration_specifiers(declaration_specifiers_t *specifiers) break; } case T_union: { - type = allocate_type_zero(TYPE_COMPOUND_STRUCT); + type = allocate_type_zero(TYPE_COMPOUND_UNION); type->compound.declaration = parse_compound_type_specifier(false); break; @@ -1804,6 +1810,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 +2320,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 +2343,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 +2374,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 +2391,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 +2415,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 +2431,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 +2445,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 +2518,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: { @@ -2713,13 +2745,27 @@ static void parse_kr_declaration_list(declaration_t *declaration) declaration->type = type; } +static bool first_err = true; + +/** + * When called with first_err set, prints the name of the current function, + * else does noting. + */ +static void print_in_function(void) { + if (first_err) { + first_err = false; + diagnosticf("%s: In function '%Y':\n", + current_function->source_position.input_name, + current_function->symbol); + } +} + /** * Check if all labels are defined in the current function. * Check if all labels are used in the current function. */ static void check_labels(void) { - bool first_err = true; for (const goto_statement_t *goto_statement = goto_first; goto_statement != NULL; goto_statement = goto_statement->next) { @@ -2727,14 +2773,9 @@ static void check_labels(void) label->used = true; if (label->source_position.input_name == NULL) { - if (first_err) { - first_err = false; - diagnosticf("%s: In function '%Y':\n", - current_function->source_position.input_name, - current_function->symbol); - } - errorf(goto_statement->statement.source_position, - "label '%Y' used but not defined", label->symbol); + print_in_function(); + errorf(goto_statement->base.source_position, + "label '%Y' used but not defined", label->symbol); } } goto_first = goto_last = NULL; @@ -2746,13 +2787,8 @@ static void check_labels(void) const declaration_t *label = label_statement->label; if (! label->used) { - if (first_err) { - first_err = false; - diagnosticf("%s: In function '%Y':\n", - current_function->source_position.input_name, - current_function->symbol); - } - warningf(label_statement->statement.source_position, + print_in_function(); + warningf(label_statement->base.source_position, "label '%Y' defined but not used", label->symbol); } } @@ -2760,6 +2796,27 @@ static void check_labels(void) label_first = label_last = NULL; } +/** + * Check declarations of current_function for unused entities. + */ +static void check_declarations(void) +{ + if (warning.unused_parameter) { + const scope_t *scope = ¤t_function->scope; + + const declaration_t *parameter = scope->declarations; + for (; parameter != NULL; parameter = parameter->next) { + if (! parameter->used) { + print_in_function(); + warningf(parameter->source_position, + "unused parameter '%Y'", parameter->symbol); + } + } + } + if (warning.unused_variable) { + } +} + static void parse_external_declaration(void) { /* function-definitions and declarations both start with declaration @@ -2851,7 +2908,9 @@ static void parse_external_declaration(void) current_function = declaration; declaration->init.statement = parse_compound_statement(); + first_err = true; check_labels(); + check_declarations(); assert(current_function == declaration); current_function = old_current_function; @@ -2993,9 +3052,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; } @@ -3006,8 +3065,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; } @@ -3018,7 +3077,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(); @@ -3032,7 +3091,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(); @@ -3110,11 +3169,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: @@ -3157,7 +3216,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; } @@ -3166,8 +3225,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)); @@ -3177,7 +3236,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) @@ -3214,8 +3273,11 @@ 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; return expression; } @@ -3240,8 +3302,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; } @@ -3262,12 +3324,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(')'); @@ -3307,13 +3369,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) @@ -3325,13 +3384,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) @@ -3389,8 +3445,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(); @@ -3413,6 +3469,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; @@ -3434,7 +3492,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; @@ -3452,7 +3510,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; } @@ -3465,7 +3523,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; } @@ -3487,7 +3545,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; } @@ -3519,6 +3577,7 @@ static expression_t *parse_compare_builtin(void) panic("invalid compare builtin found"); break; } + expression->base.source_position = HERE; next_token(); expect('('); @@ -3527,15 +3586,15 @@ 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", - token.source_position, orig_type_left, orig_type_right); + expression->base.source_position, orig_type_left, orig_type_right); } } else { semantic_comparison(&expression->binary); @@ -3557,7 +3616,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; } @@ -3572,21 +3631,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; } @@ -3616,8 +3661,10 @@ static expression_t *parse_primary_expression(void) return parse_va_arg(); case T___builtin_expect: return parse_builtin_expect(); - case T___builtin_nanf: case T___builtin_alloca: + case T___builtin_nan: + case T___builtin_nand: + case T___builtin_nanf: case T___builtin_va_end: return parse_builtin_symbol(); case T___builtin_isgreater: @@ -3631,8 +3678,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(); @@ -3650,7 +3695,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) && @@ -3669,13 +3714,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); @@ -3704,38 +3748,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, @@ -3758,7 +3810,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; @@ -3806,13 +3858,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; } @@ -3834,7 +3886,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; @@ -3842,8 +3894,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; } } @@ -3888,7 +3940,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); } @@ -3904,7 +3956,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)) { @@ -3953,7 +4005,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", @@ -3967,8 +4019,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); @@ -3980,9 +4032,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) @@ -4003,7 +4055,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; } @@ -4024,8 +4076,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); @@ -4037,7 +4089,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) { @@ -4048,12 +4100,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)) { @@ -4063,12 +4115,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)) { @@ -4077,12 +4129,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)) { @@ -4091,12 +4143,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)) { @@ -4105,9 +4157,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; } /** @@ -4115,10 +4167,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; @@ -4126,7 +4178,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); } @@ -4134,7 +4186,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) \ @@ -4238,8 +4290,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); @@ -4252,17 +4304,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); @@ -4277,17 +4329,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); @@ -4296,12 +4348,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); } @@ -4311,28 +4363,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); } } @@ -4345,23 +4400,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)) { @@ -4372,18 +4434,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); @@ -4402,15 +4464,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); @@ -4420,10 +4482,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); } @@ -4436,8 +4498,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); @@ -4449,7 +4511,7 @@ static void semantic_logical_op(binary_expression_t *expression) return; } - expression->expression.datatype = type_int; + expression->base.type = type_int; } /** @@ -4478,7 +4540,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); @@ -4508,14 +4570,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) @@ -4570,7 +4632,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; @@ -4627,7 +4689,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) \ @@ -4635,10 +4697,12 @@ static expression_t *parse_##binexpression_type(unsigned precedence, \ expression_t *left) \ { \ eat(token_type); \ + source_position_t pos = HERE; \ \ expression_t *right = parse_sub_expression(precedence + lr); \ \ expression_t *binexpr = allocate_expression_zero(binexpression_type); \ + binexpr->base.source_position = pos; \ binexpr->binary.left = left; \ binexpr->binary.right = right; \ sfunc(&binexpr->binary); \ @@ -4854,7 +4918,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); @@ -5005,7 +5070,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; } @@ -5041,7 +5106,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 */ @@ -5056,7 +5121,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; } @@ -5106,18 +5171,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 @@ -5125,18 +5189,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; } /** @@ -5146,21 +5211,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; } /** @@ -5170,39 +5234,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; } @@ -5214,17 +5281,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; } /** @@ -5234,18 +5300,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; } /** @@ -5255,21 +5322,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 { @@ -5277,20 +5343,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; } /** @@ -5310,23 +5376,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; } /** @@ -5391,6 +5456,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. */ @@ -5403,6 +5493,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. */ @@ -5410,10 +5515,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 != ';') { @@ -5426,20 +5529,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); } @@ -5448,19 +5551,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; } /** @@ -5617,47 +5720,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; } /** @@ -5680,6 +5783,37 @@ static void initialize_builtin_types(void) type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE); } +/** + * Check for unused global static functions and variables + */ +static void check_unused_globals(void) +{ + if (!warning.unused_function && !warning.unused_variable) + return; + + for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) { + if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC) + continue; + + 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); + } +} + /** * Parse a translation unit. */ @@ -5710,6 +5844,7 @@ static translation_unit_t *parse_translation_unit(void) last_declaration = NULL; assert(global_scope == &unit->scope); + check_unused_globals(); global_scope = NULL; return unit;