move to nominal typing for some types

This commit is contained in:
tocariimaa 2025-01-20 19:30:22 -03:00
parent 6ff868a646
commit 71fb88602f

View file

@ -56,8 +56,6 @@ typedef struct {
static const Symbol InvalidSymbol = {.kind = SymInvalid};
static const DataType *InvalidDataType = &(DataType){.kind = DtkInvalid};
static DataTypeCheck
datatype_struct_cmp(SemaCtx *sctx, DataType *s1, DataType *s2);
static DataTypeCheck
datatype_cmp(SemaCtx *sctx, DataType *dt1, DataType *dt2);
static DataType *
@ -414,7 +412,7 @@ sema_expr_list(SemaCtx *sctx, Vec(Ast *) exprs, Location loc)
/* Structurally compare two structural data types. */
static DataTypeCheck
datatype_struct_cmp(SemaCtx *sctx, DataType *s1, DataType *s2)
datatype_struct_can_cast(SemaCtx *sctx, DataType *s1, DataType *s2)
{
compiler_assert(sctx->cm, s1->kind == DtkStruct && s2->kind == DtkStruct);
const DataTypeCompound *s1s = &s1->compound;
@ -444,7 +442,7 @@ datatype_array_cmp(SemaCtx *sctx, DataType *a1, DataType *a2)
}
static DataTypeCheck
datatype_proc_cmp(SemaCtx *sctx, DataType *pc1, DataType *pc2)
datatype_proc_can_cast(SemaCtx *sctx, DataType *pc1, DataType *pc2)
{
DataTypeCheck tchk = {.ok = true};
@ -484,7 +482,7 @@ datatype_cmp(SemaCtx *sctx, DataType *dt1, DataType *dt2)
if (dt1 == nil || dt2 == nil)
return (DataTypeCheck){false, Sl("")};
/* TODO: return more information in case of a mismatch... */
if (dt1 == dt2) /* shallow */
if (dt1 == dt2) /* shallow comparison */
return (DataTypeCheck){.ok = true};
if (dt1->kind != dt2->kind)
return (DataTypeCheck){.ok = false};
@ -492,16 +490,17 @@ datatype_cmp(SemaCtx *sctx, DataType *dt1, DataType *dt2)
switch (dt1->kind) {
case DtkBasic:
return datatype_basic_cmp(sctx, dt1, dt2);
case DtkStruct:
case DtkUnion:
return datatype_struct_cmp(sctx, dt1, dt2);
case DtkProc:
return datatype_proc_cmp(sctx, dt1, dt2);
case DtkArray:
return datatype_array_cmp(sctx, dt1, dt2);
/* Nominally typed, should have been catched by the shallow comparison above */
case DtkBool:
case DtkVoid:
return (DataTypeCheck){.ok = true};
case DtkStruct:
case DtkUnion:
case DtkProc:
break;
case DtkInvalid:
unreachable();
}
return (DataTypeCheck){.ok = false};
}