From 188ce5f3ee5c83d749918f9f286537d65a513c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Wed, 18 Sep 2019 20:02:30 +0200 Subject: [PATCH] add nodeId proc to macros (#11456) * add nodeId proc to macros * add doc comment. * fix typo --- compiler/ast.nim | 3 ++- compiler/condsyms.nim | 1 + compiler/vm.nim | 6 ++++++ compiler/vmdef.nim | 2 +- compiler/vmgen.nim | 2 ++ lib/core/macros.nim | 6 ++++++ 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index addb2c33b..c3ed33914 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -666,7 +666,8 @@ type mInstantiationInfo, mGetTypeInfo, mNimvm, mIntDefine, mStrDefine, mBoolDefine, mRunnableExamples, mException, mBuiltinType, mSymOwner, mUncheckedArray, mGetImplTransf, - mSymIsInstantiationOf + mSymIsInstantiationOf, mNodeId + # things that we can evaluate safely at compile time, even if not asked for it: const diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index 00ce352f2..3dc614a37 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -87,6 +87,7 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimHasDefault") defineSymbol("nimMacrosSizealignof") defineSymbol("nimNoZeroExtendMagic") + defineSymbol("nimMacrosGetNodeId") for f in low(Feature)..high(Feature): defineSymbol("nimHas" & $f) diff --git a/compiler/vm.nim b/compiler/vm.nim index 40d12db97..0496e37ca 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -1472,6 +1472,12 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = regs[ra].node = copyNode(a) else: stackTrace(c, tos, pc, errFieldXNotFound & "ident") + of opcNodeId: + decodeB(rkInt) + when defined(useNodeIds): + regs[ra].intVal = regs[rb].node.id + else: + regs[ra].intVal = -1 of opcNGetType: let rb = instr.regB let rc = instr.regC diff --git a/compiler/vmdef.nim b/compiler/vmdef.nim index 2f6cbcb85..54c18b03a 100644 --- a/compiler/vmdef.nim +++ b/compiler/vmdef.nim @@ -99,7 +99,7 @@ type opcNNewNimNode, opcNCopyNimNode, opcNCopyNimTree, opcNDel, opcGenSym, opcNccValue, opcNccInc, opcNcsAdd, opcNcsIncl, opcNcsLen, opcNcsAt, - opcNctPut, opcNctLen, opcNctGet, opcNctHasNext, opcNctNext, + opcNctPut, opcNctLen, opcNctGet, opcNctHasNext, opcNctNext, opcNodeId, opcSlurp, opcGorge, diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 0b41fed28..898916c64 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1324,6 +1324,8 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) = c.gABx(n, opcNodeToReg, a, a) c.genAsgnPatch(arg, a) c.freeTemp(a) + of mNodeId: + c.genUnaryABC(n, dest, opcNodeId) else: # mGCref, mGCunref, globalError(c.config, n.info, "cannot generate code for: " & $m) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index b0414153e..73f5790fa 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -1426,6 +1426,12 @@ proc boolVal*(n: NimNode): bool {.compileTime, noSideEffect.} = if n.kind == nnkIntLit: n.intVal != 0 else: n == bindSym"true" # hacky solution for now +when defined(nimMacrosGetNodeId): + proc nodeID*(n: NimNode): int {.magic: NodeId.} + ## Returns the id of ``n``, when the compiler has been compiled + ## with the flag ``-d:useNodeids``, otherwise returns ``-1``. This + ## proc is for the purpose to debug the compiler only. + macro expandMacros*(body: typed): untyped = ## Expands one level of macro - useful for debugging. ## Can be used to inspect what happens when a macro call is expanded,