fixes #5617, 'copyLineInfo' addition (#8523)

This commit is contained in:
andri lim
2018-08-06 04:38:21 +07:00
committed by Andreas Rumpf
parent 6fffadb7fd
commit 6e3d1dced5
6 changed files with 40 additions and 23 deletions

View File

@@ -91,6 +91,7 @@
- ``parseOct`` and ``parseBin`` in parseutils now also support the ``maxLen`` argument similar to ``parseHexInt``
- Added the proc ``flush`` for memory mapped files.
- Added the ``MemMapFileStream``.
- Added ``macros.copyLineInfo`` to copy lineInfo from other node
### Library changes

View File

@@ -1422,24 +1422,23 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
ensureKind(rkNode)
if c.callsite != nil: regs[ra].node = c.callsite
else: stackTrace(c, tos, pc, errFieldXNotFound & "callsite")
of opcNGetFile:
decodeB(rkNode)
of opcNGetLineInfo:
decodeBImm(rkNode)
let n = regs[rb].node
regs[ra].node = newStrNode(nkStrLit, toFullPath(c.config, n.info))
case imm
of 0: # getFile
regs[ra].node = newStrNode(nkStrLit, toFullPath(c.config, n.info))
of 1: # getLine
regs[ra].node = newIntNode(nkIntLit, n.info.line.int)
of 2: # getColumn
regs[ra].node = newIntNode(nkIntLit, n.info.col)
else:
internalAssert c.config, false
regs[ra].node.info = n.info
regs[ra].node.typ = n.typ
of opcNGetLine:
of opcNSetLineInfo:
decodeB(rkNode)
let n = regs[rb].node
regs[ra].node = newIntNode(nkIntLit, n.info.line.int)
regs[ra].node.info = n.info
regs[ra].node.typ = n.typ
of opcNGetColumn:
decodeB(rkNode)
let n = regs[rb].node
regs[ra].node = newIntNode(nkIntLit, n.info.col)
regs[ra].node.info = n.info
regs[ra].node.typ = n.typ
regs[ra].node.info = regs[rb].node.info
of opcEqIdent:
decodeBC(rkInt)
# aliases for shorter and easier to understand code below

View File

@@ -102,7 +102,7 @@ type
opcNError,
opcNWarning,
opcNHint,
opcNGetLine, opcNGetColumn, opcNGetFile,
opcNGetLineInfo, opcNSetLineInfo,
opcEqIdent,
opcStrToIdent,
opcGetImpl,

View File

@@ -1181,14 +1181,14 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
of mSameNodeType: genBinaryABC(c, n, dest, opcSameNodeType)
of mNLineInfo:
case n[0].sym.name.s
of "getFile":
genUnaryABC(c, n, dest, opcNGetFile)
of "getLine":
genUnaryABC(c, n, dest, opcNGetLine)
of "getColumn":
genUnaryABC(c, n, dest, opcNGetColumn)
else:
internalAssert c.config, false
of "getFile": genUnaryABI(c, n, dest, opcNGetLineInfo, 0)
of "getLine": genUnaryABI(c, n, dest, opcNGetLineInfo, 1)
of "getColumn": genUnaryABI(c, n, dest, opcNGetLineInfo, 2)
of "copyLineInfo":
internalAssert c.config, n.len == 3
unused(c, n, dest)
genBinaryStmt(c, n, opcNSetLineInfo)
else: internalAssert c.config, false
of mNHint:
unused(c, n, dest)
genUnaryStmt(c, n, opcNHint)

View File

@@ -432,6 +432,9 @@ proc getLine(arg: NimNode): int {.magic: "NLineInfo", noSideEffect.}
proc getColumn(arg: NimNode): int {.magic: "NLineInfo", noSideEffect.}
proc getFile(arg: NimNode): string {.magic: "NLineInfo", noSideEffect.}
proc copyLineInfo*(arg: NimNode, info: NimNode) {.magic: "NLineInfo", noSideEffect.}
## copy lineinfo from info node
proc lineInfoObj*(n: NimNode): LineInfo {.compileTime.} =
## returns ``LineInfo`` of ``n``, using absolute path for ``filename``
result.filename = n.getFile

View File

@@ -0,0 +1,14 @@
# issue #5617, feature request
# Ability to set a NimNode's lineinfo
import macros
type
Test = object
macro mixer(n: typed): untyped =
let x = newIdentNode("echo")
x.copyLineInfo(n)
result = newLit(x.lineInfo == n.lineInfo)
var z = mixer(Test)
doAssert z