liftdestructors.nim 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. # Todo:
  12. # - use openArray instead of array to avoid over-specializations
  13. import modulegraphs, lineinfos, idents, ast, renderer, semdata,
  14. sighashes, lowerings, options, types, msgs, magicsys, tables
  15. from trees import isCaseObj
  16. type
  17. TLiftCtx = object
  18. g: ModuleGraph
  19. info: TLineInfo # for construction
  20. kind: TTypeAttachedOp
  21. fn: PSym
  22. asgnForType: PType
  23. recurse: bool
  24. filterDiscriminator: PSym # we generating destructor for case branch
  25. addMemReset: bool # add wasMoved() call after destructor call
  26. c: PContext # c can be nil, then we are called from lambdalifting!
  27. proc fillBody(c: var TLiftCtx; t: PType; body, x, y: PNode)
  28. proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
  29. info: TLineInfo): PSym
  30. proc createTypeBoundOps*(g: ModuleGraph; c: PContext; orig: PType; info: TLineInfo)
  31. proc at(a, i: PNode, elemType: PType): PNode =
  32. result = newNodeI(nkBracketExpr, a.info, 2)
  33. result[0] = a
  34. result[1] = i
  35. result.typ = elemType
  36. proc fillBodyTup(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  37. for i in 0..<t.len:
  38. let lit = lowerings.newIntLit(c.g, x.info, i)
  39. let b = if c.kind == attachedTrace: y else: y.at(lit, t[i])
  40. fillBody(c, t[i], body, x.at(lit, t[i]), b)
  41. proc dotField(x: PNode, f: PSym): PNode =
  42. result = newNodeI(nkDotExpr, x.info, 2)
  43. if x.typ.skipTypes(abstractInst).kind == tyVar:
  44. result[0] = x.newDeref
  45. else:
  46. result[0] = x
  47. result[1] = newSymNode(f, x.info)
  48. result.typ = f.typ
  49. proc newAsgnStmt(le, ri: PNode): PNode =
  50. result = newNodeI(nkAsgn, le.info, 2)
  51. result[0] = le
  52. result[1] = ri
  53. proc genBuiltin(g: ModuleGraph; magic: TMagic; name: string; i: PNode): PNode =
  54. result = newNodeI(nkCall, i.info)
  55. result.add createMagic(g, name, magic).newSymNode
  56. result.add i
  57. proc defaultOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  58. if c.kind in {attachedAsgn, attachedDeepCopy, attachedSink}:
  59. body.add newAsgnStmt(x, y)
  60. elif c.kind == attachedDestructor and c.addMemReset:
  61. let call = genBuiltin(c.g, mDefault, "default", x)
  62. call.typ = t
  63. body.add newAsgnStmt(x, call)
  64. proc genAddr(g: ModuleGraph; x: PNode): PNode =
  65. if x.kind == nkHiddenDeref:
  66. checkSonsLen(x, 1, g.config)
  67. result = x[0]
  68. else:
  69. result = newNodeIT(nkHiddenAddr, x.info, makeVarType(x.typ.owner, x.typ))
  70. result.add x
  71. proc genWhileLoop(c: var TLiftCtx; i, dest: PNode): PNode =
  72. result = newNodeI(nkWhileStmt, c.info, 2)
  73. let cmp = genBuiltin(c.g, mLtI, "<", i)
  74. cmp.add genLen(c.g, dest)
  75. cmp.typ = getSysType(c.g, c.info, tyBool)
  76. result[0] = cmp
  77. result[1] = newNodeI(nkStmtList, c.info)
  78. proc genIf(c: var TLiftCtx; cond, action: PNode): PNode =
  79. result = newTree(nkIfStmt, newTree(nkElifBranch, cond, action))
  80. proc genContainerOf(c: TLiftCtx; objType: PType, field, x: PSym): PNode =
  81. # generate: cast[ptr ObjType](cast[int](addr(x)) - offsetOf(objType.field))
  82. let intType = getSysType(c.g, unknownLineInfo, tyInt)
  83. let addrOf = newNodeIT(nkAddr, c.info, makePtrType(x.owner, x.typ))
  84. addrOf.add newDeref(newSymNode(x))
  85. let castExpr1 = newNodeIT(nkCast, c.info, intType)
  86. castExpr1.add newNodeIT(nkType, c.info, intType)
  87. castExpr1.add addrOf
  88. let dotExpr = newNodeIT(nkDotExpr, c.info, x.typ)
  89. dotExpr.add newNodeIT(nkType, c.info, objType)
  90. dotExpr.add newSymNode(field)
  91. let offsetOf = genBuiltin(c.g, mOffsetOf, "offsetof", dotExpr)
  92. offsetOf.typ = intType
  93. let minusExpr = genBuiltin(c.g, mSubI, "-", castExpr1)
  94. minusExpr.typ = intType
  95. minusExpr.add offsetOf
  96. let objPtr = makePtrType(objType.owner, objType)
  97. result = newNodeIT(nkCast, c.info, objPtr)
  98. result.add newNodeIT(nkType, c.info, objPtr)
  99. result.add minusExpr
  100. proc destructorCall(c: TLiftCtx; op: PSym; x: PNode): PNode =
  101. var destroy = newNodeIT(nkCall, x.info, op.typ[0])
  102. destroy.add(newSymNode(op))
  103. destroy.add genAddr(c.g, x)
  104. if c.addMemReset:
  105. result = newTree(nkStmtList, destroy, genBuiltin(c.g, mWasMoved, "wasMoved", x))
  106. else:
  107. result = destroy
  108. proc fillBodyObj(c: var TLiftCtx; n, body, x, y: PNode; enforceDefaultOp: bool) =
  109. case n.kind
  110. of nkSym:
  111. if c.filterDiscriminator != nil: return
  112. let f = n.sym
  113. let b = if c.kind == attachedTrace: y else: y.dotField(f)
  114. if (sfCursor in f.flags and f.typ.skipTypes(abstractInst).kind in {tyRef, tyProc} and
  115. c.g.config.selectedGC in {gcArc, gcOrc, gcHooks}) or
  116. enforceDefaultOp:
  117. defaultOp(c, f.typ, body, x.dotField(f), b)
  118. else:
  119. fillBody(c, f.typ, body, x.dotField(f), b)
  120. of nkNilLit: discard
  121. of nkRecCase:
  122. # XXX This is only correct for 'attachedSink'!
  123. var localEnforceDefaultOp = enforceDefaultOp
  124. if c.kind == attachedSink:
  125. # the value needs to be destroyed before we assign the selector
  126. # or the value is lost
  127. let prevKind = c.kind
  128. c.kind = attachedDestructor
  129. fillBodyObj(c, n, body, x, y, enforceDefaultOp = false)
  130. c.kind = prevKind
  131. localEnforceDefaultOp = true
  132. if c.kind != attachedDestructor:
  133. # copy the selector before case stmt, but destroy after case stmt
  134. fillBodyObj(c, n[0], body, x, y, enforceDefaultOp = false)
  135. let oldfilterDiscriminator = c.filterDiscriminator
  136. if c.filterDiscriminator == n[0].sym:
  137. c.filterDiscriminator = nil # we have found the case part, proceed as normal
  138. # we need to generate a case statement:
  139. var caseStmt = newNodeI(nkCaseStmt, c.info)
  140. # XXX generate 'if' that checks same branches
  141. # generate selector:
  142. var access = dotField(x, n[0].sym)
  143. caseStmt.add(access)
  144. var emptyBranches = 0
  145. # copy the branches over, but replace the fields with the for loop body:
  146. for i in 1..<n.len:
  147. var branch = copyTree(n[i])
  148. branch[^1] = newNodeI(nkStmtList, c.info)
  149. fillBodyObj(c, n[i].lastSon, branch[^1], x, y,
  150. enforceDefaultOp = localEnforceDefaultOp)
  151. if branch[^1].len == 0: inc emptyBranches
  152. caseStmt.add(branch)
  153. if emptyBranches != n.len-1:
  154. body.add(caseStmt)
  155. if c.kind == attachedDestructor:
  156. # destructor for selector is done after case stmt
  157. fillBodyObj(c, n[0], body, x, y, enforceDefaultOp = false)
  158. c.filterDiscriminator = oldfilterDiscriminator
  159. of nkRecList:
  160. for t in items(n): fillBodyObj(c, t, body, x, y, enforceDefaultOp)
  161. else:
  162. illFormedAstLocal(n, c.g.config)
  163. proc fillBodyObjTImpl(c: var TLiftCtx; t: PType, body, x, y: PNode) =
  164. if t.len > 0 and t[0] != nil:
  165. fillBody(c, skipTypes(t[0], abstractPtrs), body, x, y)
  166. fillBodyObj(c, t.n, body, x, y, enforceDefaultOp = false)
  167. proc fillBodyObjT(c: var TLiftCtx; t: PType, body, x, y: PNode) =
  168. var hasCase = isCaseObj(t.n)
  169. var obj = t
  170. while obj.len > 0 and obj[0] != nil:
  171. obj = skipTypes(obj[0], abstractPtrs)
  172. hasCase = hasCase or isCaseObj(obj.n)
  173. if hasCase and c.kind in {attachedAsgn, attachedDeepCopy}:
  174. # assignment for case objects is complex, we do:
  175. # =destroy(dest)
  176. # wasMoved(dest)
  177. # for every field:
  178. # `=` dest.field, src.field
  179. # ^ this is what we used to do, but for 'result = result.sons[0]' it
  180. # destroys 'result' too early.
  181. # So this is what we really need to do:
  182. # let blob {.cursor.} = dest # remembers the old dest.kind
  183. # wasMoved(dest)
  184. # dest.kind = src.kind
  185. # for every field (dependent on dest.kind):
  186. # `=` dest.field, src.field
  187. # =destroy(blob)
  188. var temp = newSym(skTemp, getIdent(c.g.cache, lowerings.genPrefix), c.fn, c.info)
  189. temp.typ = x.typ
  190. incl(temp.flags, sfFromGeneric)
  191. var v = newNodeI(nkVarSection, c.info)
  192. let blob = newSymNode(temp)
  193. v.addVar(blob, x)
  194. body.add v
  195. #body.add newAsgnStmt(blob, x)
  196. var wasMovedCall = newNodeI(nkCall, c.info)
  197. wasMovedCall.add(newSymNode(createMagic(c.g, "wasMoved", mWasMoved)))
  198. wasMovedCall.add x # mWasMoved does not take the address
  199. body.add wasMovedCall
  200. fillBodyObjTImpl(c, t, body, x, y)
  201. when false:
  202. # does not work yet due to phase-ordering problems:
  203. assert t.destructor != nil
  204. body.add destructorCall(c.g, t.destructor, blob)
  205. let prevKind = c.kind
  206. c.kind = attachedDestructor
  207. fillBodyObjTImpl(c, t, body, blob, y)
  208. c.kind = prevKind
  209. else:
  210. fillBodyObjTImpl(c, t, body, x, y)
  211. proc newHookCall(g: ModuleGraph; op: PSym; x, y: PNode): PNode =
  212. #if sfError in op.flags:
  213. # localError(c.config, x.info, "usage of '$1' is a user-defined error" % op.name.s)
  214. result = newNodeI(nkCall, x.info)
  215. result.add newSymNode(op)
  216. if op.typ.sons[1].kind == tyVar:
  217. result.add genAddr(g, x)
  218. else:
  219. result.add x
  220. if y != nil:
  221. result.add y
  222. proc newOpCall(op: PSym; x: PNode): PNode =
  223. result = newNodeIT(nkCall, x.info, op.typ[0])
  224. result.add(newSymNode(op))
  225. result.add x
  226. proc newDeepCopyCall(op: PSym; x, y: PNode): PNode =
  227. result = newAsgnStmt(x, newOpCall(op, y))
  228. proc usesBuiltinArc(t: PType): bool =
  229. proc wrap(t: PType): bool {.nimcall.} = ast.isGCedMem(t)
  230. result = types.searchTypeFor(t, wrap)
  231. proc useNoGc(c: TLiftCtx; t: PType): bool {.inline.} =
  232. result = optSeqDestructors in c.g.config.globalOptions and
  233. ({tfHasGCedMem, tfHasOwned} * t.flags != {} or usesBuiltinArc(t))
  234. proc requiresDestructor(c: TLiftCtx; t: PType): bool {.inline.} =
  235. result = optSeqDestructors in c.g.config.globalOptions and
  236. containsGarbageCollectedRef(t)
  237. proc instantiateGeneric(c: var TLiftCtx; op: PSym; t, typeInst: PType): PSym =
  238. if c.c != nil and typeInst != nil:
  239. result = c.c.instTypeBoundOp(c.c, op, typeInst, c.info, attachedAsgn, 1)
  240. else:
  241. localError(c.g.config, c.info,
  242. "cannot generate destructor for generic type: " & typeToString(t))
  243. result = nil
  244. proc considerAsgnOrSink(c: var TLiftCtx; t: PType; body, x, y: PNode;
  245. field: var PSym): bool =
  246. if optSeqDestructors in c.g.config.globalOptions:
  247. let op = field
  248. if field != nil and sfOverriden in field.flags:
  249. if sfError in op.flags:
  250. incl c.fn.flags, sfError
  251. #else:
  252. # markUsed(c.g.config, c.info, op, c.g.usageSym)
  253. onUse(c.info, op)
  254. body.add newHookCall(c.g, op, x, y)
  255. result = true
  256. elif tfHasAsgn in t.flags:
  257. var op: PSym
  258. if sameType(t, c.asgnForType):
  259. # generate recursive call:
  260. if c.recurse:
  261. op = c.fn
  262. else:
  263. c.recurse = true
  264. return false
  265. else:
  266. op = field
  267. if op == nil:
  268. op = produceSym(c.g, c.c, t, c.kind, c.info)
  269. if sfError in op.flags:
  270. incl c.fn.flags, sfError
  271. #else:
  272. # markUsed(c.g.config, c.info, op, c.g.usageSym)
  273. onUse(c.info, op)
  274. # We also now do generic instantiations in the destructor lifting pass:
  275. if op.ast[genericParamsPos].kind != nkEmpty:
  276. op = instantiateGeneric(c, op, t, t.typeInst)
  277. field = op
  278. #echo "trying to use ", op.ast
  279. #echo "for ", op.name.s, " "
  280. #debug(t)
  281. #return false
  282. assert op.ast[genericParamsPos].kind == nkEmpty
  283. body.add newHookCall(c.g, op, x, y)
  284. result = true
  285. proc addDestructorCall(c: var TLiftCtx; orig: PType; body, x: PNode) =
  286. let t = orig.skipTypes(abstractInst)
  287. var op = t.destructor
  288. if op != nil and sfOverriden in op.flags:
  289. if op.ast[genericParamsPos].kind != nkEmpty:
  290. # patch generic destructor:
  291. op = instantiateGeneric(c, op, t, t.typeInst)
  292. t.attachedOps[attachedDestructor] = op
  293. if op == nil and (useNoGc(c, t) or requiresDestructor(c, t)):
  294. op = produceSym(c.g, c.c, t, attachedDestructor, c.info)
  295. doAssert op != nil
  296. doAssert op == t.destructor
  297. if op != nil:
  298. #markUsed(c.g.config, c.info, op, c.g.usageSym)
  299. onUse(c.info, op)
  300. body.add destructorCall(c, op, x)
  301. elif useNoGc(c, t):
  302. internalError(c.g.config, c.info,
  303. "type-bound operator could not be resolved")
  304. proc considerUserDefinedOp(c: var TLiftCtx; t: PType; body, x, y: PNode): bool =
  305. case c.kind
  306. of attachedDestructor:
  307. var op = t.destructor
  308. if op != nil and sfOverriden in op.flags:
  309. if op.ast[genericParamsPos].kind != nkEmpty:
  310. # patch generic destructor:
  311. op = instantiateGeneric(c, op, t, t.typeInst)
  312. t.attachedOps[attachedDestructor] = op
  313. #markUsed(c.g.config, c.info, op, c.g.usageSym)
  314. onUse(c.info, op)
  315. body.add destructorCall(c, op, x)
  316. result = true
  317. #result = addDestructorCall(c, t, body, x)
  318. of attachedAsgn, attachedSink, attachedTrace:
  319. result = considerAsgnOrSink(c, t, body, x, y, t.attachedOps[c.kind])
  320. of attachedDispose:
  321. result = considerAsgnOrSink(c, t, body, x, nil, t.attachedOps[c.kind])
  322. of attachedDeepCopy:
  323. let op = t.attachedOps[attachedDeepCopy]
  324. if op != nil:
  325. #markUsed(c.g.config, c.info, op, c.g.usageSym)
  326. onUse(c.info, op)
  327. body.add newDeepCopyCall(op, x, y)
  328. result = true
  329. proc declareCounter(c: var TLiftCtx; body: PNode; first: BiggestInt): PNode =
  330. var temp = newSym(skTemp, getIdent(c.g.cache, lowerings.genPrefix), c.fn, c.info)
  331. temp.typ = getSysType(c.g, body.info, tyInt)
  332. incl(temp.flags, sfFromGeneric)
  333. var v = newNodeI(nkVarSection, c.info)
  334. result = newSymNode(temp)
  335. v.addVar(result, lowerings.newIntLit(c.g, body.info, first))
  336. body.add v
  337. proc addIncStmt(c: var TLiftCtx; body, i: PNode) =
  338. let incCall = genBuiltin(c.g, mInc, "inc", i)
  339. incCall.add lowerings.newIntLit(c.g, c.info, 1)
  340. body.add incCall
  341. proc newSeqCall(g: ModuleGraph; x, y: PNode): PNode =
  342. # don't call genAddr(c, x) here:
  343. result = genBuiltin(g, mNewSeq, "newSeq", x)
  344. let lenCall = genBuiltin(g, mLengthSeq, "len", y)
  345. lenCall.typ = getSysType(g, x.info, tyInt)
  346. result.add lenCall
  347. proc setLenStrCall(g: ModuleGraph; x, y: PNode): PNode =
  348. let lenCall = genBuiltin(g, mLengthStr, "len", y)
  349. lenCall.typ = getSysType(g, x.info, tyInt)
  350. result = genBuiltin(g, mSetLengthStr, "setLen", x) # genAddr(g, x))
  351. result.add lenCall
  352. proc setLenSeqCall(c: var TLiftCtx; t: PType; x, y: PNode): PNode =
  353. let lenCall = genBuiltin(c.g, mLengthSeq, "len", y)
  354. lenCall.typ = getSysType(c.g, x.info, tyInt)
  355. var op = getSysMagic(c.g, x.info, "setLen", mSetLengthSeq)
  356. op = instantiateGeneric(c, op, t, t)
  357. result = newTree(nkCall, newSymNode(op, x.info), x, lenCall)
  358. proc forallElements(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  359. let i = declareCounter(c, body, toInt64(firstOrd(c.g.config, t)))
  360. let whileLoop = genWhileLoop(c, i, x)
  361. let elemType = t.lastSon
  362. let b = if c.kind == attachedTrace: y else: y.at(i, elemType)
  363. fillBody(c, elemType, whileLoop[1], x.at(i, elemType), b)
  364. addIncStmt(c, whileLoop[1], i)
  365. body.add whileLoop
  366. proc fillSeqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  367. case c.kind
  368. of attachedAsgn, attachedDeepCopy:
  369. # we generate:
  370. # setLen(dest, y.len)
  371. # var i = 0
  372. # while i < y.len: dest[i] = y[i]; inc(i)
  373. # This is usually more efficient than a destroy/create pair.
  374. body.add setLenSeqCall(c, t, x, y)
  375. forallElements(c, t, body, x, y)
  376. of attachedSink:
  377. let moveCall = genBuiltin(c.g, mMove, "move", x)
  378. moveCall.add y
  379. doAssert t.destructor != nil
  380. moveCall.add destructorCall(c, t.destructor, x)
  381. body.add moveCall
  382. of attachedDestructor:
  383. # destroy all elements:
  384. forallElements(c, t, body, x, y)
  385. body.add genBuiltin(c.g, mDestroy, "destroy", x)
  386. of attachedTrace:
  387. # follow all elements:
  388. forallElements(c, t, body, x, y)
  389. of attachedDispose:
  390. body.add genBuiltin(c.g, mDestroy, "destroy", x)
  391. proc useSeqOrStrOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  392. createTypeBoundOps(c.g, c.c, t, body.info)
  393. # recursions are tricky, so we might need to forward the generated
  394. # operation here:
  395. var t = t
  396. if t.assignment == nil or t.destructor == nil:
  397. let h = sighashes.hashType(t, {CoType, CoConsiderOwned, CoDistinct})
  398. let canon = c.g.canonTypes.getOrDefault(h)
  399. if canon != nil: t = canon
  400. case c.kind
  401. of attachedAsgn, attachedDeepCopy:
  402. if t.assignment == nil:
  403. return # protect from recursion
  404. body.add newHookCall(c.g, t.assignment, x, y)
  405. of attachedSink:
  406. # we always inline the move for better performance:
  407. let moveCall = genBuiltin(c.g, mMove, "move", x)
  408. moveCall.add y
  409. doAssert t.destructor != nil
  410. moveCall.add destructorCall(c, t.destructor, x)
  411. body.add moveCall
  412. # alternatively we could do this:
  413. when false:
  414. doAssert t.asink != nil
  415. body.add newHookCall(c.g, t.asink, x, y)
  416. of attachedDestructor:
  417. doAssert t.destructor != nil
  418. body.add destructorCall(c, t.destructor, x)
  419. of attachedTrace:
  420. if t.attachedOps[c.kind] == nil:
  421. return # protect from recursion
  422. body.add newHookCall(c.g, t.attachedOps[c.kind], x, y)
  423. of attachedDispose:
  424. if t.attachedOps[c.kind] == nil:
  425. return # protect from recursion
  426. body.add newHookCall(c.g, t.attachedOps[c.kind], x, nil)
  427. proc fillStrOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  428. case c.kind
  429. of attachedAsgn, attachedDeepCopy:
  430. body.add callCodegenProc(c.g, "nimAsgnStrV2", c.info, genAddr(c.g, x), y)
  431. of attachedSink:
  432. let moveCall = genBuiltin(c.g, mMove, "move", x)
  433. moveCall.add y
  434. doAssert t.destructor != nil
  435. moveCall.add destructorCall(c, t.destructor, x)
  436. body.add moveCall
  437. of attachedDestructor, attachedDispose:
  438. body.add genBuiltin(c.g, mDestroy, "destroy", x)
  439. of attachedTrace:
  440. discard "strings are atomic and have no inner elements that are to trace"
  441. proc atomicRefOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  442. var actions = newNodeI(nkStmtList, c.info)
  443. let elemType = t.lastSon
  444. if isFinal(elemType):
  445. addDestructorCall(c, elemType, actions, genDeref(x, nkDerefExpr))
  446. actions.add callCodegenProc(c.g, "nimRawDispose", c.info, x)
  447. else:
  448. addDestructorCall(c, elemType, newNodeI(nkStmtList, c.info), genDeref(x, nkDerefExpr))
  449. actions.add callCodegenProc(c.g, "nimDestroyAndDispose", c.info, x)
  450. let isCyclic = c.g.config.selectedGC == gcOrc and types.canFormAcycle(t)
  451. var cond: PNode
  452. if isCyclic:
  453. if isFinal(elemType):
  454. let typInfo = genBuiltin(c.g, mGetTypeInfo, "getTypeInfo", newNodeIT(nkType, x.info, elemType))
  455. typInfo.typ = getSysType(c.g, c.info, tyPointer)
  456. cond = callCodegenProc(c.g, "nimDecRefIsLastCyclicStatic", c.info, x, typInfo)
  457. else:
  458. cond = callCodegenProc(c.g, "nimDecRefIsLastCyclicDyn", c.info, x)
  459. else:
  460. cond = callCodegenProc(c.g, "nimDecRefIsLast", c.info, x)
  461. cond.typ = getSysType(c.g, x.info, tyBool)
  462. case c.kind
  463. of attachedSink:
  464. body.add genIf(c, cond, actions)
  465. body.add newAsgnStmt(x, y)
  466. of attachedAsgn:
  467. body.add genIf(c, y, callCodegenProc(c.g,
  468. if isCyclic: "nimIncRefCyclic" else: "nimIncRef", c.info, y))
  469. body.add genIf(c, cond, actions)
  470. body.add newAsgnStmt(x, y)
  471. of attachedDestructor:
  472. body.add genIf(c, cond, actions)
  473. body.add newAsgnStmt(x, newNodeIT(nkNilLit, body.info, t))
  474. of attachedDeepCopy: assert(false, "cannot happen")
  475. of attachedTrace:
  476. if isFinal(elemType):
  477. let typInfo = genBuiltin(c.g, mGetTypeInfo, "getTypeInfo", newNodeIT(nkType, x.info, elemType))
  478. typInfo.typ = getSysType(c.g, c.info, tyPointer)
  479. body.add callCodegenProc(c.g, "nimTraceRef", c.info, genAddrOf(x), typInfo, y)
  480. else:
  481. # If the ref is polymorphic we have to account for this
  482. body.add callCodegenProc(c.g, "nimTraceRefDyn", c.info, genAddrOf(x), y)
  483. of attachedDispose:
  484. # this is crucial! dispose is like =destroy but we don't follow refs
  485. # as that is dealt within the cycle collector.
  486. when false:
  487. let cond = copyTree(x)
  488. cond.typ = getSysType(c.g, x.info, tyBool)
  489. actions.add callCodegenProc(c.g, "nimRawDispose", c.info, x)
  490. body.add genIf(c, cond, actions)
  491. proc atomicClosureOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  492. ## Closures are really like refs except they always use a virtual destructor
  493. ## and we need to do the refcounting only on the ref field which we call 'xenv':
  494. let xenv = genBuiltin(c.g, mAccessEnv, "accessEnv", x)
  495. xenv.typ = getSysType(c.g, c.info, tyPointer)
  496. var actions = newNodeI(nkStmtList, c.info)
  497. actions.add callCodegenProc(c.g, "nimDestroyAndDispose", c.info, xenv)
  498. let decRefProc =
  499. if c.g.config.selectedGC == gcOrc: "nimDecRefIsLastCyclicDyn"
  500. else: "nimDecRefIsLast"
  501. let cond = callCodegenProc(c.g, decRefProc, c.info, xenv)
  502. cond.typ = getSysType(c.g, x.info, tyBool)
  503. case c.kind
  504. of attachedSink:
  505. body.add genIf(c, cond, actions)
  506. body.add newAsgnStmt(x, y)
  507. of attachedAsgn:
  508. let yenv = genBuiltin(c.g, mAccessEnv, "accessEnv", y)
  509. yenv.typ = getSysType(c.g, c.info, tyPointer)
  510. let incRefProc =
  511. if c.g.config.selectedGC == gcOrc: "nimIncRefCyclic"
  512. else: "nimIncRef"
  513. body.add genIf(c, yenv, callCodegenProc(c.g, incRefProc, c.info, yenv))
  514. body.add genIf(c, cond, actions)
  515. body.add newAsgnStmt(x, y)
  516. of attachedDestructor:
  517. body.add genIf(c, cond, actions)
  518. body.add newAsgnStmt(xenv, newNodeIT(nkNilLit, body.info, xenv.typ))
  519. of attachedDeepCopy: assert(false, "cannot happen")
  520. of attachedTrace:
  521. body.add callCodegenProc(c.g, "nimTraceRefDyn", c.info, genAddrOf(xenv), y)
  522. of attachedDispose:
  523. # this is crucial! dispose is like =destroy but we don't follow refs
  524. # as that is dealt within the cycle collector.
  525. when false:
  526. let cond = copyTree(xenv)
  527. cond.typ = getSysType(c.g, xenv.info, tyBool)
  528. actions.add callCodegenProc(c.g, "nimRawDispose", c.info, xenv)
  529. body.add genIf(c, cond, actions)
  530. proc weakrefOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  531. case c.kind
  532. of attachedSink:
  533. # we 'nil' y out afterwards so we *need* to take over its reference
  534. # count value:
  535. body.add genIf(c, x, callCodegenProc(c.g, "nimDecWeakRef", c.info, x))
  536. body.add newAsgnStmt(x, y)
  537. of attachedAsgn:
  538. body.add genIf(c, y, callCodegenProc(c.g, "nimIncRef", c.info, y))
  539. body.add genIf(c, x, callCodegenProc(c.g, "nimDecWeakRef", c.info, x))
  540. body.add newAsgnStmt(x, y)
  541. of attachedDestructor:
  542. # it's better to prepend the destruction of weak refs in order to
  543. # prevent wrong "dangling refs exist" problems:
  544. var actions = newNodeI(nkStmtList, c.info)
  545. actions.add callCodegenProc(c.g, "nimDecWeakRef", c.info, x)
  546. actions.add newAsgnStmt(x, newNodeIT(nkNilLit, body.info, t))
  547. let des = genIf(c, x, actions)
  548. if body.len == 0:
  549. body.add des
  550. else:
  551. body.sons.insert(des, 0)
  552. of attachedDeepCopy: assert(false, "cannot happen")
  553. of attachedTrace, attachedDispose: discard
  554. proc ownedRefOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  555. var actions = newNodeI(nkStmtList, c.info)
  556. let elemType = t.lastSon
  557. #fillBody(c, elemType, actions, genDeref(x), genDeref(y))
  558. #var disposeCall = genBuiltin(c.g, mDispose, "dispose", x)
  559. if isFinal(elemType):
  560. addDestructorCall(c, elemType, actions, genDeref(x, nkDerefExpr))
  561. actions.add callCodegenProc(c.g, "nimRawDispose", c.info, x)
  562. else:
  563. addDestructorCall(c, elemType, newNodeI(nkStmtList, c.info), genDeref(x, nkDerefExpr))
  564. actions.add callCodegenProc(c.g, "nimDestroyAndDispose", c.info, x)
  565. case c.kind
  566. of attachedSink, attachedAsgn:
  567. body.add genIf(c, x, actions)
  568. body.add newAsgnStmt(x, y)
  569. of attachedDestructor:
  570. body.add genIf(c, x, actions)
  571. body.add newAsgnStmt(x, newNodeIT(nkNilLit, body.info, t))
  572. of attachedDeepCopy: assert(false, "cannot happen")
  573. of attachedTrace, attachedDispose: discard
  574. proc closureOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  575. if c.kind == attachedDeepCopy:
  576. # a big problem is that we don't know the environment's type here, so we
  577. # have to go through some indirection; we delegate this to the codegen:
  578. let call = newNodeI(nkCall, c.info, 2)
  579. call.typ = t
  580. call[0] = newSymNode(createMagic(c.g, "deepCopy", mDeepCopy))
  581. call[1] = y
  582. body.add newAsgnStmt(x, call)
  583. elif (optOwnedRefs in c.g.config.globalOptions and
  584. optRefCheck in c.g.config.options) or c.g.config.selectedGC in {gcArc, gcOrc}:
  585. let xx = genBuiltin(c.g, mAccessEnv, "accessEnv", x)
  586. xx.typ = getSysType(c.g, c.info, tyPointer)
  587. case c.kind
  588. of attachedSink:
  589. # we 'nil' y out afterwards so we *need* to take over its reference
  590. # count value:
  591. body.add genIf(c, xx, callCodegenProc(c.g, "nimDecWeakRef", c.info, xx))
  592. body.add newAsgnStmt(x, y)
  593. of attachedAsgn:
  594. let yy = genBuiltin(c.g, mAccessEnv, "accessEnv", y)
  595. yy.typ = getSysType(c.g, c.info, tyPointer)
  596. body.add genIf(c, yy, callCodegenProc(c.g, "nimIncRef", c.info, yy))
  597. body.add genIf(c, xx, callCodegenProc(c.g, "nimDecWeakRef", c.info, xx))
  598. body.add newAsgnStmt(x, y)
  599. of attachedDestructor:
  600. var actions = newNodeI(nkStmtList, c.info)
  601. actions.add callCodegenProc(c.g, "nimDecWeakRef", c.info, xx)
  602. actions.add newAsgnStmt(xx, newNodeIT(nkNilLit, body.info, xx.typ))
  603. let des = genIf(c, xx, actions)
  604. if body.len == 0:
  605. body.add des
  606. else:
  607. body.sons.insert(des, 0)
  608. of attachedDeepCopy: assert(false, "cannot happen")
  609. of attachedTrace, attachedDispose: discard
  610. proc ownedClosureOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  611. let xx = genBuiltin(c.g, mAccessEnv, "accessEnv", x)
  612. xx.typ = getSysType(c.g, c.info, tyPointer)
  613. var actions = newNodeI(nkStmtList, c.info)
  614. #discard addDestructorCall(c, elemType, newNodeI(nkStmtList, c.info), genDeref(xx))
  615. actions.add callCodegenProc(c.g, "nimDestroyAndDispose", c.info, xx)
  616. case c.kind
  617. of attachedSink, attachedAsgn:
  618. body.add genIf(c, xx, actions)
  619. body.add newAsgnStmt(x, y)
  620. of attachedDestructor:
  621. body.add genIf(c, xx, actions)
  622. body.add newAsgnStmt(xx, newNodeIT(nkNilLit, body.info, xx.typ))
  623. of attachedDeepCopy: assert(false, "cannot happen")
  624. of attachedTrace, attachedDispose: discard
  625. proc fillBody(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  626. case t.kind
  627. of tyNone, tyEmpty, tyVoid: discard
  628. of tyPointer, tySet, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCString,
  629. tyPtr, tyUncheckedArray, tyVar, tyLent:
  630. defaultOp(c, t, body, x, y)
  631. of tyRef:
  632. if c.g.config.selectedGC in {gcArc, gcOrc}:
  633. atomicRefOp(c, t, body, x, y)
  634. elif (optOwnedRefs in c.g.config.globalOptions and
  635. optRefCheck in c.g.config.options):
  636. weakrefOp(c, t, body, x, y)
  637. else:
  638. defaultOp(c, t, body, x, y)
  639. of tyProc:
  640. if t.callConv == ccClosure:
  641. if c.g.config.selectedGC in {gcArc, gcOrc}:
  642. atomicClosureOp(c, t, body, x, y)
  643. else:
  644. closureOp(c, t, body, x, y)
  645. else:
  646. defaultOp(c, t, body, x, y)
  647. of tyOwned:
  648. let base = t.skipTypes(abstractInstOwned)
  649. if optOwnedRefs in c.g.config.globalOptions:
  650. case base.kind
  651. of tyRef:
  652. ownedRefOp(c, base, body, x, y)
  653. return
  654. of tyProc:
  655. if base.callConv == ccClosure:
  656. ownedClosureOp(c, base, body, x, y)
  657. return
  658. else: discard
  659. defaultOp(c, base, body, x, y)
  660. of tyArray:
  661. if tfHasAsgn in t.flags or useNoGc(c, t):
  662. forallElements(c, t, body, x, y)
  663. else:
  664. defaultOp(c, t, body, x, y)
  665. of tySequence:
  666. if useNoGc(c, t):
  667. useSeqOrStrOp(c, t, body, x, y)
  668. elif optSeqDestructors in c.g.config.globalOptions:
  669. # note that tfHasAsgn is propagated so we need the check on
  670. # 'selectedGC' here to determine if we have the new runtime.
  671. discard considerUserDefinedOp(c, t, body, x, y)
  672. elif tfHasAsgn in t.flags:
  673. if c.kind in {attachedAsgn, attachedSink, attachedDeepCopy}:
  674. body.add newSeqCall(c.g, x, y)
  675. forallElements(c, t, body, x, y)
  676. else:
  677. defaultOp(c, t, body, x, y)
  678. of tyString:
  679. if useNoGc(c, t):
  680. useSeqOrStrOp(c, t, body, x, y)
  681. elif tfHasAsgn in t.flags:
  682. discard considerUserDefinedOp(c, t, body, x, y)
  683. else:
  684. defaultOp(c, t, body, x, y)
  685. of tyObject:
  686. if not considerUserDefinedOp(c, t, body, x, y):
  687. if c.kind in {attachedAsgn, attachedSink} and t.sym != nil and sfImportc in t.sym.flags:
  688. body.add newAsgnStmt(x, y)
  689. else:
  690. fillBodyObjT(c, t, body, x, y)
  691. of tyDistinct:
  692. if not considerUserDefinedOp(c, t, body, x, y):
  693. fillBody(c, t[0], body, x, y)
  694. of tyTuple:
  695. fillBodyTup(c, t, body, x, y)
  696. of tyVarargs, tyOpenArray:
  697. if c.kind == attachedDestructor and (tfHasAsgn in t.flags or useNoGc(c, t)):
  698. forallElements(c, t, body, x, y)
  699. else:
  700. discard "cannot copy openArray"
  701. of tyFromExpr, tyProxy, tyBuiltInTypeClass, tyUserTypeClass,
  702. tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot, tyAnything,
  703. tyGenericParam, tyGenericBody, tyNil, tyUntyped, tyTyped,
  704. tyTypeDesc, tyGenericInvocation, tyForward, tyStatic:
  705. #internalError(c.g.config, c.info, "assignment requested for type: " & typeToString(t))
  706. discard
  707. of tyOrdinal, tyRange, tyInferred,
  708. tyGenericInst, tyAlias, tySink:
  709. fillBody(c, lastSon(t), body, x, y)
  710. of tyOptDeprecated: doAssert false
  711. proc produceSymDistinctType(g: ModuleGraph; c: PContext; typ: PType;
  712. kind: TTypeAttachedOp; info: TLineInfo): PSym =
  713. assert typ.kind == tyDistinct
  714. let baseType = typ[0]
  715. if baseType.attachedOps[kind] == nil:
  716. discard produceSym(g, c, baseType, kind, info)
  717. typ.attachedOps[kind] = baseType.attachedOps[kind]
  718. result = typ.attachedOps[kind]
  719. proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp;
  720. info: TLineInfo): PSym =
  721. let procname = getIdent(g.cache, AttachedOpToStr[kind])
  722. result = newSym(skProc, procname, owner, info)
  723. let dest = newSym(skParam, getIdent(g.cache, "dest"), result, info)
  724. let src = newSym(skParam, getIdent(g.cache, if kind == attachedTrace: "env" else: "src"), result, info)
  725. dest.typ = makeVarType(typ.owner, typ)
  726. if kind == attachedTrace:
  727. src.typ = getSysType(g, info, tyPointer)
  728. else:
  729. src.typ = typ
  730. result.typ = newProcType(info, owner)
  731. result.typ.addParam dest
  732. if kind notin {attachedDestructor, attachedDispose}:
  733. result.typ.addParam src
  734. var n = newNodeI(nkProcDef, info, bodyPos+1)
  735. for i in 0..<n.len: n[i] = newNodeI(nkEmpty, info)
  736. n[namePos] = newSymNode(result)
  737. n[paramsPos] = result.typ.n
  738. n[bodyPos] = newNodeI(nkStmtList, info)
  739. result.ast = n
  740. incl result.flags, sfFromGeneric
  741. incl result.flags, sfGeneratedOp
  742. proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
  743. info: TLineInfo): PSym =
  744. if typ.kind == tyDistinct:
  745. return produceSymDistinctType(g, c, typ, kind, info)
  746. result = symPrototype(g, typ, typ.owner, kind, info)
  747. var a = TLiftCtx(info: info, g: g, kind: kind, c: c, asgnForType:typ)
  748. a.fn = result
  749. let dest = result.typ.n[1].sym
  750. let d = newDeref(newSymNode(dest))
  751. let src = if kind in {attachedDestructor, attachedDispose}: newNodeIT(nkSym, info, getSysType(g, info, tyPointer))
  752. else: newSymNode(result.typ.n[2].sym)
  753. # register this operation already:
  754. typ.attachedOps[kind] = result
  755. if kind == attachedSink and typ.attachedOps[attachedDestructor] != nil and
  756. sfOverriden in typ.attachedOps[attachedDestructor].flags:
  757. ## compiler can use a combination of `=destroy` and memCopy for sink op
  758. dest.flags.incl sfCursor
  759. result.ast[bodyPos].add newOpCall(typ.attachedOps[attachedDestructor], d[0])
  760. result.ast[bodyPos].add newAsgnStmt(d, src)
  761. else:
  762. var tk: TTypeKind
  763. if g.config.selectedGC in {gcArc, gcOrc, gcHooks}:
  764. tk = skipTypes(typ, {tyOrdinal, tyRange, tyInferred, tyGenericInst, tyStatic, tyAlias, tySink}).kind
  765. else:
  766. tk = tyNone # no special casing for strings and seqs
  767. case tk
  768. of tySequence:
  769. fillSeqOp(a, typ, result.ast[bodyPos], d, src)
  770. of tyString:
  771. fillStrOp(a, typ, result.ast[bodyPos], d, src)
  772. else:
  773. fillBody(a, typ, result.ast[bodyPos], d, src)
  774. proc produceDestructorForDiscriminator*(g: ModuleGraph; typ: PType; field: PSym, info: TLineInfo): PSym =
  775. assert(typ.skipTypes({tyAlias, tyGenericInst}).kind == tyObject)
  776. result = symPrototype(g, field.typ, typ.owner, attachedDestructor, info)
  777. var a = TLiftCtx(info: info, g: g, kind: attachedDestructor, asgnForType: typ)
  778. a.fn = result
  779. a.asgnForType = typ
  780. a.filterDiscriminator = field
  781. a.addMemReset = true
  782. let discrimantDest = result.typ.n[1].sym
  783. let dst = newSym(skVar, getIdent(g.cache, "dest"), result, info)
  784. dst.typ = makePtrType(typ.owner, typ)
  785. let dstSym = newSymNode(dst)
  786. let d = newDeref(dstSym)
  787. let v = newNodeI(nkVarSection, info)
  788. v.addVar(dstSym, genContainerOf(a, typ, field, discrimantDest))
  789. result.ast[bodyPos].add v
  790. let placeHolder = newNodeIT(nkSym, info, getSysType(g, info, tyPointer))
  791. fillBody(a, typ, result.ast[bodyPos], d, placeHolder)
  792. template liftTypeBoundOps*(c: PContext; typ: PType; info: TLineInfo) =
  793. discard "now a nop"
  794. proc patchBody(g: ModuleGraph; c: PContext; n: PNode; info: TLineInfo) =
  795. if n.kind in nkCallKinds:
  796. if n[0].kind == nkSym and n[0].sym.magic == mDestroy:
  797. let t = n[1].typ.skipTypes(abstractVar)
  798. if t.destructor == nil:
  799. discard produceSym(g, c, t, attachedDestructor, info)
  800. if t.destructor != nil:
  801. if t.destructor.ast[genericParamsPos].kind != nkEmpty:
  802. internalError(g.config, info, "resolved destructor is generic")
  803. if t.destructor.magic == mDestroy:
  804. internalError(g.config, info, "patching mDestroy with mDestroy?")
  805. n[0] = newSymNode(t.destructor)
  806. for x in n: patchBody(g, c, x, info)
  807. template inst(field, t) =
  808. if field.ast != nil and field.ast[genericParamsPos].kind != nkEmpty:
  809. if t.typeInst != nil:
  810. var a: TLiftCtx
  811. a.info = info
  812. a.g = g
  813. a.kind = k
  814. a.c = c
  815. field = instantiateGeneric(a, field, t, t.typeInst)
  816. if field.ast != nil:
  817. patchBody(g, c, field.ast, info)
  818. else:
  819. localError(g.config, info, "unresolved generic parameter")
  820. proc isTrival(s: PSym): bool {.inline.} =
  821. s == nil or (s.ast != nil and s.ast[bodyPos].len == 0)
  822. proc isEmptyContainer(g: ModuleGraph, t: PType): bool =
  823. (t.kind == tyArray and lengthOrd(g.config, t[0]) == 0) or
  824. (t.kind == tySequence and t[0].kind == tyError)
  825. proc createTypeBoundOps(g: ModuleGraph; c: PContext; orig: PType; info: TLineInfo) =
  826. ## In the semantic pass this is called in strategic places
  827. ## to ensure we lift assignment, destructors and moves properly.
  828. ## The later 'injectdestructors' pass depends on it.
  829. if orig == nil or {tfCheckedForDestructor, tfHasMeta} * orig.flags != {}: return
  830. incl orig.flags, tfCheckedForDestructor
  831. let skipped = orig.skipTypes({tyGenericInst, tyAlias, tySink})
  832. if isEmptyContainer(skipped) or skipped.kind == tyStatic: return
  833. let h = sighashes.hashType(skipped, {CoType, CoConsiderOwned, CoDistinct})
  834. var canon = g.canonTypes.getOrDefault(h)
  835. if canon == nil:
  836. g.canonTypes[h] = skipped
  837. canon = skipped
  838. # multiple cases are to distinguish here:
  839. # 1. we don't know yet if 'typ' has a nontrival destructor.
  840. # 2. we have a nop destructor. --> mDestroy
  841. # 3. we have a lifted destructor.
  842. # 4. We have a custom destructor.
  843. # 5. We have a (custom) generic destructor.
  844. # we do not generate '=trace' nor '=dispose' procs if we
  845. # have the cycle detection disabled, saves code size.
  846. let lastAttached = if g.config.selectedGC == gcOrc: attachedDispose
  847. else: attachedSink
  848. # we generate the destructor first so that other operators can depend on it:
  849. for k in attachedDestructor..lastAttached:
  850. if canon.attachedOps[k] == nil:
  851. discard produceSym(g, c, canon, k, info)
  852. else:
  853. inst(canon.attachedOps[k], canon)
  854. if canon != orig:
  855. orig.attachedOps[k] = canon.attachedOps[k]
  856. if not isTrival(orig.destructor):
  857. #or not isTrival(orig.assignment) or
  858. # not isTrival(orig.sink):
  859. orig.flags.incl tfHasAsgn