injectdestructors.nim: code cleanups

This commit is contained in:
Araq 2019-04-16 10:34:47 +02:00
commit a520eb7766

View file

@ -202,16 +202,19 @@ proc isLastRead(n: PNode; c: var Con): bool =
instr = i instr = i
break break
dbg:
echo "starting point for ", n, " is ", instr
if instr < 0: return false if instr < 0: return false
# we go through all paths beginning from 'instr+1' and need to # we go through all paths beginning from 'instr+1' and need to
# ensure that we don't find another 'use X' instruction. # ensure that we don't find another 'use X' instruction.
if instr+1 >= c.g.len: return true if instr+1 >= c.g.len: return true
dbg:
echo "beginning here ", instr
when true: result = isLastRead(n, c, instr+1, -1) >= 0
result = isLastRead(n, c, instr+1, -1) >= 0 dbg:
else: echo "ugh ", c.otherRead.isNil, " ", result
when false:
let s = n.sym let s = n.sym
var pcs: seq[int] = @[instr+1] var pcs: seq[int] = @[instr+1]
var takenGotos: IntSet var takenGotos: IntSet
@ -292,30 +295,6 @@ template isUnpackedTuple(s: PSym): bool =
## hence unpacked tuples themselves don't need to be destroyed ## hence unpacked tuples themselves don't need to be destroyed
s.kind == skTemp and s.typ.kind == tyTuple s.kind == skTemp and s.typ.kind == tyTuple
proc patchHead(n: PNode) =
if n.kind in nkCallKinds and n[0].kind == nkSym and n.len > 1:
let s = n[0].sym
if s.name.s[0] == '=' and s.name.s in ["=sink", "=", "=destroy"]:
if sfFromGeneric in s.flags:
excl(s.flags, sfFromGeneric)
patchHead(s.getBody)
let t = n[1].typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink, tyInferred})
template patch(op, field) =
if s.name.s == op and field != nil and field != s:
n.sons[0].sym = field
patch "=sink", t.sink
patch "=", t.assignment
patch "=destroy", t.destructor
for x in n:
patchHead(x)
proc patchHead(s: PSym) =
if sfFromGeneric in s.flags:
# do not patch the builtin type bound operators for seqs:
let dest = s.typ.sons[1].skipTypes(abstractVar)
if dest.kind != tySequence:
patchHead(s.ast[bodyPos])
proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string) = proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string) =
var m = "'" & opname & "' is not available for type <" & typeToString(t) & ">" var m = "'" & opname & "' is not available for type <" & typeToString(t) & ">"
if opname == "=" and ri != nil: if opname == "=" and ri != nil:
@ -343,19 +322,12 @@ template genOp(opr, opname, ri) =
elif op.ast[genericParamsPos].kind != nkEmpty: elif op.ast[genericParamsPos].kind != nkEmpty:
globalError(c.graph.config, dest.info, "internal error: '" & opname & globalError(c.graph.config, dest.info, "internal error: '" & opname &
"' operator is generic") "' operator is generic")
#patchHead op
if sfError in op.flags: checkForErrorPragma(c, t, ri, opname) if sfError in op.flags: checkForErrorPragma(c, t, ri, opname)
let addrExp = newNodeIT(nkHiddenAddr, dest.info, makePtrType(c, dest.typ)) let addrExp = newNodeIT(nkHiddenAddr, dest.info, makePtrType(c, dest.typ))
addrExp.add(dest) addrExp.add(dest)
result = newTree(nkCall, newSymNode(op), addrExp) result = newTree(nkCall, newSymNode(op), addrExp)
proc genSink(c: Con; t: PType; dest, ri: PNode): PNode = proc genSink(c: Con; t: PType; dest, ri: PNode): PNode =
when false:
if t.kind != tyString:
echo "this one ", c.graph.config$dest.info, " for ", typeToString(t, preferDesc)
debug t.sink.typ.sons[2]
echo t.sink.id, " owner ", t.id
quit 1
let t = t.skipTypes({tyGenericInst, tyAlias, tySink}) let t = t.skipTypes({tyGenericInst, tyAlias, tySink})
let op = if t.sink != nil: t.sink else: t.assignment let op = if t.sink != nil: t.sink else: t.assignment
if op != nil: if op != nil:
@ -786,18 +758,6 @@ proc p(n: PNode; c: var Con): PNode =
else: else:
result = copyNode(n) result = copyNode(n)
recurse(n, result) recurse(n, result)
when false:
if optNimV2 in c.graph.config.globalOptions:
# raise e does consume the 'e':
let r = result
if isAtom(r[0]):
result = newTree(nkStmtList, r, genWasMoved(r[0], c))
elif r[0].kind == nkStmtListExpr:
# terrible hack ahead...
result = newTree(nkStmtList, r, genWasMoved(r[0].lastSon, c))
else:
let asTmp = evalOnce(c.graph, r[0], c.owner)
result = newTree(nkStmtList, newTree(nkRaiseStmt, asTmp), genWasMoved(asTmp.lastSon, c))
else: else:
result = copyNode(n) result = copyNode(n)
recurse(n, result) recurse(n, result)