liftdestructors.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements lifting for type-bound operations
  10. ## (``=sink``, ``=``, ``=destroy``, ``=deepCopy``).
  11. # included from sem.nim
  12. type
  13. TLiftCtx = object
  14. graph: ModuleGraph
  15. info: TLineInfo # for construction
  16. kind: TTypeAttachedOp
  17. fn: PSym
  18. asgnForType: PType
  19. recurse: bool
  20. proc liftBodyAux(c: var TLiftCtx; t: PType; body, x, y: PNode)
  21. proc liftBody(g: ModuleGraph; typ: PType; kind: TTypeAttachedOp;
  22. info: TLineInfo): PSym {.discardable.}
  23. proc at(a, i: PNode, elemType: PType): PNode =
  24. result = newNodeI(nkBracketExpr, a.info, 2)
  25. result.sons[0] = a
  26. result.sons[1] = i
  27. result.typ = elemType
  28. proc liftBodyTup(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  29. for i in 0 ..< t.len:
  30. let lit = lowerings.newIntLit(c.graph, x.info, i)
  31. liftBodyAux(c, t.sons[i], body, x.at(lit, t.sons[i]), y.at(lit, t.sons[i]))
  32. proc dotField(x: PNode, f: PSym): PNode =
  33. result = newNodeI(nkDotExpr, x.info, 2)
  34. result.sons[0] = x
  35. result.sons[1] = newSymNode(f, x.info)
  36. result.typ = f.typ
  37. proc liftBodyObj(c: var TLiftCtx; n, body, x, y: PNode) =
  38. case n.kind
  39. of nkSym:
  40. let f = n.sym
  41. liftBodyAux(c, f.typ, body, x.dotField(f), y.dotField(f))
  42. of nkNilLit: discard
  43. of nkRecCase:
  44. if c.kind in {attachedSink, attachedAsgn, attachedDeepCopy}:
  45. ## the value needs to be destroyed before we assign the selector
  46. ## or the value is lost
  47. let prevKind = c.kind
  48. c.kind = attachedDestructor
  49. liftBodyObj(c, n, body, x, y)
  50. c.kind = prevKind
  51. # copy the selector:
  52. liftBodyObj(c, n[0], body, x, y)
  53. # we need to generate a case statement:
  54. var caseStmt = newNodeI(nkCaseStmt, c.info)
  55. # XXX generate 'if' that checks same branches
  56. # generate selector:
  57. var access = dotField(x, n[0].sym)
  58. caseStmt.add(access)
  59. # copy the branches over, but replace the fields with the for loop body:
  60. for i in 1 ..< n.len:
  61. var branch = copyTree(n[i])
  62. let L = branch.len
  63. branch.sons[L-1] = newNodeI(nkStmtList, c.info)
  64. liftBodyObj(c, n[i].lastSon, branch.sons[L-1], x, y)
  65. caseStmt.add(branch)
  66. body.add(caseStmt)
  67. of nkRecList:
  68. for t in items(n): liftBodyObj(c, t, body, x, y)
  69. else:
  70. illFormedAstLocal(n, c.graph.config)
  71. proc genAddr(g: ModuleGraph; x: PNode): PNode =
  72. if x.kind == nkHiddenDeref:
  73. checkSonsLen(x, 1, g.config)
  74. result = x.sons[0]
  75. else:
  76. result = newNodeIT(nkHiddenAddr, x.info, makeVarType(x.typ.owner, x.typ))
  77. addSon(result, x)
  78. proc newAsgnCall(g: ModuleGraph; op: PSym; x, y: PNode): PNode =
  79. #if sfError in op.flags:
  80. # localError(c.config, x.info, "usage of '$1' is a user-defined error" % op.name.s)
  81. result = newNodeI(nkCall, x.info)
  82. result.add newSymNode(op)
  83. result.add genAddr(g, x)
  84. result.add y
  85. proc newAsgnStmt(le, ri: PNode): PNode =
  86. result = newNodeI(nkAsgn, le.info, 2)
  87. result.sons[0] = le
  88. result.sons[1] = ri
  89. proc newOpCall(op: PSym; x: PNode): PNode =
  90. result = newNodeIT(nkCall, x.info, op.typ.sons[0])
  91. result.add(newSymNode(op))
  92. result.add x
  93. proc destructorCall(g: ModuleGraph; op: PSym; x: PNode): PNode =
  94. result = newNodeIT(nkCall, x.info, op.typ.sons[0])
  95. result.add(newSymNode(op))
  96. result.add genAddr(g, x)
  97. proc newDeepCopyCall(op: PSym; x, y: PNode): PNode =
  98. result = newAsgnStmt(x, newOpCall(op, y))
  99. proc useNoGc(c: TLiftCtx; t: PType): bool {.inline.} =
  100. result = optNimV2 in c.graph.config.globalOptions and
  101. (tfHasGCedMem in t.flags or t.isGCedMem)
  102. proc considerAsgnOrSink(c: var TLiftCtx; t: PType; body, x, y: PNode;
  103. field: PSym): bool =
  104. if tfHasAsgn in t.flags or useNoGc(c, t):
  105. var op: PSym
  106. if sameType(t, c.asgnForType):
  107. # generate recursive call:
  108. if c.recurse:
  109. op = c.fn
  110. else:
  111. c.recurse = true
  112. return false
  113. else:
  114. op = field
  115. if op == nil:
  116. op = liftBody(c.graph, t, c.kind, c.info)
  117. if sfError in op.flags:
  118. incl c.fn.flags, sfError
  119. else:
  120. markUsed(c.graph.config, c.info, op, c.graph.usageSym)
  121. onUse(c.info, op)
  122. body.add newAsgnCall(c.graph, op, x, y)
  123. result = true
  124. proc addDestructorCall(c: var TLiftCtx; t: PType; body, x: PNode): bool =
  125. let op = t.destructor
  126. if op != nil:
  127. markUsed(c.graph.config, c.info, op, c.graph.usageSym)
  128. onUse(c.info, op)
  129. body.add destructorCall(c.graph, op, x)
  130. result = true
  131. elif useNoGc(c, t):
  132. if sameType(t, c.asgnForType) and c.kind == attachedDestructor:
  133. let op = c.fn
  134. markUsed(c.graph.config, c.info, op, c.graph.usageSym)
  135. onUse(c.info, op)
  136. body.add destructorCall(c.graph, op, x)
  137. result = true
  138. else:
  139. internalError(c.graph.config, c.info,
  140. "type-bound operator could not be resolved")
  141. proc considerOverloadedOp(c: var TLiftCtx; t: PType; body, x, y: PNode): bool =
  142. case c.kind
  143. of attachedDestructor:
  144. result = addDestructorCall(c, t, body, x)
  145. of attachedAsgn:
  146. result = considerAsgnOrSink(c, t, body, x, y, t.assignment)
  147. of attachedSink:
  148. result = considerAsgnOrSink(c, t, body, x, y, t.sink)
  149. of attachedDeepCopy:
  150. let op = t.deepCopy
  151. if op != nil:
  152. markUsed(c.graph.config, c.info, op, c.graph.usageSym)
  153. onUse(c.info, op)
  154. body.add newDeepCopyCall(op, x, y)
  155. result = true
  156. proc defaultOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  157. if c.kind != attachedDestructor:
  158. body.add newAsgnStmt(x, y)
  159. proc addVar(father, v, value: PNode) =
  160. var vpart = newNodeI(nkIdentDefs, v.info, 3)
  161. vpart.sons[0] = v
  162. vpart.sons[1] = newNodeI(nkEmpty, v.info)
  163. vpart.sons[2] = value
  164. addSon(father, vpart)
  165. proc declareCounter(c: var TLiftCtx; body: PNode; first: BiggestInt): PNode =
  166. var temp = newSym(skTemp, getIdent(c.graph.cache, lowerings.genPrefix), c.fn, c.info)
  167. temp.typ = getSysType(c.graph, body.info, tyInt)
  168. incl(temp.flags, sfFromGeneric)
  169. var v = newNodeI(nkVarSection, c.info)
  170. result = newSymNode(temp)
  171. v.addVar(result, lowerings.newIntLit(c.graph, body.info, first))
  172. body.add v
  173. proc genBuiltin(g: ModuleGraph; magic: TMagic; name: string; i: PNode): PNode =
  174. result = newNodeI(nkCall, i.info)
  175. result.add createMagic(g, name, magic).newSymNode
  176. result.add i
  177. proc genWhileLoop(c: var TLiftCtx; i, dest: PNode): PNode =
  178. result = newNodeI(nkWhileStmt, c.info, 2)
  179. let cmp = genBuiltin(c.graph, mLtI, "<", i)
  180. cmp.add genLen(c.graph, dest)
  181. cmp.typ = getSysType(c.graph, c.info, tyBool)
  182. result.sons[0] = cmp
  183. result.sons[1] = newNodeI(nkStmtList, c.info)
  184. proc genIf(c: var TLiftCtx; cond, action: PNode): PNode =
  185. result = newTree(nkIfStmt, newTree(nkElifBranch, cond, action))
  186. proc addIncStmt(c: var TLiftCtx; body, i: PNode) =
  187. let incCall = genBuiltin(c.graph, mInc, "inc", i)
  188. incCall.add lowerings.newIntLit(c.graph, c.info, 1)
  189. body.add incCall
  190. proc newSeqCall(g: ModuleGraph; x, y: PNode): PNode =
  191. # don't call genAddr(c, x) here:
  192. result = genBuiltin(g, mNewSeq, "newSeq", x)
  193. let lenCall = genBuiltin(g, mLengthSeq, "len", y)
  194. lenCall.typ = getSysType(g, x.info, tyInt)
  195. result.add lenCall
  196. proc setLenCall(g: ModuleGraph; x, y: PNode): PNode =
  197. let lenCall = genBuiltin(g, mLengthSeq, "len", y)
  198. lenCall.typ = getSysType(g, x.info, tyInt)
  199. result = genBuiltin(g, mSetLengthSeq, "setLen", genAddr(g, x))
  200. result.add lenCall
  201. proc forallElements(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  202. let i = declareCounter(c, body, firstOrd(c.graph.config, t))
  203. let whileLoop = genWhileLoop(c, i, x)
  204. let elemType = t.lastSon
  205. liftBodyAux(c, elemType, whileLoop.sons[1], x.at(i, elemType),
  206. y.at(i, elemType))
  207. addIncStmt(c, whileLoop.sons[1], i)
  208. body.add whileLoop
  209. proc seqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  210. case c.kind
  211. of attachedAsgn, attachedDeepCopy:
  212. # we generate:
  213. # setLen(dest, y.len)
  214. # var i = 0
  215. # while i < y.len: dest[i] = y[i]; inc(i)
  216. # This is usually more efficient than a destroy/create pair.
  217. body.add setLenCall(c.graph, x, y)
  218. forallElements(c, t, body, x, y)
  219. of attachedSink:
  220. let moveCall = genBuiltin(c.graph, mMove, "move", x)
  221. moveCall.add y
  222. doAssert t.destructor != nil
  223. moveCall.add destructorCall(c.graph, t.destructor, x)
  224. body.add moveCall
  225. when false:
  226. # we generate:
  227. # if a.len != 0 and a.p != b.p:
  228. # `=destroy`(x)
  229. # a.len = b.len
  230. # a.p = b.p
  231. # Note: '@' is either '.' or '->'.
  232. body.add genIf(c, genVerbatim("dest@len != 0 && dest@p != src.p", c.info),
  233. destructorCall(c.graph, t.destructor, x))
  234. body.add genVerbatim("dest@len=src.len; dest@p=src.p;", c.info)
  235. of attachedDestructor:
  236. # destroy all elements:
  237. forallElements(c, t, body, x, y)
  238. body.add genBuiltin(c.graph, mDestroy, "destroy", x)
  239. when false:
  240. var deallocStmt = genVerbatim("dest@region->dealloc(dest@region, dest@p, " &
  241. "(dest@p->cap * sizeof($)) + sizeof(NI) + sizeof(void*)); dest@len = 0;", c.info)
  242. deallocStmt.typ = t.lastSon
  243. body.add genIf(c, genVerbatim("dest@len != 0 && dest@region", c.info), deallocStmt)
  244. proc strOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  245. seqOp(c, t, body, x, y)
  246. proc weakrefOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  247. case c.kind
  248. of attachedSink:
  249. # we 'nil' y out afterwards so we *need* to take over its reference
  250. # count value:
  251. body.add genIf(c, x, callCodegenProc(c.graph, "nimDecWeakRef", c.info, x))
  252. body.add newAsgnStmt(x, y)
  253. of attachedAsgn:
  254. body.add callCodegenProc(c.graph, "nimIncWeakRef", c.info, y)
  255. body.add genIf(c, x, callCodegenProc(c.graph, "nimDecWeakRef", c.info, x))
  256. body.add newAsgnStmt(x, y)
  257. of attachedDestructor:
  258. body.add genIf(c, x, callCodegenProc(c.graph, "nimDecWeakRef", c.info, x))
  259. of attachedDeepCopy: assert(false, "cannot happen")
  260. proc ownedRefOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  261. var actions = newNodeI(nkStmtList, c.info)
  262. let elemType = t.lastSon
  263. #liftBodyAux(c, elemType, actions, genDeref(x), genDeref(y))
  264. #var disposeCall = genBuiltin(c.graph, mDispose, "dispose", x)
  265. if isFinal(elemType):
  266. discard addDestructorCall(c, elemType, actions, x)
  267. actions.add callCodegenProc(c.graph, "nimRawDispose", c.info, x)
  268. else:
  269. discard addDestructorCall(c, elemType, newNodeI(nkStmtList, c.info), x)
  270. actions.add callCodegenProc(c.graph, "nimDestroyAndDispose", c.info, x)
  271. case c.kind
  272. of attachedSink, attachedAsgn:
  273. body.add genIf(c, x, actions)
  274. body.add newAsgnStmt(x, y)
  275. of attachedDestructor:
  276. body.add genIf(c, x, actions)
  277. of attachedDeepCopy: assert(false, "cannot happen")
  278. proc closureOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  279. if c.kind == attachedDeepCopy:
  280. # a big problem is that we don't know the enviroment's type here, so we
  281. # have to go through some indirection; we delegate this to the codegen:
  282. let call = newNodeI(nkCall, c.info, 2)
  283. call.typ = t
  284. call.sons[0] = newSymNode(createMagic(c.graph, "deepCopy", mDeepCopy))
  285. call.sons[1] = y
  286. body.add newAsgnStmt(x, call)
  287. elif optNimV2 in c.graph.config.globalOptions:
  288. case c.kind
  289. of attachedSink, attachedAsgn: discard
  290. of attachedDestructor: discard
  291. of attachedDeepCopy: assert(false, "cannot happen")
  292. proc ownedClosureOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  293. discard "to implement"
  294. proc liftBodyAux(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  295. case t.kind
  296. of tyNone, tyEmpty, tyVoid: discard
  297. of tyPointer, tySet, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCString,
  298. tyPtr, tyOpt, tyUncheckedArray:
  299. defaultOp(c, t, body, x, y)
  300. of tyRef:
  301. if optNimV2 in c.graph.config.globalOptions:
  302. weakrefOp(c, t, body, x, y)
  303. else:
  304. defaultOp(c, t, body, x, y)
  305. of tyProc:
  306. if t.callConv == ccClosure:
  307. closureOp(c, t, body, x, y)
  308. else:
  309. defaultOp(c, t, body, x, y)
  310. of tyOwned:
  311. let base = t.skipTypes(abstractInstOwned)
  312. if optNimV2 in c.graph.config.globalOptions:
  313. case base.kind
  314. of tyRef:
  315. ownedRefOp(c, base, body, x, y)
  316. return
  317. of tyProc:
  318. if base.callConv == ccClosure:
  319. ownedClosureOp(c, base, body, x, y)
  320. return
  321. else: discard
  322. defaultOp(c, base, body, x, y)
  323. of tyArray:
  324. if tfHasAsgn in t.flags or useNoGc(c, t):
  325. forallElements(c, t, body, x, y)
  326. else:
  327. defaultOp(c, t, body, x, y)
  328. of tySequence:
  329. if useNoGc(c, t):
  330. seqOp(c, t, body, x, y)
  331. elif c.graph.config.selectedGC == gcDestructors:
  332. # note that tfHasAsgn is propagated so we need the check on
  333. # 'selectedGC' here to determine if we have the new runtime.
  334. discard considerOverloadedOp(c, t, body, x, y)
  335. elif tfHasAsgn in t.flags:
  336. if c.kind != attachedDestructor:
  337. body.add newSeqCall(c.graph, x, y)
  338. forallElements(c, t, body, x, y)
  339. else:
  340. defaultOp(c, t, body, x, y)
  341. of tyString:
  342. if useNoGc(c, t):
  343. strOp(c, t, body, x, y)
  344. elif tfHasAsgn in t.flags:
  345. discard considerOverloadedOp(c, t, body, x, y)
  346. else:
  347. defaultOp(c, t, body, x, y)
  348. of tyObject:
  349. if not considerOverloadedOp(c, t, body, x, y):
  350. liftBodyObj(c, t.n, body, x, y)
  351. of tyDistinct:
  352. if not considerOverloadedOp(c, t, body, x, y):
  353. liftBodyAux(c, t.sons[0].skipTypes(skipPtrs), body, x, y)
  354. of tyTuple:
  355. liftBodyTup(c, t, body, x, y)
  356. of tyVarargs, tyOpenArray:
  357. localError(c.graph.config, c.info, "cannot copy openArray")
  358. of tyFromExpr, tyProxy, tyBuiltInTypeClass, tyUserTypeClass,
  359. tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot, tyAnything,
  360. tyGenericParam, tyGenericBody, tyNil, tyExpr, tyStmt,
  361. tyTypeDesc, tyGenericInvocation, tyForward:
  362. internalError(c.graph.config, c.info, "assignment requested for type: " & typeToString(t))
  363. of tyOrdinal, tyRange, tyInferred,
  364. tyGenericInst, tyStatic, tyVar, tyLent, tyAlias, tySink:
  365. liftBodyAux(c, lastSon(t), body, x, y)
  366. proc newProcType(info: TLineInfo; owner: PSym): PType =
  367. result = newType(tyProc, owner)
  368. result.n = newNodeI(nkFormalParams, info)
  369. rawAddSon(result, nil) # return type
  370. # result.n[0] used to be `nkType`, but now it's `nkEffectList` because
  371. # the effects are now stored in there too ... this is a bit hacky, but as
  372. # usual we desperately try to save memory:
  373. addSon(result.n, newNodeI(nkEffectList, info))
  374. proc addParam(procType: PType; param: PSym) =
  375. param.position = procType.len-1
  376. addSon(procType.n, newSymNode(param))
  377. rawAddSon(procType, param.typ)
  378. proc liftBodyDistinctType(g: ModuleGraph; typ: PType; kind: TTypeAttachedOp; info: TLineInfo): PSym =
  379. assert typ.kind == tyDistinct
  380. let baseType = typ[0]
  381. case kind
  382. of attachedAsgn:
  383. if baseType.assignment == nil:
  384. discard liftBody(g, baseType, kind, info)
  385. typ.assignment = baseType.assignment
  386. result = typ.assignment
  387. of attachedSink:
  388. if baseType.sink == nil:
  389. discard liftBody(g, baseType, kind, info)
  390. typ.sink = baseType.sink
  391. result = typ.sink
  392. of attachedDeepCopy:
  393. if baseType.deepCopy == nil:
  394. discard liftBody(g, baseType, kind, info)
  395. typ.deepCopy = baseType.deepCopy
  396. result = typ.deepCopy
  397. of attachedDestructor:
  398. if baseType.destructor == nil:
  399. discard liftBody(g, baseType, kind, info)
  400. typ.destructor = baseType.destructor
  401. result = typ.destructor
  402. proc liftBody(g: ModuleGraph; typ: PType; kind: TTypeAttachedOp;
  403. info: TLineInfo): PSym =
  404. if typ.kind == tyDistinct:
  405. return liftBodyDistinctType(g, typ, kind, info)
  406. when false:
  407. var typ = typ
  408. if c.config.selectedGC == gcDestructors and typ.kind == tySequence:
  409. # use the canonical type to access the =sink and =destroy etc.
  410. typ = c.graph.sysTypes[tySequence]
  411. var a: TLiftCtx
  412. a.info = info
  413. a.graph = g
  414. a.kind = kind
  415. let body = newNodeI(nkStmtList, info)
  416. let procname = case kind
  417. of attachedAsgn: getIdent(g.cache, "=")
  418. of attachedSink: getIdent(g.cache, "=sink")
  419. of attachedDeepCopy: getIdent(g.cache, "=deepcopy")
  420. of attachedDestructor: getIdent(g.cache, "=destroy")
  421. result = newSym(skProc, procname, typ.owner, info)
  422. a.fn = result
  423. a.asgnForType = typ
  424. let dest = newSym(skParam, getIdent(g.cache, "dest"), result, info)
  425. let src = newSym(skParam, getIdent(g.cache, "src"), result, info)
  426. dest.typ = makeVarType(typ.owner, typ)
  427. src.typ = typ
  428. result.typ = newProcType(info, typ.owner)
  429. result.typ.addParam dest
  430. if kind != attachedDestructor:
  431. result.typ.addParam src
  432. liftBodyAux(a, typ, body, newSymNode(dest).newDeref, newSymNode(src))
  433. # recursion is handled explicitly, do not register the type based operation
  434. # before 'liftBodyAux':
  435. if g.config.selectedGC == gcDestructors and
  436. typ.kind in {tySequence, tyString} and body.len == 0:
  437. discard "do not cache it yet"
  438. else:
  439. case kind
  440. of attachedAsgn: typ.assignment = result
  441. of attachedSink: typ.sink = result
  442. of attachedDeepCopy: typ.deepCopy = result
  443. of attachedDestructor: typ.destructor = result
  444. var n = newNodeI(nkProcDef, info, bodyPos+1)
  445. for i in 0 ..< n.len: n.sons[i] = newNodeI(nkEmpty, info)
  446. n.sons[namePos] = newSymNode(result)
  447. n.sons[paramsPos] = result.typ.n
  448. n.sons[bodyPos] = body
  449. result.ast = n
  450. incl result.flags, sfFromGeneric
  451. proc getAsgnOrLiftBody(g: ModuleGraph; typ: PType; info: TLineInfo): PSym =
  452. let t = typ.skipTypes({tyGenericInst, tyVar, tyLent, tyAlias, tySink})
  453. result = t.assignment
  454. if result.isNil:
  455. result = liftBody(g, t, attachedAsgn, info)
  456. proc overloadedAsgn(g: ModuleGraph; dest, src: PNode): PNode =
  457. let a = getAsgnOrLiftBody(g, dest.typ, dest.info)
  458. result = newAsgnCall(g, a, dest, src)
  459. proc liftTypeBoundOps*(g: ModuleGraph; typ: PType; info: TLineInfo) =
  460. ## In the semantic pass this is called in strategic places
  461. ## to ensure we lift assignment, destructors and moves properly.
  462. ## The later 'destroyer' pass depends on it.
  463. if not hasDestructor(typ): return
  464. when false:
  465. # do not produce wrong liftings while we're still instantiating generics:
  466. # now disabled; breaks topttree.nim!
  467. if c.typesWithOps.len > 0: return
  468. let typ = typ.skipTypes({tyGenericInst, tyAlias})
  469. # we generate the destructor first so that other operators can depend on it:
  470. if typ.destructor == nil:
  471. liftBody(g, typ, attachedDestructor, info)
  472. if typ.assignment == nil:
  473. liftBody(g, typ, attachedAsgn, info)
  474. if typ.sink == nil:
  475. liftBody(g, typ, attachedSink, info)
  476. #proc patchResolvedTypeBoundOp*(g: ModuleGraph; n: PNode): PNode =
  477. # if n.kind == nkCall and