This commit is contained in:
Andrii Riabushenko 2018-12-05 19:29:14 +00:00
commit 938d3ffad7
2 changed files with 29 additions and 15 deletions

View file

@ -413,7 +413,7 @@ proc passCopyToSink(n: PNode; c: var Con): PNode =
proc pArg(arg: PNode; c: var Con; isSink: bool): PNode = proc pArg(arg: PNode; c: var Con; isSink: bool): PNode =
template pArgIfTyped(arg_part: PNode): PNode = template pArgIfTyped(arg_part: PNode): PNode =
# typ is nil if we are in if/case expr branch with noreturn # typ is nil if we are in if/case expr branch with noreturn
if arg_part.typ == nil: copyTree(arg_part) if arg_part.typ == nil: p(arg_part, c)
else: pArg(arg_part, c, isSink) else: pArg(arg_part, c, isSink)
if isSink: if isSink:
@ -478,7 +478,7 @@ proc pArg(arg: PNode; c: var Con; isSink: bool): PNode =
proc moveOrCopy(dest, ri: PNode; c: var Con): PNode = proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
template moveOrCopyIfTyped(ri_part: PNode): PNode = template moveOrCopyIfTyped(ri_part: PNode): PNode =
# typ is nil if we are in if/case expr branch with noreturn # typ is nil if we are in if/case expr branch with noreturn
if ri_part.typ == nil: copyTree(ri_part) if ri_part.typ == nil: p(ri_part, c)
else: moveOrCopy(dest, ri_part, c) else: moveOrCopy(dest, ri_part, c)
case ri.kind case ri.kind
@ -537,29 +537,32 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
result.add branch result.add branch
of nkBracket: of nkBracket:
# array constructor # array constructor
result = genSink(c, dest.typ, dest, ri)
let ri2 = copyTree(ri) let ri2 = copyTree(ri)
for i in 0..<ri.len: for i in 0..<ri.len:
# everything that is passed to an array constructor is consumed, # everything that is passed to an array constructor is consumed,
# so these all act like 'sink' parameters: # so these all act like 'sink' parameters:
ri2[i] = pArg(ri[i], c, isSink = true) ri2[i] = pArg(ri[i], c, isSink = true)
result = genSink(c, dest.typ, dest, ri2) result.add ri2
of nkObjConstr: of nkObjConstr:
result = genSink(c, dest.typ, dest, ri)
let ri2 = copyTree(ri) let ri2 = copyTree(ri)
for i in 1..<ri.len: for i in 1..<ri.len:
# everything that is passed to an object constructor is consumed, # everything that is passed to an object constructor is consumed,
# so these all act like 'sink' parameters: # so these all act like 'sink' parameters:
ri2[i][1] = pArg(ri[i][1], c, isSink = true) ri2[i].sons[1] = pArg(ri[i][1], c, isSink = true)
result = genSink(c, dest.typ, dest, ri2) result.add ri2
of nkTupleConstr: of nkTupleConstr:
result = genSink(c, dest.typ, dest, ri)
let ri2 = copyTree(ri) let ri2 = copyTree(ri)
for i in 0..<ri.len: for i in 0..<ri.len:
# everything that is passed to an tuple constructor is consumed, # everything that is passed to an tuple constructor is consumed,
# so these all act like 'sink' parameters: # so these all act like 'sink' parameters:
if ri[i].kind == nkExprColonExpr: if ri[i].kind == nkExprColonExpr:
ri2[i][1] = pArg(ri[i][1], c, isSink = true) ri2[i].sons[1] = pArg(ri[i][1], c, isSink = true)
else: else:
ri2[i] = pArg(ri[i], c, isSink = true) ri2[i] = pArg(ri[i], c, isSink = true)
result = genSink(c, dest.typ, dest, ri2) result.add ri2
of nkSym: of nkSym:
if ri.sym.kind != skParam and isLastRead(ri, c): if ri.sym.kind != skParam and isLastRead(ri, c):
# Rule 3: `=sink`(x, z); wasMoved(z) # Rule 3: `=sink`(x, z); wasMoved(z)
@ -639,8 +642,8 @@ proc p(n: PNode; c: var Con): PNode =
recurse(n, result) recurse(n, result)
proc injectDestructorCalls*(g: ModuleGraph; owner: PSym; n: PNode): PNode = proc injectDestructorCalls*(g: ModuleGraph; owner: PSym; n: PNode): PNode =
when false: # defined(nimDebugDestroys): #when true: # defined(nimDebugDestroys):
echo "injecting into ", n # echo "injecting into ", n
var c: Con var c: Con
c.owner = owner c.owner = owner
c.tmp = newSym(skTemp, getIdent(g.cache, ":d"), owner, n.info) c.tmp = newSym(skTemp, getIdent(g.cache, ":d"), owner, n.info)
@ -675,8 +678,10 @@ proc injectDestructorCalls*(g: ModuleGraph; owner: PSym; n: PNode): PNode =
else: else:
result.add body result.add body
when defined(nimDebugDestroys): #when defined(nimDebugDestroys):
if true: if true:
echo "------------------------------------" echo "------------------------------------"
echo owner.name.s, " transformed to: " echo n
echo result echo "-------"
echo owner.name.s, " transformed to: "
echo result

View file

@ -151,4 +151,13 @@ seq4 =
of 'B': quit(-1) of 'B': quit(-1)
else: else:
let (x1, x2, x3) = myfunc2(2, 3) let (x1, x2, x3) = myfunc2(2, 3)
x3 x3
#------------------------------------------------------------
#-- Move into array constructor
#------------------------------------------------------------
var ii = 1
let arr2 = [newMySeq(2, 5.0), if i > 1: newMySeq(3, 1.0) else: newMySeq(0, 0.0)]
var seqOfSeq2 = @[newMySeq(2, 5.0), newMySeq(3, 1.0)]