destroyer.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Injects destructor calls into Nim code as well as
  10. ## an optimizer that optimizes copies to moves. This is implemented as an
  11. ## AST to AST transformation so that every backend benefits from it.
  12. ## Rules for destructor injections:
  13. ##
  14. ## foo(bar(X(), Y()))
  15. ## X and Y get destroyed after bar completes:
  16. ##
  17. ## foo( (tmpX = X(); tmpY = Y(); tmpBar = bar(tmpX, tmpY);
  18. ## destroy(tmpX); destroy(tmpY);
  19. ## tmpBar))
  20. ## destroy(tmpBar)
  21. ##
  22. ## var x = f()
  23. ## body
  24. ##
  25. ## is the same as:
  26. ##
  27. ## var x;
  28. ## try:
  29. ## move(x, f())
  30. ## finally:
  31. ## destroy(x)
  32. ##
  33. ## But this really just an optimization that tries to avoid to
  34. ## introduce too many temporaries, the 'destroy' is caused by
  35. ## the 'f()' call. No! That is not true for 'result = f()'!
  36. ##
  37. ## x = y where y is read only once
  38. ## is the same as: move(x, y)
  39. ##
  40. ## Actually the more general rule is: The *last* read of ``y``
  41. ## can become a move if ``y`` is the result of a construction.
  42. ##
  43. ## We also need to keep in mind here that the number of reads is
  44. ## control flow dependent:
  45. ## let x = foo()
  46. ## while true:
  47. ## y = x # only one read, but the 2nd iteration will fail!
  48. ## This also affects recursions! Only usages that do not cross
  49. ## a loop boundary (scope) and are not used in function calls
  50. ## are safe.
  51. ##
  52. ##
  53. ## x = f() is the same as: move(x, f())
  54. ##
  55. ## x = y
  56. ## is the same as: copy(x, y)
  57. ##
  58. ## Reassignment works under this scheme:
  59. ## var x = f()
  60. ## x = y
  61. ##
  62. ## is the same as:
  63. ##
  64. ## var x;
  65. ## try:
  66. ## move(x, f())
  67. ## copy(x, y)
  68. ## finally:
  69. ## destroy(x)
  70. ##
  71. ## result = f() must not destroy 'result'!
  72. ##
  73. ## The produced temporaries clutter up the code and might lead to
  74. ## inefficiencies. A better strategy is to collect all the temporaries
  75. ## in a single object that we put into a single try-finally that
  76. ## surrounds the proc body. This means the code stays quite efficient
  77. ## when compiled to C. In fact, we do the same for variables, so
  78. ## destructors are called when the proc returns, not at scope exit!
  79. ## This makes certains idioms easier to support. (Taking the slice
  80. ## of a temporary object.)
  81. ##
  82. ## foo(bar(X(), Y()))
  83. ## X and Y get destroyed after bar completes:
  84. ##
  85. ## var tmp: object
  86. ## foo( (move tmp.x, X(); move tmp.y, Y(); tmp.bar = bar(tmpX, tmpY);
  87. ## tmp.bar))
  88. ## destroy(tmp.bar)
  89. ## destroy(tmp.x); destroy(tmp.y)
  90. ##
  91. ##[
  92. From https://github.com/nim-lang/Nim/wiki/Destructors
  93. Rule Pattern Transformed into
  94. ---- ------- ----------------
  95. 1.1 var x: T; stmts var x: T; try stmts
  96. finally: `=destroy`(x)
  97. 1.2 var x: sink T; stmts var x: sink T; stmts; ensureEmpty(x)
  98. 2 x = f() `=sink`(x, f())
  99. 3 x = lastReadOf z `=sink`(x, z); wasMoved(z)
  100. 4.1 y = sinkParam `=sink`(y, sinkParam)
  101. 4.2 x = y `=`(x, y) # a copy
  102. 5.1 f_sink(g()) f_sink(g())
  103. 5.2 f_sink(y) f_sink(copy y); # copy unless we can see it's the last read
  104. 5.3 f_sink(move y) f_sink(y); wasMoved(y) # explicit moves empties 'y'
  105. 5.4 f_noSink(g()) var tmp = bitwiseCopy(g()); f(tmp); `=destroy`(tmp)
  106. Remarks: Rule 1.2 is not yet implemented because ``sink`` is currently
  107. not allowed as a local variable.
  108. ``move`` builtin needs to be implemented.
  109. ]##
  110. import
  111. intsets, ast, astalgo, msgs, renderer, magicsys, types, idents, trees,
  112. strutils, options, dfa, lowerings, tables, modulegraphs,
  113. lineinfos
  114. const
  115. InterestingSyms = {skVar, skResult, skLet}
  116. type
  117. Con = object
  118. owner: PSym
  119. g: ControlFlowGraph
  120. jumpTargets: IntSet
  121. tmpObj: PType
  122. tmp: PSym
  123. destroys, topLevelVars: PNode
  124. toDropBit: Table[int, PSym]
  125. graph: ModuleGraph
  126. emptyNode: PNode
  127. otherRead: PNode
  128. proc getTemp(c: var Con; typ: PType; info: TLineInfo): PNode =
  129. # XXX why are temps fields in an object here?
  130. let f = newSym(skField, getIdent(c.graph.cache, ":d" & $c.tmpObj.n.len), c.owner, info)
  131. f.typ = typ
  132. rawAddField c.tmpObj, f
  133. result = rawDirectAccess(c.tmp, f)
  134. proc isHarmlessVar*(s: PSym; c: Con): bool =
  135. # 's' is harmless if it used only once and its
  136. # definition/usage are not split by any labels:
  137. #
  138. # let s = foo()
  139. # while true:
  140. # a[i] = s
  141. #
  142. # produces:
  143. #
  144. # def s
  145. # L1:
  146. # use s
  147. # goto L1
  148. #
  149. # let s = foo()
  150. # if cond:
  151. # a[i] = s
  152. # else:
  153. # a[j] = s
  154. #
  155. # produces:
  156. #
  157. # def s
  158. # fork L2
  159. # use s
  160. # goto L3
  161. # L2:
  162. # use s
  163. # L3
  164. #
  165. # So this analysis is for now overly conservative, but correct.
  166. var defsite = -1
  167. var usages = 0
  168. for i in 0..<c.g.len:
  169. case c.g[i].kind
  170. of def:
  171. if c.g[i].sym == s:
  172. if defsite < 0: defsite = i
  173. else: return false
  174. of use:
  175. if c.g[i].sym == s:
  176. if defsite < 0: return false
  177. for j in defsite .. i:
  178. # not within the same basic block?
  179. if j in c.jumpTargets: return false
  180. # if we want to die after the first 'use':
  181. if usages > 1: return false
  182. inc usages
  183. #of useWithinCall:
  184. # if c.g[i].sym == s: return false
  185. of goto, fork:
  186. discard "we do not perform an abstract interpretation yet"
  187. result = usages <= 1
  188. proc isLastRead(n: PNode; c: var Con): bool =
  189. # first we need to search for the instruction that belongs to 'n':
  190. doAssert n.kind == nkSym
  191. c.otherRead = nil
  192. var instr = -1
  193. for i in 0..<c.g.len:
  194. if c.g[i].n == n:
  195. if instr < 0: instr = i
  196. else:
  197. # eh, we found two positions that belong to 'n'?
  198. # better return 'false' then:
  199. return false
  200. if instr < 0: return false
  201. # we go through all paths beginning from 'instr+1' and need to
  202. # ensure that we don't find another 'use X' instruction.
  203. if instr+1 >= c.g.len: return true
  204. let s = n.sym
  205. var pcs: seq[int] = @[instr+1]
  206. var takenGotos: IntSet
  207. var takenForks = initIntSet()
  208. while pcs.len > 0:
  209. var pc = pcs.pop
  210. takenGotos = initIntSet()
  211. while pc < c.g.len:
  212. case c.g[pc].kind
  213. of def:
  214. if c.g[pc].sym == s:
  215. # the path lead to a redefinition of 's' --> abandon it.
  216. when false:
  217. # Too complex thinking ahead: In reality it is enough to find
  218. # the 'def x' here on the current path to make the 'use x' valid.
  219. # but for this the definition needs to dominate the usage:
  220. var dominates = true
  221. for j in pc+1 .. instr:
  222. # not within the same basic block?
  223. if c.g[j].kind in {goto, fork} and (j + c.g[j].dest) in (pc+1 .. instr):
  224. #if j in c.jumpTargets:
  225. dominates = false
  226. if dominates: break
  227. break
  228. inc pc
  229. of use:
  230. if c.g[pc].sym == s:
  231. c.otherRead = c.g[pc].n
  232. return false
  233. inc pc
  234. of goto:
  235. # we must leave endless loops eventually:
  236. if not takenGotos.containsOrIncl(pc):
  237. pc = pc + c.g[pc].dest
  238. else:
  239. inc pc
  240. of fork:
  241. # we follow the next instruction but push the dest onto our "work" stack:
  242. if not takenForks.containsOrIncl(pc):
  243. pcs.add pc + c.g[pc].dest
  244. inc pc
  245. #echo c.graph.config $ n.info, " last read here!"
  246. return true
  247. template interestingSym(s: PSym): bool =
  248. s.owner == c.owner and s.kind in InterestingSyms and hasDestructor(s.typ)
  249. proc patchHead(n: PNode) =
  250. if n.kind in nkCallKinds and n[0].kind == nkSym and n.len > 1:
  251. let s = n[0].sym
  252. if s.name.s[0] == '=' and s.name.s in ["=sink", "=", "=destroy"]:
  253. if sfFromGeneric in s.flags:
  254. excl(s.flags, sfFromGeneric)
  255. patchHead(s.getBody)
  256. let t = n[1].typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink, tyInferred})
  257. template patch(op, field) =
  258. if s.name.s == op and field != nil and field != s:
  259. n.sons[0].sym = field
  260. patch "=sink", t.sink
  261. patch "=", t.assignment
  262. patch "=destroy", t.destructor
  263. for x in n:
  264. patchHead(x)
  265. proc patchHead(s: PSym) =
  266. if sfFromGeneric in s.flags:
  267. patchHead(s.ast[bodyPos])
  268. proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string) =
  269. var m = "'" & opname & "' is not available for type <" & typeToString(t) & ">"
  270. if opname == "=" and ri != nil:
  271. m.add "; requires a copy because it's not the last read of '"
  272. m.add renderTree(ri)
  273. m.add '\''
  274. if c.otherRead != nil:
  275. m.add "; another read is done here: "
  276. m.add c.graph.config $ c.otherRead.info
  277. localError(c.graph.config, ri.info, errGenerated, m)
  278. proc makePtrType(c: Con, baseType: PType): PType =
  279. result = newType(tyPtr, c.owner)
  280. addSonSkipIntLit(result, baseType)
  281. template genOp(opr, opname, ri) =
  282. let op = opr
  283. if op == nil:
  284. globalError(c.graph.config, dest.info, "internal error: '" & opname & "' operator not found for type " & typeToString(t))
  285. elif op.ast[genericParamsPos].kind != nkEmpty:
  286. globalError(c.graph.config, dest.info, "internal error: '" & opname & "' operator is generic")
  287. patchHead op
  288. if sfError in op.flags: checkForErrorPragma(c, t, ri, opname)
  289. let addrExp = newNodeIT(nkHiddenAddr, dest.info, makePtrType(c, dest.typ))
  290. addrExp.add(dest)
  291. result = newTree(nkCall, newSymNode(op), addrExp)
  292. proc genSink(c: Con; t: PType; dest, ri: PNode): PNode =
  293. let t = t.skipTypes({tyGenericInst, tyAlias, tySink})
  294. genOp(if t.sink != nil: t.sink else: t.assignment, "=sink", ri)
  295. proc genCopy(c: Con; t: PType; dest, ri: PNode): PNode =
  296. let t = t.skipTypes({tyGenericInst, tyAlias, tySink})
  297. genOp(t.assignment, "=", ri)
  298. proc genDestroy(c: Con; t: PType; dest: PNode): PNode =
  299. let t = t.skipTypes({tyGenericInst, tyAlias, tySink})
  300. genOp(t.destructor, "=destroy", nil)
  301. proc addTopVar(c: var Con; v: PNode) =
  302. c.topLevelVars.add newTree(nkIdentDefs, v, c.emptyNode, c.emptyNode)
  303. proc dropBit(c: var Con; s: PSym): PSym =
  304. result = c.toDropBit.getOrDefault(s.id)
  305. assert result != nil
  306. proc registerDropBit(c: var Con; s: PSym) =
  307. let result = newSym(skTemp, getIdent(c.graph.cache, s.name.s & "_AliveBit"), c.owner, s.info)
  308. result.typ = getSysType(c.graph, s.info, tyBool)
  309. let trueVal = newIntTypeNode(nkIntLit, 1, result.typ)
  310. c.topLevelVars.add newTree(nkIdentDefs, newSymNode result, c.emptyNode, trueVal)
  311. c.toDropBit[s.id] = result
  312. # generate:
  313. # if not sinkParam_AliveBit: `=destroy`(sinkParam)
  314. let t = s.typ.skipTypes({tyGenericInst, tyAlias, tySink})
  315. if t.destructor != nil:
  316. c.destroys.add newTree(nkIfStmt,
  317. newTree(nkElifBranch, newSymNode result, genDestroy(c, t, newSymNode s)))
  318. proc p(n: PNode; c: var Con): PNode
  319. template recurse(n, dest) =
  320. for i in 0..<n.len:
  321. dest.add p(n[i], c)
  322. proc isSinkParam(s: PSym): bool {.inline.} =
  323. result = s.kind == skParam and s.typ.kind == tySink
  324. proc destructiveMoveSink(n: PNode; c: var Con): PNode =
  325. # generate: (chckMove(sinkParam_AliveBit); sinkParam_AliveBit = false; sinkParam)
  326. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  327. let bit = newSymNode dropBit(c, n.sym)
  328. if optMoveCheck in c.owner.options:
  329. result.add callCodegenProc(c.graph, "chckMove", bit.info, bit)
  330. result.add newTree(nkAsgn, bit,
  331. newIntTypeNode(nkIntLit, 0, getSysType(c.graph, n.info, tyBool)))
  332. result.add n
  333. proc genMagicCall(n: PNode; c: var Con; magicname: string; m: TMagic): PNode =
  334. result = newNodeI(nkCall, n.info)
  335. result.add(newSymNode(createMagic(c.graph, magicname, m)))
  336. result.add n
  337. proc genWasMoved(n: PNode; c: var Con): PNode =
  338. # The mWasMoved builtin does not take the address.
  339. result = genMagicCall(n, c, "wasMoved", mWasMoved)
  340. proc destructiveMoveVar(n: PNode; c: var Con): PNode =
  341. # generate: (let tmp = v; reset(v); tmp)
  342. # XXX: Strictly speaking we can only move if there is a ``=sink`` defined
  343. # or if no ``=sink`` is defined and also no assignment.
  344. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  345. var temp = newSym(skLet, getIdent(c.graph.cache, "blitTmp"), c.owner, n.info)
  346. temp.typ = n.typ
  347. var v = newNodeI(nkLetSection, n.info)
  348. let tempAsNode = newSymNode(temp)
  349. var vpart = newNodeI(nkIdentDefs, tempAsNode.info, 3)
  350. vpart.sons[0] = tempAsNode
  351. vpart.sons[1] = c.emptyNode
  352. vpart.sons[2] = n
  353. add(v, vpart)
  354. result.add v
  355. result.add genWasMoved(n, c)
  356. result.add tempAsNode
  357. proc passCopyToSink(n: PNode; c: var Con): PNode =
  358. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  359. let tmp = getTemp(c, n.typ, n.info)
  360. if hasDestructor(n.typ):
  361. var m = genCopy(c, n.typ, tmp, n)
  362. m.add p(n, c)
  363. result.add m
  364. message(c.graph.config, n.info, hintPerformance,
  365. ("passing '$1' to a sink parameter introduces an implicit copy; " &
  366. "use 'move($1)' to prevent it") % $n)
  367. else:
  368. result.add newTree(nkAsgn, tmp, p(n, c))
  369. result.add tmp
  370. proc pArg(arg: PNode; c: var Con; isSink: bool): PNode =
  371. if isSink:
  372. if arg.kind in nkCallKinds:
  373. # recurse but skip the call expression in order to prevent
  374. # destructor injections: Rule 5.1 is different from rule 5.4!
  375. result = copyNode(arg)
  376. let parameters = arg[0].typ
  377. let L = if parameters != nil: parameters.len else: 0
  378. result.add arg[0]
  379. for i in 1..<arg.len:
  380. result.add pArg(arg[i], c, i < L and parameters[i].kind == tySink)
  381. elif arg.kind in {nkObjConstr, nkCharLit..nkFloat128Lit}:
  382. discard "object construction to sink parameter: nothing to do"
  383. result = arg
  384. elif arg.kind == nkSym and arg.sym.kind in InterestingSyms and isLastRead(arg, c):
  385. # if x is a variable and it its last read we eliminate its
  386. # destructor invokation, but don't. We need to reset its memory
  387. # to disable its destructor which we have not elided:
  388. result = destructiveMoveVar(arg, c)
  389. elif arg.kind == nkSym and isSinkParam(arg.sym):
  390. # mark the sink parameter as used:
  391. result = destructiveMoveSink(arg, c)
  392. else:
  393. # an object that is not temporary but passed to a 'sink' parameter
  394. # results in a copy.
  395. result = passCopyToSink(arg, c)
  396. else:
  397. result = p(arg, c)
  398. proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
  399. case ri.kind
  400. of nkCallKinds:
  401. result = genSink(c, dest.typ, dest, ri)
  402. # watch out and no not transform 'ri' twice if it's a call:
  403. let ri2 = copyNode(ri)
  404. let parameters = ri[0].typ
  405. let L = if parameters != nil: parameters.len else: 0
  406. ri2.add ri[0]
  407. for i in 1..<ri.len:
  408. ri2.add pArg(ri[i], c, i < L and parameters[i].kind == tySink)
  409. #recurse(ri, ri2)
  410. result.add ri2
  411. of nkObjConstr:
  412. result = genSink(c, dest.typ, dest, ri)
  413. let ri2 = copyTree(ri)
  414. for i in 1..<ri.len:
  415. # everything that is passed to an object constructor is consumed,
  416. # so these all act like 'sink' parameters:
  417. ri2[i].sons[1] = pArg(ri[i][1], c, isSink = true)
  418. result.add ri2
  419. of nkSym:
  420. if ri.sym.kind != skParam and isLastRead(ri, c):
  421. # Rule 3: `=sink`(x, z); wasMoved(z)
  422. var snk = genSink(c, dest.typ, dest, ri)
  423. snk.add p(ri, c)
  424. result = newTree(nkStmtList, snk, genMagicCall(ri, c, "wasMoved", mWasMoved))
  425. elif isSinkParam(ri.sym):
  426. result = genSink(c, dest.typ, dest, ri)
  427. result.add destructiveMoveSink(ri, c)
  428. else:
  429. result = genCopy(c, dest.typ, dest, ri)
  430. result.add p(ri, c)
  431. else:
  432. result = genCopy(c, dest.typ, dest, ri)
  433. result.add p(ri, c)
  434. proc p(n: PNode; c: var Con): PNode =
  435. case n.kind
  436. of nkVarSection, nkLetSection:
  437. discard "transform; var x = y to var x; x op y where op is a move or copy"
  438. result = newNodeI(nkStmtList, n.info)
  439. for i in 0..<n.len:
  440. let it = n[i]
  441. let L = it.len-1
  442. let ri = it[L]
  443. if it.kind == nkVarTuple and hasDestructor(ri.typ):
  444. let x = lowerTupleUnpacking(c.graph, it, c.owner)
  445. result.add p(x, c)
  446. elif it.kind == nkIdentDefs and hasDestructor(it[0].typ):
  447. for j in 0..L-2:
  448. let v = it[j]
  449. doAssert v.kind == nkSym
  450. # move the variable declaration to the top of the frame:
  451. c.addTopVar v
  452. # make sure it's destroyed at the end of the proc:
  453. c.destroys.add genDestroy(c, v.typ, v)
  454. if ri.kind != nkEmpty:
  455. let r = moveOrCopy(v, ri, c)
  456. result.add r
  457. else:
  458. # keep it, but transform 'ri':
  459. var varSection = copyNode(n)
  460. var itCopy = copyNode(it)
  461. for j in 0..L-1:
  462. itCopy.add it[j]
  463. itCopy.add p(ri, c)
  464. varSection.add itCopy
  465. result.add varSection
  466. of nkCallKinds:
  467. let parameters = n[0].typ
  468. let L = if parameters != nil: parameters.len else: 0
  469. for i in 1 ..< n.len:
  470. n.sons[i] = pArg(n[i], c, i < L and parameters[i].kind == tySink)
  471. if n.typ != nil and hasDestructor(n.typ):
  472. discard "produce temp creation"
  473. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  474. let tmp = getTemp(c, n.typ, n.info)
  475. var sinkExpr = genSink(c, n.typ, tmp, n)
  476. sinkExpr.add n
  477. result.add sinkExpr
  478. result.add tmp
  479. c.destroys.add genDestroy(c, n.typ, tmp)
  480. else:
  481. result = n
  482. of nkAsgn, nkFastAsgn:
  483. if hasDestructor(n[0].typ):
  484. result = moveOrCopy(n[0], n[1], c)
  485. else:
  486. result = copyNode(n)
  487. recurse(n, result)
  488. of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef,
  489. nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo, nkFuncDef:
  490. result = n
  491. else:
  492. result = copyNode(n)
  493. recurse(n, result)
  494. proc injectDestructorCalls*(g: ModuleGraph; owner: PSym; n: PNode): PNode =
  495. when false: # defined(nimDebugDestroys):
  496. echo "injecting into ", n
  497. var c: Con
  498. c.owner = owner
  499. c.tmp = newSym(skTemp, getIdent(g.cache, ":d"), owner, n.info)
  500. c.tmpObj = createObj(g, owner, n.info)
  501. c.tmp.typ = c.tmpObj
  502. c.destroys = newNodeI(nkStmtList, n.info)
  503. c.topLevelVars = newNodeI(nkVarSection, n.info)
  504. c.toDropBit = initTable[int, PSym]()
  505. c.graph = g
  506. c.emptyNode = newNodeI(nkEmpty, n.info)
  507. let cfg = constructCfg(owner, n)
  508. shallowCopy(c.g, cfg)
  509. c.jumpTargets = initIntSet()
  510. for i in 0..<c.g.len:
  511. if c.g[i].kind in {goto, fork}:
  512. c.jumpTargets.incl(i+c.g[i].dest)
  513. #if owner.name.s == "test0p1":
  514. # echoCfg(c.g)
  515. if owner.kind in {skProc, skFunc, skMethod, skIterator, skConverter}:
  516. let params = owner.typ.n
  517. for i in 1 ..< params.len:
  518. let param = params[i].sym
  519. if param.typ.kind == tySink: registerDropBit(c, param)
  520. let body = p(n, c)
  521. if c.tmp.typ.n.len > 0:
  522. c.addTopVar(newSymNode c.tmp)
  523. result = newNodeI(nkStmtList, n.info)
  524. if c.topLevelVars.len > 0:
  525. result.add c.topLevelVars
  526. if c.destroys.len > 0:
  527. result.add newTryFinally(body, c.destroys)
  528. else:
  529. result.add body
  530. when defined(nimDebugDestroys):
  531. if true:
  532. echo "------------------------------------"
  533. echo owner.name.s, " transformed to: "
  534. echo result