PMunch / futhark

Automatic wrapping of C headers in Nim
MIT License
394 stars 22 forks source link

Use C function pointer argument names in proc types #117

Open jon-edward opened 3 months ago

jon-edward commented 3 months ago

Currently, futhark replaces argument names of function pointers with a[some number].

This can be seen with the simple example:

// ./include/testlib.h

typedef struct ContainsFunctionPointer {
    int (*myFunc)(int descriptiveParameter1, int descriptiveParameter2);
} ContainsFunctionPointer;
# ./test.nim

import std/os

import futhark

const currentDir = currentSourcePath.parentDir

importc:
    outputPath currentDir / "generated.nim"
    path currentDir / "include"
    "testLib.h"

The result of generated.nim after nim r -d:nodeclguards ./test.nim (no declaration guards for readability, the existence or nonexistence of this flag does not change the resulting type):

# ./generated.nim

type
  struct_ContainsFunctionPointer* {.pure, inheritable, bycopy.} = object
    myFunc*: proc (a0: cint; a1: cint): cint {.cdecl.}
  ContainsFunctionPointer* = struct_ContainsFunctionPointer

It might be nicer for library usage if the resulting generated.nim was something more like:

type
  struct_ContainsFunctionPointer* {.pure, inheritable, bycopy.} = object
    myFunc*: proc (descriptiveParameter1: cint; descriptiveParameter2: cint): cint {.cdecl.}
  ContainsFunctionPointer* = struct_ContainsFunctionPointer

For users of the wrapper, it will offer more insight into what kind of objects should be passed to procs (more than just type information).

jon-edward commented 3 months ago

This seems to be an issue with opir, perhaps at https://github.com/PMunch/futhark/blob/783f2aff860f582ca7bc8d6f4801e13822a219c4/src/opir.nim#L322-L326

AmjadHD commented 2 months ago

This needs to be passed a cursor https://github.com/PMunch/futhark/blob/8b5509892bdca65dd259ce450123cc9eaab8f7f5/src/opir.nim#L126-L127