From 3ef92a34ee0712139d7527d0b3d3f8e725d9d549 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 16 Feb 2014 19:17:12 +0100 Subject: fixes #922 --- compiler/vmgen.nim | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'compiler/vmgen.nim') diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index a6d044e24..6fca7f907 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -321,9 +321,18 @@ proc genAndOr(c: PCtx; n: PNode; opc: TOpcode; dest: var TDest) = c.gen(n.sons[2], dest) c.patch(L1) +proc nilLiteral(n: PNode): PNode = + if n.kind == nkNilLit and n.typ.sym != nil and + n.typ.sym.magic == mPNimrodNode: + let nilo = newNodeIT(nkNilLit, n.info, n.typ) + result = newNodeIT(nkMetaNode, n.info, n.typ) + result.add nilo + else: + result = n + proc rawGenLiteral(c: PCtx; n: PNode): int = result = c.constants.len - c.constants.add n + c.constants.add n.nilLiteral internalAssert result < 0x7fff proc sameConstant*(a, b: PNode): bool = @@ -1109,7 +1118,12 @@ proc getNullValue(typ: PType, info: TLineInfo): PNode = result = newNodeIT(nkFloatLit, info, t) of tyVar, tyPointer, tyPtr, tyCString, tySequence, tyString, tyExpr, tyStmt, tyTypeDesc, tyStatic, tyRef: - result = newNodeIT(nkNilLit, info, t) + if t.sym != nil and t.sym.magic == mPNimrodNode: + let nilo = newNodeIT(nkNilLit, info, t) + result = newNodeIT(nkMetaNode, info, t) + result.add nilo + else: + result = newNodeIT(nkNilLit, info, t) of tyProc: if t.callConv != ccClosure: result = newNodeIT(nkNilLit, info, t) -- cgit 1.4.1-2-gfad0 From 3ec29738757c4b8eb0a7071333a0351d802e30a6 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 17 Feb 2014 08:26:44 +0100 Subject: fixes #926 --- compiler/vmgen.nim | 19 ++++++++++++------- tests/macros/tvarnimnode.nim | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 tests/macros/tvarnimnode.nim (limited to 'compiler/vmgen.nim') diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 6fca7f907..80cf4a9bf 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -916,23 +916,28 @@ proc requiresCopy(n: PNode): bool = proc unneededIndirection(n: PNode): bool = n.typ.skipTypes(abstractInst-{tyTypeDesc}).kind == tyRef -proc skipDeref(n: PNode): PNode = - if n.kind in {nkDerefExpr, nkHiddenDeref} and unneededIndirection(n.sons[0]): - result = n.sons[0] - else: - result = n - proc genAddrDeref(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode; flags: TGenFlags) = # a nop for certain types let flags = if opc == opcAddr: flags+{gfAddrOf} else: flags - if unneededIndirection(n.sons[0]): + # consider: + # proc foo(f: var ref int) = + # f = new(int) + # proc blah() = + # var x: ref int + # foo x + # + # The type of 'f' is 'var ref int' and of 'x' is 'ref int'. Hence for + # nkAddr we must not use 'unneededIndirection', but for deref we use it. + if opc != opcAddr and unneededIndirection(n.sons[0]): gen(c, n.sons[0], dest, flags) + message(n.info, warnUser, "YES") else: let tmp = c.genx(n.sons[0], flags) if dest < 0: dest = c.getTemp(n.typ) gABC(c, n, opc, dest, tmp) c.freeTemp(tmp) + message(n.info, warnUser, "NO") proc whichAsgnOpc(n: PNode): TOpcode = case n.typ.skipTypes(abstractRange-{tyTypeDesc}).kind diff --git a/tests/macros/tvarnimnode.nim b/tests/macros/tvarnimnode.nim new file mode 100644 index 000000000..73fcc16ea --- /dev/null +++ b/tests/macros/tvarnimnode.nim @@ -0,0 +1,19 @@ +discard """ + output: 10 +""" + +#bug #926 + +import macros + +proc test(f: var PNimrodNode) {.compileTime.} = + f = newNimNode(nnkStmtList) + f.add newCall(newIdentNode("echo"), newLit(10)) + +macro blah(prc: stmt): stmt = + result = prc + + test(result) + +proc test() {.blah.} = + echo 5 -- cgit 1.4.1-2-gfad0 From 14bdedb0f7a8dbd15d685866fc022ad559a4e35b Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 17 Feb 2014 08:27:16 +0100 Subject: got rid of debugging code --- compiler/vmgen.nim | 2 -- 1 file changed, 2 deletions(-) (limited to 'compiler/vmgen.nim') diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 80cf4a9bf..206cca0d7 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -931,13 +931,11 @@ proc genAddrDeref(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode; # nkAddr we must not use 'unneededIndirection', but for deref we use it. if opc != opcAddr and unneededIndirection(n.sons[0]): gen(c, n.sons[0], dest, flags) - message(n.info, warnUser, "YES") else: let tmp = c.genx(n.sons[0], flags) if dest < 0: dest = c.getTemp(n.typ) gABC(c, n, opc, dest, tmp) c.freeTemp(tmp) - message(n.info, warnUser, "NO") proc whichAsgnOpc(n: PNode): TOpcode = case n.typ.skipTypes(abstractRange-{tyTypeDesc}).kind -- cgit 1.4.1-2-gfad0