new destroyer pass works with procs too

This commit is contained in:
Andreas Rumpf
2017-10-13 07:00:52 +02:00
parent ffe52a7f7b
commit 5dca695bcf
4 changed files with 11 additions and 9 deletions

View File

@@ -290,4 +290,7 @@ proc injectDestructorCalls*(owner: PSym; n: PNode): PNode =
else:
result.add body
#echo "transformed into: ", result
when false:
echo "------------------------------------"
echo owner.name.s, " transformed from: ", n
echo result

View File

@@ -400,7 +400,7 @@ proc addToVarSection(c: PContext; result: var PNode; orig, identDefs: PNode) =
# in order for this transformation to be correct.
let L = identDefs.len
let value = identDefs[L-1]
if value.typ != nil and tfHasAsgn in value.typ.flags and c.p.owner.kind != skFunc:
if value.typ != nil and tfHasAsgn in value.typ.flags and not newDestructors:
# the spec says we need to rewrite 'var x = T()' to 'var x: T; x = T()':
identDefs.sons[L-1] = emptyNode
if result.kind != nkStmtList:

View File

@@ -971,11 +971,11 @@ proc transformBody*(module: PSym, n: PNode, prc: PSym): PNode =
result = processTransf(c, result, prc)
liftDefer(c, result)
#result = liftLambdas(prc, result)
incl(result.flags, nfTransf)
when useEffectSystem: trackProc(prc, result)
if c.needsDestroyPass and newDestructors:
result = injectDestructorCalls(prc, result)
#if prc.name.s == "testbody":
incl(result.flags, nfTransf)
#if prc.name.s == "testbody":
# echo renderTree(result)
proc transformStmt*(module: PSym, n: PNode): PNode =
@@ -986,12 +986,12 @@ proc transformStmt*(module: PSym, n: PNode): PNode =
result = processTransf(c, n, module)
liftDefer(c, result)
#result = liftLambdasForTopLevel(module, result)
incl(result.flags, nfTransf)
when useEffectSystem: trackTopLevelStmt(module, result)
#if n.info ?? "temp.nim":
# echo renderTree(result, {renderIds})
if c.needsDestroyPass and newDestructors:
result = injectDestructorCalls(module, result)
incl(result.flags, nfTransf)
proc transformExpr*(module: PSym, n: PNode): PNode =
if nfTransf in n.flags:
@@ -1000,6 +1000,6 @@ proc transformExpr*(module: PSym, n: PNode): PNode =
var c = openTransf(module, "")
result = processTransf(c, n, module)
liftDefer(c, result)
incl(result.flags, nfTransf)
if c.needsDestroyPass and newDestructors:
result = injectDestructorCalls(module, result)
incl(result.flags, nfTransf)

View File

@@ -78,11 +78,11 @@ proc create*(lit: string): mystring =
copyMem(addr result.data[result.len], unsafeAddr lit[0], newLen + 1)
result.len = newLen
func `&`*(a, b: mystring): mystring =
proc `&`*(a, b: mystring): mystring =
result = a
result.add b
func main(n: int) =
proc main(n: int) =
var a: mystring
let b = create" to append"
for i in 0..<n:
@@ -92,6 +92,5 @@ func main(n: int) =
a.add c
echo cstring(a.data)
main(1000)
echo "after ", allocCount, " ", deallocCount