injectdestructors.nim 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. ## See doc/destructors.rst for a spec of the implemented rewrite rules
  13. import
  14. intsets, strtabs, ast, astalgo, msgs, renderer, magicsys, types, idents,
  15. strutils, options, dfa, lowerings, tables, modulegraphs, msgs,
  16. lineinfos, parampatterns, sighashes, liftdestructors, optimizer,
  17. varpartitions
  18. from trees import exprStructuralEquivalent, getRoot
  19. type
  20. Scope = object # well we do scope-based memory management. \
  21. # a scope is comparable to an nkStmtListExpr like
  22. # (try: statements; dest = y(); finally: destructors(); dest)
  23. vars: seq[PSym]
  24. wasMoved: seq[PNode]
  25. final: seq[PNode] # finally section
  26. needsTry: bool
  27. parent: ptr Scope
  28. type
  29. Con = object
  30. owner: PSym
  31. g: ControlFlowGraph
  32. graph: ModuleGraph
  33. otherRead: PNode
  34. inLoop, inSpawn: int
  35. uninit: IntSet # set of uninit'ed vars
  36. uninitComputed: bool
  37. idgen: IdGenerator
  38. ProcessMode = enum
  39. normal
  40. consumed
  41. sinkArg
  42. const toDebug {.strdefine.} = ""
  43. proc hasDestructor(c: Con; t: PType): bool {.inline.} =
  44. result = ast.hasDestructor(t)
  45. when toDebug.len > 0:
  46. # for more effective debugging
  47. if not result and c.graph.config.selectedGC in {gcArc, gcOrc}:
  48. assert(not containsGarbageCollectedRef(t))
  49. template dbg(body) =
  50. when toDebug.len > 0:
  51. if c.owner.name.s == toDebug or toDebug == "always":
  52. body
  53. proc getTemp(c: var Con; s: var Scope; typ: PType; info: TLineInfo): PNode =
  54. let sym = newSym(skTemp, getIdent(c.graph.cache, ":tmpD"), nextId c.idgen, c.owner, info)
  55. sym.typ = typ
  56. s.vars.add(sym)
  57. result = newSymNode(sym)
  58. proc nestedScope(parent: var Scope): Scope =
  59. Scope(vars: @[], wasMoved: @[], final: @[], needsTry: false, parent: addr(parent))
  60. proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode): PNode
  61. proc moveOrCopy(dest, ri: PNode; c: var Con; s: var Scope; isDecl = false): PNode
  62. proc isLastRead(location: PNode; cfg: ControlFlowGraph; otherRead: var PNode; pc, until: int): int =
  63. var pc = pc
  64. while pc < cfg.len and pc < until:
  65. case cfg[pc].kind
  66. of def:
  67. if instrTargets(cfg[pc].n, location) == Full:
  68. # the path leads to a redefinition of 's' --> abandon it.
  69. return high(int)
  70. elif instrTargets(cfg[pc].n, location) == Partial:
  71. # only partially writes to 's' --> can't sink 's', so this def reads 's'
  72. otherRead = cfg[pc].n
  73. return -1
  74. inc pc
  75. of use:
  76. if instrTargets(cfg[pc].n, location) != None:
  77. otherRead = cfg[pc].n
  78. return -1
  79. inc pc
  80. of goto:
  81. pc += cfg[pc].dest
  82. of fork:
  83. # every branch must lead to the last read of the location:
  84. var variantA = pc + 1
  85. var variantB = pc + cfg[pc].dest
  86. while variantA != variantB:
  87. if min(variantA, variantB) < 0: return -1
  88. if max(variantA, variantB) >= cfg.len or min(variantA, variantB) >= until:
  89. break
  90. if variantA < variantB:
  91. variantA = isLastRead(location, cfg, otherRead, variantA, min(variantB, until))
  92. else:
  93. variantB = isLastRead(location, cfg, otherRead, variantB, min(variantA, until))
  94. pc = min(variantA, variantB)
  95. return pc
  96. proc isCursor(n: PNode; c: Con): bool =
  97. case n.kind
  98. of nkSym:
  99. sfCursor in n.sym.flags
  100. of nkDotExpr:
  101. isCursor(n[1], c)
  102. of nkCheckedFieldExpr:
  103. isCursor(n[0], c)
  104. else:
  105. false
  106. proc isLastRead(n: PNode; c: var Con): bool =
  107. # first we need to search for the instruction that belongs to 'n':
  108. var instr = -1
  109. let m = dfa.skipConvDfa(n)
  110. if m.kind == nkSym and sfSingleUsedTemp in m.sym.flags: return true
  111. for i in 0..<c.g.len:
  112. # This comparison is correct and MUST not be ``instrTargets``:
  113. if c.g[i].kind == use and c.g[i].n == m:
  114. if instr < 0:
  115. instr = i
  116. break
  117. dbg: echo "starting point for ", n, " is ", instr, " ", n.kind
  118. if instr < 0: return false
  119. # we go through all paths beginning from 'instr+1' and need to
  120. # ensure that we don't find another 'use X' instruction.
  121. if instr+1 >= c.g.len: return true
  122. c.otherRead = nil
  123. result = isLastRead(n, c.g, c.otherRead, instr+1, int.high) >= 0
  124. dbg: echo "ugh ", c.otherRead.isNil, " ", result
  125. proc isFirstWrite(location: PNode; cfg: ControlFlowGraph; pc, until: int): int =
  126. var pc = pc
  127. while pc < until:
  128. case cfg[pc].kind
  129. of def:
  130. if instrTargets(cfg[pc].n, location) != None:
  131. # a definition of 's' before ours makes ours not the first write
  132. return -1
  133. inc pc
  134. of use:
  135. if instrTargets(cfg[pc].n, location) != None:
  136. return -1
  137. inc pc
  138. of goto:
  139. pc += cfg[pc].dest
  140. of fork:
  141. # every branch must not contain a def/use of our location:
  142. var variantA = pc + 1
  143. var variantB = pc + cfg[pc].dest
  144. while variantA != variantB:
  145. if min(variantA, variantB) < 0: return -1
  146. if max(variantA, variantB) > until:
  147. break
  148. if variantA < variantB:
  149. variantA = isFirstWrite(location, cfg, variantA, min(variantB, until))
  150. else:
  151. variantB = isFirstWrite(location, cfg, variantB, min(variantA, until))
  152. pc = min(variantA, variantB)
  153. return pc
  154. proc isFirstWrite(n: PNode; c: var Con): bool =
  155. # first we need to search for the instruction that belongs to 'n':
  156. var instr = -1
  157. let m = dfa.skipConvDfa(n)
  158. for i in countdown(c.g.len-1, 0): # We search backwards here to treat loops correctly
  159. if c.g[i].kind == def and c.g[i].n == m:
  160. if instr < 0:
  161. instr = i
  162. break
  163. if instr < 0: return false
  164. # we go through all paths going to 'instr' and need to
  165. # ensure that we don't find another 'def/use X' instruction.
  166. if instr == 0: return true
  167. result = isFirstWrite(n, c.g, 0, instr) >= 0
  168. proc initialized(code: ControlFlowGraph; pc: int,
  169. init, uninit: var IntSet; until: int): int =
  170. ## Computes the set of definitely initialized variables across all code paths
  171. ## as an IntSet of IDs.
  172. var pc = pc
  173. while pc < code.len:
  174. case code[pc].kind
  175. of goto:
  176. pc += code[pc].dest
  177. of fork:
  178. var initA = initIntSet()
  179. var initB = initIntSet()
  180. var variantA = pc + 1
  181. var variantB = pc + code[pc].dest
  182. while variantA != variantB:
  183. if max(variantA, variantB) > until:
  184. break
  185. if variantA < variantB:
  186. variantA = initialized(code, variantA, initA, uninit, min(variantB, until))
  187. else:
  188. variantB = initialized(code, variantB, initB, uninit, min(variantA, until))
  189. pc = min(variantA, variantB)
  190. # we add vars if they are in both branches:
  191. for v in initA:
  192. if v in initB:
  193. init.incl v
  194. of use:
  195. let v = code[pc].n.sym
  196. if v.kind != skParam and v.id notin init:
  197. # attempt to read an uninit'ed variable
  198. uninit.incl v.id
  199. inc pc
  200. of def:
  201. let v = code[pc].n.sym
  202. init.incl v.id
  203. inc pc
  204. return pc
  205. template isUnpackedTuple(n: PNode): bool =
  206. ## we move out all elements of unpacked tuples,
  207. ## hence unpacked tuples themselves don't need to be destroyed
  208. (n.kind == nkSym and n.sym.kind == skTemp and n.sym.typ.kind == tyTuple)
  209. proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string) =
  210. var m = "'" & opname & "' is not available for type <" & typeToString(t) & ">"
  211. if (opname == "=" or opname == "=copy") and ri != nil:
  212. m.add "; requires a copy because it's not the last read of '"
  213. m.add renderTree(ri)
  214. m.add '\''
  215. if c.otherRead != nil:
  216. m.add "; another read is done here: "
  217. m.add c.graph.config $ c.otherRead.info
  218. elif ri.kind == nkSym and ri.sym.kind == skParam and not isSinkType(ri.sym.typ):
  219. m.add "; try to make "
  220. m.add renderTree(ri)
  221. m.add " a 'sink' parameter"
  222. m.add "; routine: "
  223. m.add c.owner.name.s
  224. localError(c.graph.config, ri.info, errGenerated, m)
  225. proc makePtrType(c: var Con, baseType: PType): PType =
  226. result = newType(tyPtr, nextId c.idgen, c.owner)
  227. addSonSkipIntLit(result, baseType, c.idgen)
  228. proc genOp(c: var Con; op: PSym; dest: PNode): PNode =
  229. let addrExp = newNodeIT(nkHiddenAddr, dest.info, makePtrType(c, dest.typ))
  230. addrExp.add(dest)
  231. result = newTree(nkCall, newSymNode(op), addrExp)
  232. proc genOp(c: var Con; t: PType; kind: TTypeAttachedOp; dest, ri: PNode): PNode =
  233. var op = t.attachedOps[kind]
  234. if op == nil or op.ast[genericParamsPos].kind != nkEmpty:
  235. # give up and find the canonical type instead:
  236. let h = sighashes.hashType(t, {CoType, CoConsiderOwned, CoDistinct})
  237. let canon = c.graph.canonTypes.getOrDefault(h)
  238. if canon != nil:
  239. op = canon.attachedOps[kind]
  240. if op == nil:
  241. #echo dest.typ.id
  242. globalError(c.graph.config, dest.info, "internal error: '" & AttachedOpToStr[kind] &
  243. "' operator not found for type " & typeToString(t))
  244. elif op.ast[genericParamsPos].kind != nkEmpty:
  245. globalError(c.graph.config, dest.info, "internal error: '" & AttachedOpToStr[kind] &
  246. "' operator is generic")
  247. dbg:
  248. if kind == attachedDestructor:
  249. echo "destructor is ", op.id, " ", op.ast
  250. if sfError in op.flags: checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
  251. c.genOp(op, dest)
  252. proc genDestroy(c: var Con; dest: PNode): PNode =
  253. let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink})
  254. result = c.genOp(t, attachedDestructor, dest, nil)
  255. proc canBeMoved(c: Con; t: PType): bool {.inline.} =
  256. let t = t.skipTypes({tyGenericInst, tyAlias, tySink})
  257. if optOwnedRefs in c.graph.config.globalOptions:
  258. result = t.kind != tyRef and t.attachedOps[attachedSink] != nil
  259. else:
  260. result = t.attachedOps[attachedSink] != nil
  261. proc isNoInit(dest: PNode): bool {.inline.} =
  262. result = dest.kind == nkSym and sfNoInit in dest.sym.flags
  263. proc genSink(c: var Con; dest, ri: PNode, isDecl = false): PNode =
  264. if isUnpackedTuple(dest) or isDecl or
  265. (isAnalysableFieldAccess(dest, c.owner) and isFirstWrite(dest, c)) or
  266. isNoInit(dest):
  267. # optimize sink call into a bitwise memcopy
  268. result = newTree(nkFastAsgn, dest, ri)
  269. else:
  270. let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink})
  271. if t.attachedOps[attachedSink] != nil:
  272. result = c.genOp(t, attachedSink, dest, ri)
  273. result.add ri
  274. else:
  275. # the default is to use combination of `=destroy(dest)` and
  276. # and copyMem(dest, source). This is efficient.
  277. result = newTree(nkStmtList, c.genDestroy(dest), newTree(nkFastAsgn, dest, ri))
  278. proc isCriticalLink(dest: PNode): bool {.inline.} =
  279. #[
  280. Lins's idea that only "critical" links can introduce a cycle. This is
  281. critical for the performance gurantees that we strive for: If you
  282. traverse a data structure, no tracing will be performed at all.
  283. ORC is about this promise: The GC only touches the memory that the
  284. mutator touches too.
  285. These constructs cannot possibly create cycles::
  286. local = ...
  287. new(x)
  288. dest = ObjectConstructor(field: noalias(dest))
  289. But since 'ObjectConstructor' is already moved into 'dest' all we really have
  290. to look for is assignments to local variables.
  291. ]#
  292. result = dest.kind != nkSym
  293. proc finishCopy(c: var Con; result, dest: PNode; isFromSink: bool) =
  294. if c.graph.config.selectedGC == gcOrc:
  295. let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct})
  296. if cyclicType(t):
  297. result.add boolLit(c.graph, result.info, isFromSink or isCriticalLink(dest))
  298. proc genMarkCyclic(c: var Con; result, dest: PNode) =
  299. if c.graph.config.selectedGC == gcOrc:
  300. let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct})
  301. if cyclicType(t):
  302. if t.kind == tyRef:
  303. result.add callCodegenProc(c.graph, "nimMarkCyclic", dest.info, dest)
  304. else:
  305. let xenv = genBuiltin(c.graph, mAccessEnv, "accessEnv", dest)
  306. xenv.typ = getSysType(c.graph, dest.info, tyPointer)
  307. result.add callCodegenProc(c.graph, "nimMarkCyclic", dest.info, xenv)
  308. proc genCopyNoCheck(c: var Con; dest, ri: PNode): PNode =
  309. let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink})
  310. result = c.genOp(t, attachedAsgn, dest, ri)
  311. proc genCopy(c: var Con; dest, ri: PNode): PNode =
  312. let t = dest.typ
  313. if tfHasOwned in t.flags and ri.kind != nkNilLit:
  314. # try to improve the error message here:
  315. if c.otherRead == nil: discard isLastRead(ri, c)
  316. c.checkForErrorPragma(t, ri, "=copy")
  317. result = c.genCopyNoCheck(dest, ri)
  318. proc genDiscriminantAsgn(c: var Con; s: var Scope; n: PNode): PNode =
  319. # discriminator is ordinal value that doesn't need sink destroy
  320. # but fields within active case branch might need destruction
  321. # tmp to support self assignments
  322. let tmp = c.getTemp(s, n[1].typ, n.info)
  323. result = newTree(nkStmtList)
  324. result.add newTree(nkFastAsgn, tmp, p(n[1], c, s, consumed))
  325. result.add p(n[0], c, s, normal)
  326. let le = p(n[0], c, s, normal)
  327. let leDotExpr = if le.kind == nkCheckedFieldExpr: le[0] else: le
  328. let objType = leDotExpr[0].typ
  329. if hasDestructor(c, objType):
  330. if objType.attachedOps[attachedDestructor] != nil and
  331. sfOverriden in objType.attachedOps[attachedDestructor].flags:
  332. localError(c.graph.config, n.info, errGenerated, """Assignment to discriminant for objects with user defined destructor is not supported, object must have default destructor.
  333. It is best to factor out piece of object that needs custom destructor into separate object or not use discriminator assignment""")
  334. result.add newTree(nkFastAsgn, le, tmp)
  335. return
  336. # generate: if le != tmp: `=destroy`(le)
  337. let branchDestructor = produceDestructorForDiscriminator(c.graph, objType, leDotExpr[1].sym, n.info, c.idgen)
  338. let cond = newNodeIT(nkInfix, n.info, getSysType(c.graph, unknownLineInfo, tyBool))
  339. cond.add newSymNode(getMagicEqSymForType(c.graph, le.typ, n.info))
  340. cond.add le
  341. cond.add tmp
  342. let notExpr = newNodeIT(nkPrefix, n.info, getSysType(c.graph, unknownLineInfo, tyBool))
  343. notExpr.add newSymNode(createMagic(c.graph, "not", mNot))
  344. notExpr.add cond
  345. result.add newTree(nkIfStmt, newTree(nkElifBranch, notExpr, c.genOp(branchDestructor, le)))
  346. result.add newTree(nkFastAsgn, le, tmp)
  347. proc genWasMoved(c: var Con, n: PNode): PNode =
  348. result = newNodeI(nkCall, n.info)
  349. result.add(newSymNode(createMagic(c.graph, "wasMoved", mWasMoved)))
  350. result.add copyTree(n) #mWasMoved does not take the address
  351. #if n.kind != nkSym:
  352. # message(c.graph.config, n.info, warnUser, "wasMoved(" & $n & ")")
  353. proc genDefaultCall(t: PType; c: Con; info: TLineInfo): PNode =
  354. result = newNodeI(nkCall, info)
  355. result.add(newSymNode(createMagic(c.graph, "default", mDefault)))
  356. result.typ = t
  357. proc destructiveMoveVar(n: PNode; c: var Con; s: var Scope): PNode =
  358. # generate: (let tmp = v; reset(v); tmp)
  359. if not hasDestructor(c, n.typ):
  360. assert n.kind != nkSym or not hasDestructor(c, n.sym.typ)
  361. result = copyTree(n)
  362. else:
  363. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  364. var temp = newSym(skLet, getIdent(c.graph.cache, "blitTmp"), nextId c.idgen, c.owner, n.info)
  365. temp.typ = n.typ
  366. var v = newNodeI(nkLetSection, n.info)
  367. let tempAsNode = newSymNode(temp)
  368. var vpart = newNodeI(nkIdentDefs, tempAsNode.info, 3)
  369. vpart[0] = tempAsNode
  370. vpart[1] = newNodeI(nkEmpty, tempAsNode.info)
  371. vpart[2] = n
  372. v.add(vpart)
  373. result.add v
  374. let nn = skipConv(n)
  375. c.genMarkCyclic(result, nn)
  376. let wasMovedCall = c.genWasMoved(nn)
  377. result.add wasMovedCall
  378. result.add tempAsNode
  379. proc isCapturedVar(n: PNode): bool =
  380. let root = getRoot(n)
  381. if root != nil: result = root.name.s[0] == ':'
  382. proc passCopyToSink(n: PNode; c: var Con; s: var Scope): PNode =
  383. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  384. let tmp = c.getTemp(s, n.typ, n.info)
  385. if hasDestructor(c, n.typ):
  386. result.add c.genWasMoved(tmp)
  387. var m = c.genCopy(tmp, n)
  388. m.add p(n, c, s, normal)
  389. c.finishCopy(m, n, isFromSink = true)
  390. result.add m
  391. if isLValue(n) and not isCapturedVar(n) and n.typ.skipTypes(abstractInst).kind != tyRef and c.inSpawn == 0:
  392. message(c.graph.config, n.info, hintPerformance,
  393. ("passing '$1' to a sink parameter introduces an implicit copy; " &
  394. "if possible, rearrange your program's control flow to prevent it") % $n)
  395. else:
  396. if c.graph.config.selectedGC in {gcArc, gcOrc}:
  397. assert(not containsManagedMemory(n.typ))
  398. if n.typ.skipTypes(abstractInst).kind in {tyOpenArray, tyVarargs}:
  399. localError(c.graph.config, n.info, "cannot create an implicit openArray copy to be passed to a sink parameter")
  400. result.add newTree(nkAsgn, tmp, p(n, c, s, normal))
  401. # Since we know somebody will take over the produced copy, there is
  402. # no need to destroy it.
  403. result.add tmp
  404. proc isDangerousSeq(t: PType): bool {.inline.} =
  405. let t = t.skipTypes(abstractInst)
  406. result = t.kind == tySequence and tfHasOwned notin t[0].flags
  407. proc containsConstSeq(n: PNode): bool =
  408. if n.kind == nkBracket and n.len > 0 and n.typ != nil and isDangerousSeq(n.typ):
  409. return true
  410. result = false
  411. case n.kind
  412. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv:
  413. result = containsConstSeq(n[1])
  414. of nkObjConstr, nkClosure:
  415. for i in 1..<n.len:
  416. if containsConstSeq(n[i]): return true
  417. of nkCurly, nkBracket, nkPar, nkTupleConstr:
  418. for son in n:
  419. if containsConstSeq(son): return true
  420. else: discard
  421. proc ensureDestruction(arg, orig: PNode; c: var Con; s: var Scope): PNode =
  422. # it can happen that we need to destroy expression contructors
  423. # like [], (), closures explicitly in order to not leak them.
  424. if arg.typ != nil and hasDestructor(c, arg.typ):
  425. # produce temp creation for (fn, env). But we need to move 'env'?
  426. # This was already done in the sink parameter handling logic.
  427. result = newNodeIT(nkStmtListExpr, arg.info, arg.typ)
  428. let tmp = c.getTemp(s, arg.typ, arg.info)
  429. result.add c.genSink(tmp, arg, isDecl = true)
  430. result.add tmp
  431. s.final.add c.genDestroy(tmp)
  432. else:
  433. result = arg
  434. proc cycleCheck(n: PNode; c: var Con) =
  435. if c.graph.config.selectedGC != gcArc: return
  436. var value = n[1]
  437. if value.kind == nkClosure:
  438. value = value[1]
  439. if value.kind == nkNilLit: return
  440. let destTyp = n[0].typ.skipTypes(abstractInst)
  441. if destTyp.kind != tyRef and not (destTyp.kind == tyProc and destTyp.callConv == ccClosure):
  442. return
  443. var x = n[0]
  444. var field: PNode = nil
  445. while true:
  446. if x.kind == nkDotExpr:
  447. field = x[1]
  448. if field.kind == nkSym and sfCursor in field.sym.flags: return
  449. x = x[0]
  450. elif x.kind in {nkBracketExpr, nkCheckedFieldExpr, nkDerefExpr, nkHiddenDeref}:
  451. x = x[0]
  452. else:
  453. break
  454. if exprStructuralEquivalent(x, value, strictSymEquality = true):
  455. let msg =
  456. if field != nil:
  457. "'$#' creates an uncollectable ref cycle; annotate '$#' with .cursor" % [$n, $field]
  458. else:
  459. "'$#' creates an uncollectable ref cycle" % [$n]
  460. message(c.graph.config, n.info, warnCycleCreated, msg)
  461. break
  462. proc pVarTopLevel(v: PNode; c: var Con; s: var Scope; ri, res: PNode) =
  463. # move the variable declaration to the top of the frame:
  464. s.vars.add v.sym
  465. if isUnpackedTuple(v):
  466. if c.inLoop > 0:
  467. # unpacked tuple needs reset at every loop iteration
  468. res.add newTree(nkFastAsgn, v, genDefaultCall(v.typ, c, v.info))
  469. elif sfThread notin v.sym.flags:
  470. # do not destroy thread vars for now at all for consistency.
  471. if sfGlobal in v.sym.flags and s.parent == nil: #XXX: Rethink this logic (see tarcmisc.test2)
  472. c.graph.globalDestructors.add c.genDestroy(v)
  473. else:
  474. s.final.add c.genDestroy(v)
  475. if ri.kind == nkEmpty and c.inLoop > 0:
  476. res.add moveOrCopy(v, genDefaultCall(v.typ, c, v.info), c, s, isDecl = true)
  477. elif ri.kind != nkEmpty:
  478. res.add moveOrCopy(v, ri, c, s, isDecl = true)
  479. proc processScope(c: var Con; s: var Scope; ret: PNode): PNode =
  480. result = newNodeI(nkStmtList, ret.info)
  481. if s.vars.len > 0:
  482. let varSection = newNodeI(nkVarSection, ret.info)
  483. for tmp in s.vars:
  484. varSection.add newTree(nkIdentDefs, newSymNode(tmp), newNodeI(nkEmpty, ret.info),
  485. newNodeI(nkEmpty, ret.info))
  486. result.add varSection
  487. if s.wasMoved.len > 0 or s.final.len > 0:
  488. let finSection = newNodeI(nkStmtList, ret.info)
  489. for m in s.wasMoved: finSection.add m
  490. for i in countdown(s.final.high, 0): finSection.add s.final[i]
  491. if s.needsTry:
  492. result.add newTryFinally(ret, finSection)
  493. else:
  494. result.add ret
  495. result.add finSection
  496. else:
  497. result.add ret
  498. if s.parent != nil: s.parent[].needsTry = s.parent[].needsTry or s.needsTry
  499. template processScopeExpr(c: var Con; s: var Scope; ret: PNode, processCall: untyped): PNode =
  500. assert not ret.typ.isEmptyType
  501. var result = newNodeI(nkStmtListExpr, ret.info)
  502. # There is a possibility to do this check: s.wasMoved.len > 0 or s.final.len > 0
  503. # later and use it to eliminate the temporary when theres no need for it, but its
  504. # tricky because you would have to intercept moveOrCopy at a certain point
  505. let tmp = c.getTemp(s.parent[], ret.typ, ret.info)
  506. tmp.sym.flags.incl sfSingleUsedTemp
  507. let cpy = if hasDestructor(c, ret.typ):
  508. moveOrCopy(tmp, ret, c, s, isDecl = true)
  509. else:
  510. newTree(nkFastAsgn, tmp, p(ret, c, s, normal))
  511. if s.vars.len > 0:
  512. let varSection = newNodeI(nkVarSection, ret.info)
  513. for tmp in s.vars:
  514. varSection.add newTree(nkIdentDefs, newSymNode(tmp), newNodeI(nkEmpty, ret.info),
  515. newNodeI(nkEmpty, ret.info))
  516. result.add varSection
  517. let finSection = newNodeI(nkStmtList, ret.info)
  518. for m in s.wasMoved: finSection.add m
  519. for i in countdown(s.final.high, 0): finSection.add s.final[i]
  520. if s.needsTry:
  521. result.add newTryFinally(newTree(nkStmtListExpr, cpy, processCall(tmp, s.parent[])), finSection)
  522. else:
  523. result.add cpy
  524. result.add finSection
  525. result.add processCall(tmp, s.parent[])
  526. if s.parent != nil: s.parent[].needsTry = s.parent[].needsTry or s.needsTry
  527. result
  528. template handleNestedTempl(n, processCall: untyped, willProduceStmt = false) =
  529. template maybeVoid(child, s): untyped =
  530. if isEmptyType(child.typ): p(child, c, s, normal)
  531. else: processCall(child, s)
  532. case n.kind
  533. of nkStmtList, nkStmtListExpr:
  534. # a statement list does not open a new scope
  535. if n.len == 0: return n
  536. result = copyNode(n)
  537. for i in 0..<n.len-1:
  538. result.add p(n[i], c, s, normal)
  539. result.add maybeVoid(n[^1], s)
  540. of nkCaseStmt:
  541. result = copyNode(n)
  542. result.add p(n[0], c, s, normal)
  543. for i in 1..<n.len:
  544. let it = n[i]
  545. assert it.kind in {nkOfBranch, nkElse}
  546. var branch = shallowCopy(it)
  547. for j in 0 ..< it.len-1:
  548. branch[j] = copyTree(it[j])
  549. var ofScope = nestedScope(s)
  550. branch[^1] = if it[^1].typ.isEmptyType or willProduceStmt:
  551. processScope(c, ofScope, maybeVoid(it[^1], ofScope))
  552. else:
  553. processScopeExpr(c, ofScope, it[^1], processCall)
  554. result.add branch
  555. of nkWhileStmt:
  556. inc c.inLoop
  557. result = copyNode(n)
  558. result.add p(n[0], c, s, normal)
  559. var bodyScope = nestedScope(s)
  560. let bodyResult = p(n[1], c, bodyScope, normal)
  561. result.add processScope(c, bodyScope, bodyResult)
  562. dec c.inLoop
  563. of nkParForStmt:
  564. inc c.inLoop
  565. result = shallowCopy(n)
  566. let last = n.len-1
  567. for i in 0..<last-1:
  568. result[i] = n[i]
  569. result[last-1] = p(n[last-1], c, s, normal)
  570. var bodyScope = nestedScope(s)
  571. let bodyResult = p(n[last], c, bodyScope, normal)
  572. result[last] = processScope(c, bodyScope, bodyResult)
  573. dec c.inLoop
  574. of nkBlockStmt, nkBlockExpr:
  575. result = copyNode(n)
  576. result.add n[0]
  577. var bodyScope = nestedScope(s)
  578. result.add if n[1].typ.isEmptyType or willProduceStmt:
  579. processScope(c, bodyScope, processCall(n[1], bodyScope))
  580. else:
  581. processScopeExpr(c, bodyScope, n[1], processCall)
  582. of nkIfStmt, nkIfExpr:
  583. result = copyNode(n)
  584. for i in 0..<n.len:
  585. let it = n[i]
  586. var branch = shallowCopy(it)
  587. var branchScope = nestedScope(s)
  588. if it.kind in {nkElifBranch, nkElifExpr}:
  589. #Condition needs to be destroyed outside of the condition/branch scope
  590. branch[0] = p(it[0], c, s, normal)
  591. branch[^1] = if it[^1].typ.isEmptyType or willProduceStmt:
  592. processScope(c, branchScope, maybeVoid(it[^1], branchScope))
  593. else:
  594. processScopeExpr(c, branchScope, it[^1], processCall)
  595. result.add branch
  596. of nkTryStmt:
  597. result = copyNode(n)
  598. var tryScope = nestedScope(s)
  599. result.add if n[0].typ.isEmptyType or willProduceStmt:
  600. processScope(c, tryScope, maybeVoid(n[0], tryScope))
  601. else:
  602. processScopeExpr(c, tryScope, n[0], maybeVoid)
  603. for i in 1..<n.len:
  604. let it = n[i]
  605. var branch = copyTree(it)
  606. var branchScope = nestedScope(s)
  607. branch[^1] = if it[^1].typ.isEmptyType or willProduceStmt or it.kind == nkFinally:
  608. processScope(c, branchScope, if it.kind == nkFinally: p(it[^1], c, branchScope, normal)
  609. else: maybeVoid(it[^1], branchScope))
  610. else:
  611. processScopeExpr(c, branchScope, it[^1], processCall)
  612. result.add branch
  613. of nkWhen: # This should be a "when nimvm" node.
  614. result = copyTree(n)
  615. result[1][0] = processCall(n[1][0], s)
  616. else: assert(false)
  617. proc pRaiseStmt(n: PNode, c: var Con; s: var Scope): PNode =
  618. if optOwnedRefs in c.graph.config.globalOptions and n[0].kind != nkEmpty:
  619. if n[0].kind in nkCallKinds:
  620. let call = p(n[0], c, s, normal)
  621. result = copyNode(n)
  622. result.add call
  623. else:
  624. let tmp = c.getTemp(s, n[0].typ, n.info)
  625. var m = c.genCopyNoCheck(tmp, n[0])
  626. m.add p(n[0], c, s, normal)
  627. c.finishCopy(m, n[0], isFromSink = false)
  628. result = newTree(nkStmtList, c.genWasMoved(tmp), m)
  629. var toDisarm = n[0]
  630. if toDisarm.kind == nkStmtListExpr: toDisarm = toDisarm.lastSon
  631. if toDisarm.kind == nkSym and toDisarm.sym.owner == c.owner:
  632. result.add c.genWasMoved(toDisarm)
  633. result.add newTree(nkRaiseStmt, tmp)
  634. else:
  635. result = copyNode(n)
  636. if n[0].kind != nkEmpty:
  637. result.add p(n[0], c, s, sinkArg)
  638. else:
  639. result.add copyNode(n[0])
  640. s.needsTry = true
  641. proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode): PNode =
  642. if n.kind in {nkStmtList, nkStmtListExpr, nkBlockStmt, nkBlockExpr, nkIfStmt,
  643. nkIfExpr, nkCaseStmt, nkWhen, nkWhileStmt, nkParForStmt, nkTryStmt}:
  644. template process(child, s): untyped = p(child, c, s, mode)
  645. handleNestedTempl(n, process)
  646. elif mode == sinkArg:
  647. if n.containsConstSeq:
  648. # const sequences are not mutable and so we need to pass a copy to the
  649. # sink parameter (bug #11524). Note that the string implementation is
  650. # different and can deal with 'const string sunk into var'.
  651. result = passCopyToSink(n, c, s)
  652. elif n.kind in {nkBracket, nkObjConstr, nkTupleConstr, nkClosure, nkNilLit} +
  653. nkCallKinds + nkLiterals:
  654. result = p(n, c, s, consumed)
  655. elif ((n.kind == nkSym and isSinkParam(n.sym)) or isAnalysableFieldAccess(n, c.owner)) and
  656. isLastRead(n, c) and not (n.kind == nkSym and isCursor(n, c)):
  657. # Sinked params can be consumed only once. We need to reset the memory
  658. # to disable the destructor which we have not elided
  659. result = destructiveMoveVar(n, c, s)
  660. elif n.kind in {nkHiddenSubConv, nkHiddenStdConv, nkConv}:
  661. result = copyTree(n)
  662. if n.typ.skipTypes(abstractInst-{tyOwned}).kind != tyOwned and
  663. n[1].typ.skipTypes(abstractInst-{tyOwned}).kind == tyOwned:
  664. # allow conversions from owned to unowned via this little hack:
  665. let nTyp = n[1].typ
  666. n[1].typ = n.typ
  667. result[1] = p(n[1], c, s, sinkArg)
  668. result[1].typ = nTyp
  669. else:
  670. result[1] = p(n[1], c, s, sinkArg)
  671. elif n.kind in {nkObjDownConv, nkObjUpConv}:
  672. result = copyTree(n)
  673. result[0] = p(n[0], c, s, sinkArg)
  674. elif n.typ == nil:
  675. # 'raise X' can be part of a 'case' expression. Deal with it here:
  676. result = p(n, c, s, normal)
  677. else:
  678. # copy objects that are not temporary but passed to a 'sink' parameter
  679. result = passCopyToSink(n, c, s)
  680. else:
  681. case n.kind
  682. of nkBracket, nkObjConstr, nkTupleConstr, nkClosure, nkCurly:
  683. # Let C(x) be the construction, 'x' the vector of arguments.
  684. # C(x) either owns 'x' or it doesn't.
  685. # If C(x) owns its data, we must consume C(x).
  686. # If it doesn't own the data, it's harmful to destroy it (double frees etc).
  687. # We have the freedom to choose whether it owns it or not so we are smart about it
  688. # and we say, "if passed to a sink we demand C(x) to own its data"
  689. # otherwise we say "C(x) is just some temporary storage, it doesn't own anything,
  690. # don't destroy it"
  691. # but if C(x) is a ref it MUST own its data since we must destroy it
  692. # so then we have no choice but to use 'sinkArg'.
  693. let isRefConstr = n.kind == nkObjConstr and n.typ.skipTypes(abstractInst).kind == tyRef
  694. let m = if isRefConstr: sinkArg
  695. elif mode == normal: normal
  696. else: sinkArg
  697. result = copyTree(n)
  698. for i in ord(n.kind in {nkObjConstr, nkClosure})..<n.len:
  699. if n[i].kind == nkExprColonExpr:
  700. result[i][1] = p(n[i][1], c, s, m)
  701. elif n[i].kind == nkRange:
  702. result[i][0] = p(n[i][0], c, s, m)
  703. result[i][1] = p(n[i][1], c, s, m)
  704. else:
  705. result[i] = p(n[i], c, s, m)
  706. if mode == normal and isRefConstr:
  707. result = ensureDestruction(result, n, c, s)
  708. of nkCallKinds:
  709. let inSpawn = c.inSpawn
  710. if n[0].kind == nkSym and n[0].sym.magic == mSpawn:
  711. c.inSpawn.inc
  712. elif c.inSpawn > 0:
  713. c.inSpawn.dec
  714. let parameters = n[0].typ
  715. let L = if parameters != nil: parameters.len else: 0
  716. when false:
  717. var isDangerous = false
  718. if n[0].kind == nkSym and n[0].sym.magic in {mOr, mAnd}:
  719. inc c.inDangerousBranch
  720. isDangerous = true
  721. result = shallowCopy(n)
  722. for i in 1..<n.len:
  723. if i < L and isCompileTimeOnly(parameters[i]):
  724. result[i] = n[i]
  725. elif i < L and (isSinkTypeForParam(parameters[i]) or inSpawn > 0):
  726. result[i] = p(n[i], c, s, sinkArg)
  727. else:
  728. result[i] = p(n[i], c, s, normal)
  729. when false:
  730. if isDangerous:
  731. dec c.inDangerousBranch
  732. if n[0].kind == nkSym and n[0].sym.magic in {mNew, mNewFinalize}:
  733. result[0] = copyTree(n[0])
  734. if c.graph.config.selectedGC in {gcHooks, gcArc, gcOrc}:
  735. let destroyOld = c.genDestroy(result[1])
  736. result = newTree(nkStmtList, destroyOld, result)
  737. else:
  738. result[0] = p(n[0], c, s, normal)
  739. if canRaise(n[0]): s.needsTry = true
  740. if mode == normal:
  741. result = ensureDestruction(result, n, c, s)
  742. of nkDiscardStmt: # Small optimization
  743. result = shallowCopy(n)
  744. if n[0].kind != nkEmpty:
  745. result[0] = p(n[0], c, s, normal)
  746. else:
  747. result[0] = copyNode(n[0])
  748. of nkVarSection, nkLetSection:
  749. # transform; var x = y to var x; x op y where op is a move or copy
  750. result = newNodeI(nkStmtList, n.info)
  751. for it in n:
  752. var ri = it[^1]
  753. if it.kind == nkVarTuple and hasDestructor(c, ri.typ):
  754. let x = lowerTupleUnpacking(c.graph, it, c.idgen, c.owner)
  755. result.add p(x, c, s, consumed)
  756. elif it.kind == nkIdentDefs and hasDestructor(c, it[0].typ) and not isCursor(it[0], c):
  757. for j in 0..<it.len-2:
  758. let v = it[j]
  759. if v.kind == nkSym:
  760. if sfCompileTime in v.sym.flags: continue
  761. pVarTopLevel(v, c, s, ri, result)
  762. else:
  763. if ri.kind == nkEmpty and c.inLoop > 0:
  764. ri = genDefaultCall(v.typ, c, v.info)
  765. if ri.kind != nkEmpty:
  766. result.add moveOrCopy(v, ri, c, s, isDecl = false)
  767. else: # keep the var but transform 'ri':
  768. var v = copyNode(n)
  769. var itCopy = copyNode(it)
  770. for j in 0..<it.len-1:
  771. itCopy.add it[j]
  772. itCopy.add p(it[^1], c, s, normal)
  773. v.add itCopy
  774. result.add v
  775. of nkAsgn, nkFastAsgn:
  776. if hasDestructor(c, n[0].typ) and n[1].kind notin {nkProcDef, nkDo, nkLambda} and
  777. not isCursor(n[0], c):
  778. if n[0].kind in {nkDotExpr, nkCheckedFieldExpr}:
  779. cycleCheck(n, c)
  780. assert n[1].kind notin {nkAsgn, nkFastAsgn}
  781. result = moveOrCopy(p(n[0], c, s, mode), n[1], c, s)
  782. elif isDiscriminantField(n[0]):
  783. result = c.genDiscriminantAsgn(s, n)
  784. else:
  785. result = copyNode(n)
  786. result.add p(n[0], c, s, mode)
  787. result.add p(n[1], c, s, consumed)
  788. of nkRaiseStmt:
  789. result = pRaiseStmt(n, c, s)
  790. of nkWhileStmt:
  791. internalError(c.graph.config, n.info, "nkWhileStmt should have been handled earlier")
  792. result = n
  793. of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef,
  794. nkMethodDef, nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo,
  795. nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
  796. nkExportStmt, nkPragma, nkCommentStmt, nkBreakState, nkTypeOfExpr:
  797. result = n
  798. of nkStringToCString, nkCStringToString, nkChckRangeF, nkChckRange64, nkChckRange, nkPragmaBlock:
  799. result = shallowCopy(n)
  800. for i in 0 ..< n.len:
  801. result[i] = p(n[i], c, s, normal)
  802. if n.typ != nil and hasDestructor(c, n.typ):
  803. if mode == normal:
  804. result = ensureDestruction(result, n, c, s)
  805. of nkHiddenSubConv, nkHiddenStdConv, nkConv:
  806. # we have an "ownership invariance" for all constructors C(x).
  807. # See the comment for nkBracket construction. If the caller wants
  808. # to own 'C(x)', it really wants to own 'x' too. If it doesn't,
  809. # we need to destroy 'x' but the function call handling ensures that
  810. # already.
  811. result = copyTree(n)
  812. if n.typ.skipTypes(abstractInst-{tyOwned}).kind != tyOwned and
  813. n[1].typ.skipTypes(abstractInst-{tyOwned}).kind == tyOwned:
  814. # allow conversions from owned to unowned via this little hack:
  815. let nTyp = n[1].typ
  816. n[1].typ = n.typ
  817. result[1] = p(n[1], c, s, mode)
  818. result[1].typ = nTyp
  819. else:
  820. result[1] = p(n[1], c, s, mode)
  821. of nkObjDownConv, nkObjUpConv:
  822. result = copyTree(n)
  823. result[0] = p(n[0], c, s, mode)
  824. of nkDotExpr:
  825. result = shallowCopy(n)
  826. result[0] = p(n[0], c, s, normal)
  827. for i in 1 ..< n.len:
  828. result[i] = n[i]
  829. if mode == sinkArg and hasDestructor(c, n.typ):
  830. if isAnalysableFieldAccess(n, c.owner) and isLastRead(n, c):
  831. s.wasMoved.add c.genWasMoved(n)
  832. else:
  833. result = passCopyToSink(result, c, s)
  834. of nkBracketExpr, nkAddr, nkHiddenAddr, nkDerefExpr, nkHiddenDeref:
  835. result = shallowCopy(n)
  836. for i in 0 ..< n.len:
  837. result[i] = p(n[i], c, s, normal)
  838. if mode == sinkArg and hasDestructor(c, n.typ):
  839. if isAnalysableFieldAccess(n, c.owner) and isLastRead(n, c):
  840. # consider 'a[(g; destroy(g); 3)]', we want to say 'wasMoved(a[3])'
  841. # without the junk, hence 'c.genWasMoved(n)'
  842. # and not 'c.genWasMoved(result)':
  843. s.wasMoved.add c.genWasMoved(n)
  844. else:
  845. result = passCopyToSink(result, c, s)
  846. of nkDefer, nkRange:
  847. result = shallowCopy(n)
  848. for i in 0 ..< n.len:
  849. result[i] = p(n[i], c, s, normal)
  850. of nkBreakStmt:
  851. s.needsTry = true
  852. result = n
  853. of nkReturnStmt:
  854. result = shallowCopy(n)
  855. for i in 0..<n.len:
  856. result[i] = p(n[i], c, s, mode)
  857. s.needsTry = true
  858. of nkCast:
  859. result = shallowCopy(n)
  860. result[0] = n[0]
  861. result[1] = p(n[1], c, s, mode)
  862. of nkCheckedFieldExpr:
  863. result = shallowCopy(n)
  864. result[0] = p(n[0], c, s, mode)
  865. for i in 1..<n.len:
  866. result[i] = n[i]
  867. of nkGotoState, nkState, nkAsmStmt:
  868. result = n
  869. else:
  870. internalError(c.graph.config, n.info, "cannot inject destructors to node kind: " & $n.kind)
  871. proc moveOrCopy(dest, ri: PNode; c: var Con; s: var Scope, isDecl = false): PNode =
  872. case ri.kind
  873. of nkCallKinds:
  874. result = c.genSink(dest, p(ri, c, s, consumed), isDecl)
  875. of nkBracketExpr:
  876. if isUnpackedTuple(ri[0]):
  877. # unpacking of tuple: take over the elements
  878. result = c.genSink(dest, p(ri, c, s, consumed), isDecl)
  879. elif isAnalysableFieldAccess(ri, c.owner) and isLastRead(ri, c) and
  880. not aliases(dest, ri):
  881. # Rule 3: `=sink`(x, z); wasMoved(z)
  882. var snk = c.genSink(dest, ri, isDecl)
  883. result = newTree(nkStmtList, snk, c.genWasMoved(ri))
  884. else:
  885. result = c.genCopy(dest, ri)
  886. result.add p(ri, c, s, consumed)
  887. c.finishCopy(result, dest, isFromSink = false)
  888. of nkBracket:
  889. # array constructor
  890. if ri.len > 0 and isDangerousSeq(ri.typ):
  891. result = c.genCopy(dest, ri)
  892. result.add p(ri, c, s, consumed)
  893. c.finishCopy(result, dest, isFromSink = false)
  894. else:
  895. result = c.genSink(dest, p(ri, c, s, consumed), isDecl)
  896. of nkObjConstr, nkTupleConstr, nkClosure, nkCharLit..nkNilLit:
  897. result = c.genSink(dest, p(ri, c, s, consumed), isDecl)
  898. of nkSym:
  899. if dest.kind == nkSym and dest.sym == ri.sym:
  900. # rule (self-assignment-removal):
  901. result = newNodeI(nkEmpty, dest.info)
  902. elif isSinkParam(ri.sym) and isLastRead(ri, c):
  903. # Rule 3: `=sink`(x, z); wasMoved(z)
  904. let snk = c.genSink(dest, ri, isDecl)
  905. result = newTree(nkStmtList, snk, c.genWasMoved(ri))
  906. elif ri.sym.kind != skParam and ri.sym.owner == c.owner and
  907. isLastRead(ri, c) and canBeMoved(c, dest.typ) and not isCursor(ri, c):
  908. # Rule 3: `=sink`(x, z); wasMoved(z)
  909. let snk = c.genSink(dest, ri, isDecl)
  910. result = newTree(nkStmtList, snk, c.genWasMoved(ri))
  911. else:
  912. result = c.genCopy(dest, ri)
  913. result.add p(ri, c, s, consumed)
  914. c.finishCopy(result, dest, isFromSink = false)
  915. of nkHiddenSubConv, nkHiddenStdConv, nkConv, nkObjDownConv, nkObjUpConv:
  916. result = c.genSink(dest, p(ri, c, s, sinkArg), isDecl)
  917. of nkStmtListExpr, nkBlockExpr, nkIfExpr, nkCaseStmt, nkTryStmt:
  918. template process(child, s): untyped = moveOrCopy(dest, child, c, s, isDecl)
  919. # We know the result will be a stmt so we use that fact to optimize
  920. handleNestedTempl(ri, process, willProduceStmt = true)
  921. of nkRaiseStmt:
  922. result = pRaiseStmt(ri, c, s)
  923. else:
  924. if isAnalysableFieldAccess(ri, c.owner) and isLastRead(ri, c) and
  925. canBeMoved(c, dest.typ):
  926. # Rule 3: `=sink`(x, z); wasMoved(z)
  927. let snk = c.genSink(dest, ri, isDecl)
  928. result = newTree(nkStmtList, snk, c.genWasMoved(ri))
  929. else:
  930. result = c.genCopy(dest, ri)
  931. result.add p(ri, c, s, consumed)
  932. c.finishCopy(result, dest, isFromSink = false)
  933. proc computeUninit(c: var Con) =
  934. if not c.uninitComputed:
  935. c.uninitComputed = true
  936. c.uninit = initIntSet()
  937. var init = initIntSet()
  938. discard initialized(c.g, pc = 0, init, c.uninit, int.high)
  939. proc injectDefaultCalls(n: PNode, c: var Con) =
  940. case n.kind
  941. of nkVarSection, nkLetSection:
  942. for it in n:
  943. if it.kind == nkIdentDefs and it[^1].kind == nkEmpty:
  944. computeUninit(c)
  945. for j in 0..<it.len-2:
  946. let v = it[j]
  947. doAssert v.kind == nkSym
  948. if c.uninit.contains(v.sym.id):
  949. it[^1] = genDefaultCall(v.sym.typ, c, v.info)
  950. break
  951. of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef,
  952. nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo, nkFuncDef:
  953. discard
  954. else:
  955. for i in 0..<n.safeLen:
  956. injectDefaultCalls(n[i], c)
  957. proc injectDestructorCalls*(g: ModuleGraph; idgen: IdGenerator; owner: PSym; n: PNode): PNode =
  958. if sfGeneratedOp in owner.flags or (owner.kind == skIterator and isInlineIterator(owner.typ)):
  959. return n
  960. var c = Con(owner: owner, graph: g, g: constructCfg(owner, n), idgen: idgen)
  961. dbg:
  962. echo "\n### ", owner.name.s, ":\nCFG:"
  963. echoCfg(c.g)
  964. echo n
  965. if optCursorInference in g.config.options:
  966. computeCursors(owner, n, g.config)
  967. var scope: Scope
  968. let body = p(n, c, scope, normal)
  969. if owner.kind in {skProc, skFunc, skMethod, skIterator, skConverter}:
  970. let params = owner.typ.n
  971. for i in 1..<params.len:
  972. let t = params[i].sym.typ
  973. if isSinkTypeForParam(t) and hasDestructor(c, t.skipTypes({tySink})):
  974. scope.final.add c.genDestroy(params[i])
  975. #if optNimV2 in c.graph.config.globalOptions:
  976. # injectDefaultCalls(n, c)
  977. result = optimize processScope(c, scope, body)
  978. dbg:
  979. echo ">---------transformed-to--------->"
  980. echo renderTree(result, {renderIds})
  981. if g.config.arcToExpand.hasKey(owner.name.s):
  982. echo "--expandArc: ", owner.name.s
  983. echo renderTree(result, {renderIr, renderNoComments})
  984. echo "-- end of expandArc ------------------------"