fix(bootstrap): avoid emitting malformed Slice_T* typedefs
- Strip pointer suffix when scanning for slice types in C backend - Skip slice typedefs already emitted from module.structs Fixes _test_slice
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
## Emits clean, well-structured C code from LIR instructions.
|
## Emits clean, well-structured C code from LIR instructions.
|
||||||
## Since LIR is already linear and low-level, C emission is straightforward.
|
## Since LIR is already linear and low-level, C emission is straightforward.
|
||||||
|
|
||||||
import std/[strutils, strformat, tables, sequtils]
|
import std/[strutils, strformat, tables, sequtils, sets]
|
||||||
import lir, hir, types, token
|
import lir, hir, types, token
|
||||||
|
|
||||||
type
|
type
|
||||||
@@ -570,12 +570,18 @@ proc emitModule*(be: var LirCBackend, builder: LirBuilder, module: HirModule): s
|
|||||||
# Slice types (collect from functions/structs)
|
# Slice types (collect from functions/structs)
|
||||||
# Simple: scan function params/returns for slice types
|
# Simple: scan function params/returns for slice types
|
||||||
var sliceTypes: seq[tuple[name: string, elem: string]] = @[]
|
var sliceTypes: seq[tuple[name: string, elem: string]] = @[]
|
||||||
|
var structNames: HashSet[string]
|
||||||
|
for s in module.structs:
|
||||||
|
structNames.incl(s.name)
|
||||||
for f in module.funcs:
|
for f in module.funcs:
|
||||||
for p in f.params:
|
for p in f.params:
|
||||||
let ct = typeToCStr(p.typ)
|
var ct = typeToCStr(p.typ)
|
||||||
|
# Strip pointer/reference suffix to find the base slice type.
|
||||||
|
while ct.endsWith("*"):
|
||||||
|
ct = ct[0..^2]
|
||||||
if ct.startsWith("Slice_"):
|
if ct.startsWith("Slice_"):
|
||||||
let elem = ct[6 .. ^1]
|
let elem = ct[6 .. ^1]
|
||||||
if not sliceTypes.anyIt(it.name == ct):
|
if not sliceTypes.anyIt(it.name == ct) and not structNames.contains(ct):
|
||||||
sliceTypes.add((ct, elem))
|
sliceTypes.add((ct, elem))
|
||||||
if sliceTypes.len > 0:
|
if sliceTypes.len > 0:
|
||||||
for st in sliceTypes:
|
for st in sliceTypes:
|
||||||
|
|||||||
Reference in New Issue
Block a user