diff options
author | Araq <rumpf_a@web.de> | 2018-03-24 20:26:20 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2018-03-24 20:26:34 +0100 |
commit | 1d9343080de8e19835c3f6568630ba759afbb94f (patch) | |
tree | 95649a7fecf7c5aaff39c5d2136aaac8306ba8b0 /compiler/ccgexprs.nim | |
parent | 121b9e26fb9d1ae6037c806dbb12a3ae0e26ded6 (diff) | |
download | Nim-1d9343080de8e19835c3f6568630ba759afbb94f.tar.gz |
added toOpenArray builtin for zero-copy slices; syntax sugar yet to come
Diffstat (limited to 'compiler/ccgexprs.nim')
-rw-r--r-- | compiler/ccgexprs.nim | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 6ea56d71d..dc06c8482 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -880,6 +880,23 @@ proc genCStringElem(p: BProc, n, x, y: PNode, d: var TLoc) = putIntoDest(p, d, n, rfmt(nil, "$1[$2]", rdLoc(a), rdCharLoc(b)), a.storage) +proc genIndexCheck(p: BProc; arr, idx: TLoc) = + let ty = skipTypes(arr.t, abstractVarRange) + case ty.kind + of tyOpenArray, tyVarargs: + linefmt(p, cpsStmts, "if ((NU)($1) >= (NU)($2Len_0)) #raiseIndexError();$n", + rdLoc(idx), rdLoc(arr)) + of tyArray: + let first = intLiteral(firstOrd(ty)) + if tfUncheckedArray notin ty.flags: + linefmt(p, cpsStmts, "if ($1 < $2 || $1 > $3) #raiseIndexError();$n", + rdCharLoc(idx), first, intLiteral(lastOrd(ty))) + of tySequence, tyString: + linefmt(p, cpsStmts, + "if ((NU)($1) >= (NU)($2->$3)) #raiseIndexError();$n", + rdLoc(idx), rdLoc(arr), lenField(p)) + else: discard + proc genOpenArrayElem(p: BProc, n, x, y: PNode, d: var TLoc) = var a, b: TLoc initLocExpr(p, x, a) |