Skip to content

Commit

Permalink
code cleanups for the upcoming reworked destructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Oct 12, 2017
1 parent 8a2f481 commit d6c401a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 64 deletions.
2 changes: 1 addition & 1 deletion compiler/options.nim
Expand Up @@ -14,7 +14,7 @@ const
hasTinyCBackend* = defined(tinyc)
useEffectSystem* = true
useWriteTracking* = false
newDestructors* = true
newDestructors* = defined(nimV2)
hasFFI* = defined(useFFI)
newScopeForIf* = true
useCaas* = not defined(noCaas)
Expand Down
59 changes: 0 additions & 59 deletions compiler/semdestruct.nim
Expand Up @@ -184,62 +184,3 @@ proc createDestructorCall(c: PContext, s: PSym): PNode =
useSym(destructableT.destructor, c.graph.usageSym),
useSym(s, c.graph.usageSym)]))
result = newNode(nkDefer, s.info, @[call])

proc insertDestructors(c: PContext,
varSection: PNode): tuple[outer, inner: PNode] =
# Accepts a var or let section.
#
# When a var section has variables with destructors
# the var section is split up and finally blocks are inserted
# immediately after all "destructable" vars
#
# In case there were no destrucable variables, the proc returns
# (nil, nil) and the enclosing stmt-list requires no modifications.
#
# Otherwise, after the try blocks are created, the rest of the enclosing
# stmt-list should be inserted in the most `inner` such block (corresponding
# to the last variable).
#
# `outer` is a statement list that should replace the original var section.
# It will include the new truncated var section followed by the outermost
# try block.
let totalVars = varSection.sonsLen
for j in countup(0, totalVars - 1):
let
varId = varSection[j][0]
varTyp = varId.sym.typ
info = varId.info

if varTyp == nil or sfGlobal in varId.sym.flags: continue
let destructableT = instantiateDestructor(c, varTyp)

if destructableT != nil:
var tryStmt = newNodeI(nkTryStmt, info)

if j < totalVars - 1:
var remainingVars = newNodeI(varSection.kind, info)
remainingVars.sons = varSection.sons[(j+1)..varSection.len-1]
let (outer, inner) = insertDestructors(c, remainingVars)
if outer != nil:
tryStmt.addSon(outer)
result.inner = inner
else:
result.inner = newNodeI(nkStmtList, info)
result.inner.addSon(remainingVars)
tryStmt.addSon(result.inner)
else:
result.inner = newNodeI(nkStmtList, info)
tryStmt.addSon(result.inner)

tryStmt.addSon(
newNode(nkFinally, info, @[
semStmt(c, newNode(nkCall, info, @[
useSym(destructableT.destructor, c.graph.usageSym),
useSym(varId.sym, c.graph.usageSym)]))]))

result.outer = newNodeI(nkStmtList, info)
varSection.sons.setLen(j+1)
result.outer.addSon(varSection)
result.outer.addSon(tryStmt)

return
9 changes: 5 additions & 4 deletions compiler/semstmts.nim
Expand Up @@ -116,7 +116,7 @@ proc semExprBranch(c: PContext, n: PNode): PNode =
# XXX tyGenericInst here?
semProcvarCheck(c, result)
if result.typ.kind == tyVar: result = newDeref(result)
semDestructorCheck(c, result, {})
when not newDestructors: semDestructorCheck(c, result, {})

proc semExprBranchScope(c: PContext, n: PNode): PNode =
openScope(c)
Expand Down Expand Up @@ -607,7 +607,7 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
if def.kind == nkPar: v.ast = def[j]
setVarType(v, tup.sons[j])
b.sons[j] = newSymNode(v)
addDefer(c, result, v)
when not newDestructors: addDefer(c, result, v)
checkNilable(v)
if sfCompileTime in v.flags: hasCompileTime = true
if hasCompileTime: vm.setupCompileTimeVar(c.module, c.cache, result)
Expand Down Expand Up @@ -1277,8 +1277,9 @@ proc semOverride(c: PContext, s: PSym, n: PNode) =
case s.name.s.normalize
of "destroy", "=destroy":
doDestructorStuff(c, s, n)
if not experimentalMode(c):
localError n.info, "use the {.experimental.} pragma to enable destructors"
when not newDestructors:
if not experimentalMode(c):
localError n.info, "use the {.experimental.} pragma to enable destructors"
incl(s.flags, sfUsed)
of "deepcopy", "=deepcopy":
if s.typ.len == 2 and
Expand Down

0 comments on commit d6c401a

Please sign in to comment.