Phase 8.2.4: Slice fat pointer + bounds checking

- T[] slices are now fat pointers {data: T*, len: size_t} in C backend
- Added hSliceIndex HIR node for slice indexing (distinct from hIndexPtr for raw pointers)
- Added exprIndexBoundsCheck flag to AST ekIndex
- Sema sets bounds-check flag in @[Checked] functions
- C backend emits Slice_<T> typedefs and compound literals for slice init
- bux_bounds_check() called automatically on slice indexing in @[Checked] funcs
- Unchecked functions allow raw access (C behavior) for performance
This commit is contained in:
2026-06-01 11:50:45 +03:00
parent bf9e73d56e
commit cefd2a8442
6 changed files with 85 additions and 24 deletions
+9 -1
View File
@@ -625,6 +625,13 @@ proc lowerExpr(ctx: var LowerCtx, expr: Expr): HirNode =
of ekIndex:
let base = ctx.lowerExpr(expr.exprIndexObj)
let idx = ctx.lowerExpr(expr.exprIndexIdx)
let baseType = ctx.resolveExprType(expr.exprIndexObj)
if baseType.isSlice:
let sliceIdx = HirNode(kind: hSliceIndex, sliceIndexBase: base,
sliceIndexIndex: idx,
sliceIndexBoundsCheck: expr.exprIndexBoundsCheck,
typ: typ, loc: loc)
return sliceIdx
let basePtr = HirNode(kind: hIndexPtr, indexPtrBase: base,
indexPtrIndex: idx, typ: makePointer(typ), loc: loc)
return HirNode(kind: hLoad, loadPtr: basePtr, typ: typ, loc: loc)
@@ -655,7 +662,8 @@ proc lowerExpr(ctx: var LowerCtx, expr: Expr): HirNode =
var elems: seq[HirNode] = @[]
for e in expr.exprSliceElements:
elems.add(ctx.lowerExpr(e))
return HirNode(kind: hSliceInit, sliceInitElements: elems, typ: typ, loc: loc)
return HirNode(kind: hSliceInit, sliceInitElements: elems,
sliceInitLen: elems.len, typ: typ, loc: loc)
of ekRange:
let lo = ctx.lowerExpr(expr.exprRangeLo)