From a2e4ab2e4e3570cd313e8d5422b276625f87e0d7 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 12 Oct 2017 17:42:15 +0200 Subject: [PATCH] simple programs now work with the new destroyer pass --- compiler/destroyer.nim | 86 +++++++++++++++++++++--------------------- compiler/semexprs.nim | 7 ++-- compiler/semstmts.nim | 21 ++++++----- compiler/transf.nim | 2 +- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/compiler/destroyer.nim b/compiler/destroyer.nim index db26c5366b..a650df7dd0 100644 --- a/compiler/destroyer.nim +++ b/compiler/destroyer.nim @@ -179,28 +179,34 @@ proc genDestroy(t: PType; dest: PNode): PNode = assert t.destructor != nil result = newTree(nkCall, newSymNode(t.destructor), newTree(nkHiddenAddr, dest)) +proc addTopVar(c: var Con; v: PNode) = + c.topLevelVars.add newTree(nkIdentDefs, v, emptyNode, emptyNode) + +proc p(n: PNode; c: var Con): PNode + +template recurse(n, dest) = + for i in 0.. 0: c.addTopVar(newSymNode c.tmp) result = newNodeI(nkStmtList, n.info) if c.topLevelVars.len > 0: result.add c.topLevelVars if c.destroys.len > 0: - result.add newTryFinally(stmtList, c.destroys) + result.add newTryFinally(body, c.destroys) else: - result.add stmtList + result.add body + + echo "transformed into: ", result diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index c336afc89d..e8989f8328 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1383,9 +1383,10 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode = typeMismatch(n.info, lhs.typ, rhs.typ) n.sons[1] = fitNode(c, le, rhs, n.info) - if tfHasAsgn in lhs.typ.flags and not lhsIsResult and - mode != noOverloadedAsgn: - return overloadedAsgn(c, lhs, n.sons[1]) + when not newDestructors: + if tfHasAsgn in lhs.typ.flags and not lhsIsResult and + mode != noOverloadedAsgn: + return overloadedAsgn(c, lhs, n.sons[1]) fixAbstractType(c, n) asgnToResultVar(c, n, n.sons[0], n.sons[1]) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index ccf332d3a8..c8eda9d941 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -100,15 +100,16 @@ proc semProc(c: PContext, n: PNode): PNode include semdestruct proc semDestructorCheck(c: PContext, n: PNode, flags: TExprFlags) {.inline.} = - if efAllowDestructor notin flags and - n.kind in nkCallKinds+{nkObjConstr,nkBracket}: - if instantiateDestructor(c, n.typ) != nil: - localError(n.info, warnDestructor) - # This still breaks too many things: - when false: - if efDetermineType notin flags and n.typ.kind == tyTypeDesc and - c.p.owner.kind notin {skTemplate, skMacro}: - localError(n.info, errGenerated, "value expected, but got a type") + when not newDestructors: + if efAllowDestructor notin flags and + n.kind in nkCallKinds+{nkObjConstr,nkBracket}: + if instantiateDestructor(c, n.typ) != nil: + localError(n.info, warnDestructor) + # This still breaks too many things: + when false: + if efDetermineType notin flags and n.typ.kind == tyTypeDesc and + c.p.owner.kind notin {skTemplate, skMacro}: + localError(n.info, errGenerated, "value expected, but got a type") proc semExprBranch(c: PContext, n: PNode): PNode = result = semExpr(c, n) @@ -116,7 +117,7 @@ proc semExprBranch(c: PContext, n: PNode): PNode = # XXX tyGenericInst here? semProcvarCheck(c, result) if result.typ.kind == tyVar: result = newDeref(result) - when not newDestructors: semDestructorCheck(c, result, {}) + semDestructorCheck(c, result, {}) proc semExprBranchScope(c: PContext, n: PNode): PNode = openScope(c) diff --git a/compiler/transf.nim b/compiler/transf.nim index f5ea8feb91..2a33c60a61 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -973,7 +973,7 @@ proc transformBody*(module: PSym, n: PNode, prc: PSym): PNode = incl(result.flags, nfTransf) when useEffectSystem: trackProc(prc, result) if prc.kind == skFunc: - result = injectDestructorCalls(prc, n) + result = injectDestructorCalls(prc, result) #if prc.name.s == "testbody": # echo renderTree(result)