Avoid any constants that have the "untyped list" type but isn't a CONST_UNTYPED_LIST.

This commit is contained in:
Christoffer Lerno
2024-08-16 21:49:21 +02:00
parent 16cb756d3f
commit 74b8da1e15
4 changed files with 13 additions and 26 deletions

View File

@@ -3440,6 +3440,7 @@ INLINE void expr_rewrite_const_untyped_list(Expr *expr, Expr **elements)
INLINE void expr_rewrite_const_initializer(Expr *expr, Type *type, ConstInitializer *initializer)
{
assert(type != type_untypedlist);
expr->expr_kind = EXPR_CONST;
expr->type = type;
expr->const_expr = (ExprConst) { .initializer = initializer, .const_kind = CONST_INITIALIZER };

View File

@@ -806,11 +806,6 @@ static bool rule_arrptr_to_slice(CastContext *cc, bool is_explicit, bool is_sile
static bool rule_ulist_to_struct(CastContext *cc, bool is_explicit, bool is_silent)
{
if (expr_is_const_initializer(cc->expr))
{
assert(cc->expr->const_expr.initializer->kind == CONST_INIT_ZERO);
return true;
}
assert(expr_is_const_untyped_list(cc->expr));
Expr **expressions = cc->expr->const_expr.untyped_list;
unsigned size = vec_size(expressions);
@@ -854,11 +849,7 @@ static bool rule_ulist_to_vecarr(CastContext *cc, bool is_explicit, bool is_sile
static bool rule_ulist_to_slice(CastContext *cc, bool is_explicit, bool is_silent)
{
if (cc->expr->const_expr.const_kind == CONST_INITIALIZER)
{
assert(cc->expr->const_expr.initializer->kind == CONST_INIT_ZERO);
return true;
}
assert(expr_is_const_untyped_list(cc->expr));
Type *base = cc->to->array.base;
FOREACH(Expr *, expr, cc->expr->const_expr.untyped_list)
{
@@ -1801,19 +1792,9 @@ static void cast_vec_to_vec(SemaContext *context, Expr *expr, Type *to_type)
static void cast_untyped_list_to_other(SemaContext *context, Expr *expr, Type *to_type)
{
if (expr->const_expr.const_kind == CONST_UNTYPED_LIST)
{
// Recursively set the type of all ConstInitializer inside.
expr_recursively_rewrite_untyped_list(expr, expr->const_expr.untyped_list);
}
if (expr_is_const_initializer(expr) && expr->const_expr.initializer->kind == CONST_INIT_ZERO)
{
expr->type = to_type;
expr->inner_expr->type = type_flatten(to_type);
expr->resolve_status = RESOLVE_DONE;
return;
}
expr->resolve_status = RESOLVE_NOT_DONE;
assert(expr_is_const_untyped_list(expr));
// Recursively set the type of all ConstInitializer inside.
expr_recursively_rewrite_untyped_list(expr, expr->const_expr.untyped_list);
// We can now analyse the list (this is where the actual check happens)
bool success = sema_expr_analyse_initializer_list(context, type_flatten(to_type), expr);
assert(success);

View File

@@ -7451,7 +7451,7 @@ static inline bool sema_expr_analyse_ct_concat(SemaContext *context, Expr *conca
ConstInitializer *init = single_expr->const_expr.initializer;
if (init->kind != CONST_INIT_ARRAY_FULL)
{
if (init->kind == CONST_INIT_ZERO && init->type == type_untypedlist) continue;
assert(init->type != type_untypedlist);
RETURN_SEMA_ERROR(single_expr, "Expected a full array here.");
}
FOREACH(ConstInitializer *, val, init->init_array_full)
@@ -9374,8 +9374,8 @@ static inline bool sema_expr_analyse_ct_append(SemaContext *context, Expr *appen
switch (list->const_expr.const_kind)
{
case CONST_INITIALIZER:
if (list->type != type_untypedlist) return sema_append_const_array(context, append_expr, list, exprs);
break;
assert(list->type != type_untypedlist);
return sema_append_const_array(context, append_expr, list, exprs);
case CONST_UNTYPED_LIST:
untyped_list = list->const_expr.untyped_list;
break;

View File

@@ -498,6 +498,11 @@ static inline bool sema_expr_analyse_initializer(SemaContext *context, Type *ass
SEMA_ERROR(expr, "Zero length arrays / vectors are not permitted.");
return false;
}
if (flattened == type_untypedlist)
{
expr_rewrite_const_untyped_list(expr, NULL);
return true;
}
ConstInitializer *initializer = CALLOCS(ConstInitializer);
initializer->kind = CONST_INIT_ZERO;
initializer->type = flattened;