mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-07-17 21:39:28 +00:00
53 lines
1.8 KiB
Nim
53 lines
1.8 KiB
Nim
## Helpers shared by the language-specific binding generators (cpp.nim, c.nim).
|
|
## Kept here so the per-proc envelope naming, lib-prefix stripping and
|
|
## proc-classification logic live in one place rather than being copy-pasted
|
|
## into each backend.
|
|
|
|
import std/strutils
|
|
import ./meta, ./string_helpers
|
|
|
|
proc stripLibPrefix*(procName, libName: string): string =
|
|
## Drops the `<lib>_` prefix from an exported C symbol, e.g.
|
|
## `stripLibPrefix("timer_echo", "timer")` → `"echo"`.
|
|
let prefix = libName & "_"
|
|
if procName.startsWith(prefix):
|
|
return procName[prefix.len .. ^1]
|
|
return procName
|
|
|
|
proc reqStructName*(p: FFIProcMeta): string =
|
|
## Mirrors the Nim macro: `<PascalCase(procName)>Req`, or `...CtorReq` for a
|
|
## constructor. The per-proc envelope every backend encodes onto the wire.
|
|
let camel = snakeToPascalCase(p.procName)
|
|
if p.kind == FFIKind.CTOR:
|
|
camel & "CtorReq"
|
|
else:
|
|
camel & "Req"
|
|
|
|
type ClassifiedProcs* = object
|
|
ctors*: seq[FFIProcMeta]
|
|
methods*: seq[FFIProcMeta]
|
|
dtorProcName*: string
|
|
|
|
proc classifyProcs*(procs: seq[FFIProcMeta]): ClassifiedProcs =
|
|
## Splits the registry into constructors, instance methods and (the first)
|
|
## destructor symbol — the split every backend needs before emitting a
|
|
## high-level context wrapper.
|
|
var c: ClassifiedProcs
|
|
for p in procs:
|
|
case p.kind
|
|
of FFIKind.CTOR:
|
|
c.ctors.add(p)
|
|
of FFIKind.FFI:
|
|
c.methods.add(p)
|
|
of FFIKind.DTOR:
|
|
if c.dtorProcName.len == 0:
|
|
c.dtorProcName = p.procName
|
|
c
|
|
|
|
proc libTypeName*(ctors: seq[FFIProcMeta], libName: string): string =
|
|
## The user's library type name (e.g. `MyTimer`), taken from the first ctor
|
|
## or derived from `libName` when the library declares none.
|
|
if ctors.len > 0:
|
|
return ctors[0].libTypeName
|
|
capitalizeFirstLetter(libName)
|