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 */")
+3 -3
View File
@@ -1086,11 +1086,11 @@ proc checkExpr(sema: var Sema, expr: Expr, scope: Scope): Type =
elif sym.decl.kind == dkEnum:
# Algebraic enum fields
if expr.exprFieldName == "tag":
return makeNamed(obj.name & "_Tag")
return makeNamed(objType.name & "_Tag")
elif expr.exprFieldName == "data":
return makeNamed(obj.name & "_Data")
return makeNamed(objType.name & "_Data")
else:
sema.emitError(expr.loc, &"enum '{obj.name}' has no field '{expr.exprFieldName}'")
sema.emitError(expr.loc, &"enum '{objType.name}' has no field '{expr.exprFieldName}'")
elif sym.decl.kind == dkUnion:
# Union fields
for f in sym.decl.declUnionFields: