liftdestructors.nim 36 KB

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