diff --git a/compiler/vmops.nim b/compiler/vmops.nim index 5087dabfa..792feb0be 100644 --- a/compiler/vmops.nim +++ b/compiler/vmops.nim @@ -146,5 +146,14 @@ proc registerAdditionalOps*(c: PCtx) = registerCallback c, "stdlib.macros.symBodyHash", proc (a: VmArgs) {.nimcall.} = let n = getNode(a, 0) - if n.kind != nkSym: raise newException(ValueError, "node is not a symbol") + if n.kind != nkSym: + stackTrace(c, PStackFrame(prc: c.prc.sym, comesFrom: 0, next: nil), c.exceptionInstr, + "symBodyHash() requires a symbol. '" & $n & "' is of kind '" & $n.kind & "'", n.info) setResult(a, $symBodyDigest(c.graph, n.sym)) + + registerCallback c, "stdlib.macros.isExported", proc(a: VmArgs) {.nimcall.} = + let n = getNode(a, 0) + if n.kind != nkSym: + stackTrace(c, PStackFrame(prc: c.prc.sym, comesFrom: 0, next: nil), c.exceptionInstr, + "isExported() requires a symbol. '" & $n & "' is of kind '" & $n.kind & "'", n.info) + setResult(a, sfExported in n.sym.flags) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 3fe53f6f5..4e44b9b4f 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -1611,3 +1611,6 @@ when defined(nimMacrosSizealignof): ## from a field of a type. Therefore it only requires one argument ## instead of two. Returns a negative value if the Nim compiler ## does not know the offset. + +proc isExported*(n: NimNode): bool {.noSideEffect.} = + ## Returns whether the symbol is exported or not. diff --git a/tests/macros/tisexported.nim b/tests/macros/tisexported.nim new file mode 100644 index 000000000..53766edf5 --- /dev/null +++ b/tests/macros/tisexported.nim @@ -0,0 +1,10 @@ +import macros + +proc t1* = discard +proc t2 = discard + +macro check(p1: typed, p2: typed) = + doAssert isExported(p1) == true + doAssert isExported(p2) == false + +check t1, t2