diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-11 12:29:48 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-11 12:29:48 -0400 |
| commit | 210179ee672a0c3799328a54e886f574b3823e3d (patch) | |
| tree | 25ddb95503c537c19b9fdb7614df2f16c4012d21 /builtins | |
| parent | dee3742b48e27ef36637d004163286d3352b0763 (diff) | |
Optional enums (deprecated custom tag values)
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/functions.c | 20 | ||||
| -rw-r--r-- | builtins/nextline.c | 2 | ||||
| -rw-r--r-- | builtins/optionals.c | 5 | ||||
| -rw-r--r-- | builtins/types.c | 4 | ||||
| -rw-r--r-- | builtins/types.h | 6 |
5 files changed, 20 insertions, 17 deletions
diff --git a/builtins/functions.c b/builtins/functions.c index d6778d4c..ed0889d4 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -117,8 +117,8 @@ PUREFUNC public uint64_t generic_hash(const void *obj, const TypeInfo *type) case OptionalInfo: { errx(1, "Optional hash not implemented"); } - case EmptyStruct: return 0; - case CustomInfo: + case EmptyStructInfo: return 0; + case CustomInfo: case StructInfo: case EnumInfo: // These all share the same info if (!type->CustomInfo.hash) goto hash_data; return type->CustomInfo.hash(obj, type); @@ -142,8 +142,8 @@ PUREFUNC public int32_t generic_compare(const void *x, const void *y, const Type case OptionalInfo: { errx(1, "Optional compare not implemented"); } - case EmptyStruct: return 0; - case CustomInfo: + case EmptyStructInfo: return 0; + case CustomInfo: case StructInfo: case EnumInfo: // These all share the same info if (!type->CustomInfo.compare) goto compare_data; return type->CustomInfo.compare(x, y, type); @@ -163,11 +163,11 @@ PUREFUNC public bool generic_equal(const void *x, const void *y, const TypeInfo case ArrayInfo: return Array$equal(x, y, type); case ChannelInfo: return Channel$equal((const channel_t**)x, (const channel_t**)y, type); case TableInfo: return Table$equal(x, y, type); - case EmptyStruct: return true; + case EmptyStructInfo: return true; case OptionalInfo: { errx(1, "Optional equal not implemented"); } - case CustomInfo: + case CustomInfo: case StructInfo: case EnumInfo: // These all share the same info if (!type->CustomInfo.equal) goto use_generic_compare; return type->CustomInfo.equal(x, y, type); @@ -188,10 +188,10 @@ public Text_t generic_as_text(const void *obj, bool colorize, const TypeInfo *ty case TableInfo: return Table$as_text(obj, colorize, type); case TypeInfoInfo: return Type$as_text(obj, colorize, type); case OptionalInfo: return Optional$as_text(obj, colorize, type); - case EmptyStruct: return colorize ? - Text$concat(Text("\x1b[0;1m"), Text$from_str(type->EmptyStruct.name), Text("\x1b[m()")) - : Text$concat(Text$from_str(type->EmptyStruct.name), Text("()")); - case CustomInfo: + case EmptyStructInfo: return colorize ? + Text$concat(Text("\x1b[0;1m"), Text$from_str(type->EmptyStructInfo.name), Text("\x1b[m()")) + : Text$concat(Text$from_str(type->EmptyStructInfo.name), Text("()")); + case CustomInfo: case StructInfo: case EnumInfo: // These all share the same info if (!type->CustomInfo.as_text) fail("No text function provided for type!\n"); return type->CustomInfo.as_text(obj, colorize, type); diff --git a/builtins/nextline.c b/builtins/nextline.c index 3fa37412..b7928939 100644 --- a/builtins/nextline.c +++ b/builtins/nextline.c @@ -91,7 +91,7 @@ static uint64_t NextLine$hash(const NextLine_t *obj, const TypeInfo *info) return siphash24((void *) &hashes, sizeof(hashes)); } -public const TypeInfo NextLine$Done = { 0, 0, {.tag = EmptyStruct,.EmptyStruct.name = +public const TypeInfo NextLine$Done = { 0, 0, {.tag = EmptyStructInfo,.EmptyStructInfo.name = "NextLine$Done" } }; public const TypeInfo NextLine$Next = { 24, 8, {.tag = CustomInfo,.CustomInfo = {.as_text = diff --git a/builtins/optionals.c b/builtins/optionals.c index f58c8134..88f08097 100644 --- a/builtins/optionals.c +++ b/builtins/optionals.c @@ -36,12 +36,15 @@ static inline bool is_null(const void *obj, const TypeInfo *non_optional_type) case ArrayInfo: return ((Array_t*)obj)->length < 0; case TableInfo: return ((Table_t*)obj)->entries.length < 0; case FunctionInfo: return *(void**)obj == NULL; - case CustomInfo: { + case StructInfo: { int64_t offset = non_optional_type->size; if (offset % non_optional_type->align) offset += non_optional_type->align - (offset % non_optional_type->align); return *(bool*)(obj + offset); } + case EnumInfo: { + return (*(int*)obj) == 0; // NULL tag + } default: { Text_t t = generic_as_text(NULL, false, non_optional_type); errx(1, "is_null() not implemented for: %k", &t); diff --git a/builtins/types.c b/builtins/types.c index 79f65f71..512fdfc3 100644 --- a/builtins/types.c +++ b/builtins/types.c @@ -32,8 +32,8 @@ public const TypeInfo TypeInfo$info = { .TypeInfoInfo.type_str="TypeInfo", }; -public const TypeInfo Void$info = {.size=0, .align=0, .tag=EmptyStruct}; -public const TypeInfo Abort$info = {.size=0, .align=0, .tag=EmptyStruct}; +public const TypeInfo Void$info = {.size=0, .align=0, .tag=EmptyStructInfo}; +public const TypeInfo Abort$info = {.size=0, .align=0, .tag=EmptyStructInfo}; public Text_t Func$as_text(const void *fn, bool colorize, const TypeInfo *type) { diff --git a/builtins/types.h b/builtins/types.h index a6cc40c6..38d8bdc5 100644 --- a/builtins/types.h +++ b/builtins/types.h @@ -17,8 +17,8 @@ typedef Text_t (*text_fn_t)(const void*, bool, const struct TypeInfo*); typedef struct TypeInfo { int64_t size, align; struct { // Anonymous tagged union for convenience - enum { CustomInfo, PointerInfo, TextInfo, ArrayInfo, ChannelInfo, TableInfo, FunctionInfo, - OptionalInfo, TypeInfoInfo, OpaqueInfo, EmptyStruct } tag; + enum { CustomInfo, StructInfo, EnumInfo, PointerInfo, TextInfo, ArrayInfo, ChannelInfo, TableInfo, FunctionInfo, + OptionalInfo, TypeInfoInfo, OpaqueInfo, EmptyStructInfo } tag; union { struct { equal_fn_t equal; @@ -52,7 +52,7 @@ typedef struct TypeInfo { struct {} OpaqueInfo; struct { const char *name; - } EmptyStruct; + } EmptyStructInfo; }; }; } TypeInfo; |
