injectdestructors.nim 44 KB

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