Macros
macro cdeclmacro(name: string; def: untyped)
-
Macro helper for wrapping a C macro that declares a new C variable.
It handles emitting the appropriate C code for calling the macro. Additionally it defines a new Nim variable using importc which imports the declared variable.
Example:
import macros import cdecl {.emit: """/*TYPESECTION*/ /* define example C Macro for testing */ #define C_DEFINE_VAR(NM, SZ) int NM[SZ] #define C_DEFINE_VAR_DUO(NM, SZ, NM2) int NM[SZ] """.} proc CDefineVar*(name: CToken, size: static[int]): array[size, int] {. cdeclmacro: "C_DEFINE_VAR".} # Then it's possible to invoke CDefineVar to call the C macro and # generate a variable: const cVarSz = 4 CDefineVar(myVar, cVarSz) static: discard """`CDefineVar` generates code that looks like:""" discard quote do: template CDefineVar*(name: untyped, size: static[int]) = var name* {.inject, importc, nodecl.}: array[size, int] {.emit: "/*VARSECTION*/\nC_DEFINE_VAR($1, $2); " % [ symbolName(name), $size, ] .}
macro symbolName(x: typed): string