Allow String constants -> ichar*, and allow integer pointers to explicitly convert between unsigned signed.

This commit is contained in:
Christoffer Lerno
2024-03-15 23:08:52 +01:00
parent 656202dc0d
commit 3df988d0b8
4 changed files with 26 additions and 0 deletions

View File

@@ -23,6 +23,7 @@
- Regression: no stacktrace.
- For MacOS, running with higher optimization would crash as initializers were removed.
- `compile-run` and `run` now returns the proper return code.
- Allow String constants -> ichar*, and allow integer pointers to explicitly convert between unsigned signed.
### Stdlib changes
- Added `new_aligned` and `alloc_aligned` functions to prevent accidental under-alignment when allocating simd.

View File

@@ -2430,6 +2430,7 @@ Type *type_get_vector(Type *vector_type, unsigned len);
Type *type_get_vector_bool(Type *original_type);
Type *type_int_signed_by_bitsize(BitSize bitsize);
Type *type_int_unsigned_by_bitsize(BitSize bit_size);
bool type_is_matching_int(CanonicalType *type1, CanonicalType *type2);
TypeSize type_size(Type *type); // Only call after all types are resolved.
void type_init_cint(void);
Type *type_new_func(Decl *decl, Signature *sig);
@@ -2454,6 +2455,7 @@ typedef enum
{
TYPE_MISMATCH = 0,
TYPE_SAME = 1,
TYPE_SAME_INT_SIZE = 2,
TYPE_ERROR = -1,
} TypeCmpResult;

View File

@@ -683,6 +683,7 @@ static bool rule_ptr_to_ptr(CastContext *cc, bool is_explicit, bool is_silent)
case TYPE_ERROR:
return false;
case TYPE_MISMATCH:
case TYPE_SAME_INT_SIZE:
return sema_cast_error(cc, true, is_silent);
}
UNREACHABLE
@@ -764,6 +765,9 @@ static bool rule_arrptr_to_sa(CastContext *cc, bool is_explicit, bool is_silent)
{
case TYPE_SAME:
return true;
case TYPE_SAME_INT_SIZE:
if (is_explicit) return true;
break;
case TYPE_ERROR:
return false;
case TYPE_MISMATCH:
@@ -849,6 +853,9 @@ static bool rule_sa_to_ptr(CastContext *cc, bool is_explicit, bool is_silent)
{
case TYPE_SAME:
return true;
case TYPE_SAME_INT_SIZE:
if (is_explicit || expr_is_const_string(cc->expr)) return true;
break;
case TYPE_ERROR:
return false;
case TYPE_MISMATCH:

View File

@@ -268,6 +268,17 @@ const char *type_to_error_string(Type *type)
}
bool type_is_matching_int(CanonicalType *type1, CanonicalType *type2)
{
assert(type1->canonical == type1 && type2->canonical == type2);
TypeKind typekind1 = type1->type_kind;
TypeKind typekind2 = type2->type_kind;
if (typekind1 == typekind2) return type_kind_is_any_integer(typekind1);
if (type_kind_is_signed(typekind1)) return typekind1 + (TYPE_U8 - TYPE_I8) == typekind2;
if (type_kind_is_unsigned(typekind1)) return typekind2 + (TYPE_U8 - TYPE_I8) == typekind1;
return false;
}
TypeSize type_size(Type *type)
{
RETRY:
@@ -1790,6 +1801,9 @@ TypeCmpResult type_array_element_is_equivalent(SemaContext *context, Type *eleme
case TYPE_INFERRED_ARRAY:
case TYPE_INFERRED_VECTOR:
return type_array_is_equivalent(context, element1, element2, is_explicit);
case ALL_INTS:
if (type_is_matching_int(element1, element2)) return TYPE_SAME_INT_SIZE;
FALLTHROUGH;
default:
return TYPE_MISMATCH;
}
@@ -1824,6 +1838,8 @@ RETRY:
if (to_pointee->type_kind != from_pointee->type_kind)
{
if (type_is_matching_int(to_pointee, from_pointee)) return TYPE_SAME_INT_SIZE;
if (type_is_any_arraylike(from_pointee))
{
// Try array equivalence.