diff options
author | Arne Döring <arne.doering@gmx.net> | 2019-09-18 20:02:30 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-09-18 20:02:30 +0200 |
commit | 188ce5f3ee5c83d749918f9f286537d65a513c53 (patch) | |
tree | d3a12f3b637c43aedcfec76a4acb736ac874ff23 | |
parent | f8000d4ba14414657b2255fda3ca058074166b76 (diff) | |
download | Nim-188ce5f3ee5c83d749918f9f286537d65a513c53.tar.gz |
add nodeId proc to macros (#11456)
* add nodeId proc to macros * add doc comment. * fix typo
-rw-r--r-- | compiler/ast.nim | 3 | ||||
-rw-r--r-- | compiler/condsyms.nim | 1 | ||||
-rw-r--r-- | compiler/vm.nim | 6 | ||||
-rw-r--r-- | compiler/vmdef.nim | 2 | ||||
-rw-r--r-- | compiler/vmgen.nim | 2 | ||||
-rw-r--r-- | 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, |