fix: algebraic enum C backend and sema field type resolution

- Fix sema.nim: use objType.name instead of obj.name for algebraic enum
  tag/data field types (was generating _Tag instead of EnumName_Tag)
- Fix c_backend.nim: emit enum definitions before struct definitions
  to allow structs to reference algebraic enum types
This commit is contained in:
2026-06-05 22:47:37 +03:00
parent 154579b30d
commit 1f64f4707e
2 changed files with 15 additions and 7 deletions
+12 -4
View File
@@ -616,14 +616,22 @@ proc emitModule*(be: var CBackend, module: HirModule): string =
be.emitLine(&"/* const {c.name} (complex expression) */")
be.emitLine("")
# Forward declarations for all structs
for s in module.structs:
be.emitLine(&"typedef struct {s.name} {s.name};")
if module.structs.len > 0:
be.emitLine("")
# Enum definitions (must come before structs that reference them)
for e in module.enums:
be.emitEnum(e.name, e.variants)
if module.enums.len > 0:
be.emitLine("")
# Struct definitions
for s in module.structs:
be.emitStruct(s.name, s.fields)
# Enum definitions
for e in module.enums:
be.emitEnum(e.name, e.variants)
# Slice fat-pointer typedefs
if sliceTypes.len > 0:
be.emitLine("/* Slice types */")