PMunch / futhark

Automatic wrapping of C headers in Nim
MIT License
391 stars 21 forks source link

FILE* is translated incorrectly to ptr File -> ptr ptr CFile #10

Open de-odex opened 2 years ago

de-odex commented 2 years ago
/home/deodex/Documents/CODE/Nim/natsuhi/src/natsuhi.nim(40, 29) Error: type mismatch: got <typeof(nil), File>
but expected one of:
proc notcursescoreinit(opts: ptr notcursesoptions_1342177605;
                       fp: ptr File_1342177650): ptr notcurses
  first type mismatch at position: 2
  required type for fp: ptr File_1342177650
  but expression 'stdout' is of type: File

expression: notcursescoreinit(nil, stdout)

this should work (this does in a nimterop wrapper); fp should just be File or ptr CFile

  File_1342177650* = (when declared(File):
    File
   else:
    File_1342177649)

offending C header file (notcurses/include/notcurses/notcurses.h)

// The same as notcurses_init(), but without any multimedia functionality,
// allowing for a svelter binary. Link with notcurses-core if this is used.
API ALLOC struct notcurses* notcurses_core_init(const notcurses_options* opts, FILE* fp);
de-odex commented 2 years ago

futhark perhaps should check if nim has the type in posix or io or so before going into the C headers and generating a new type, but i can see how that would be tricky

e: i misunderstood, but the comment before this still applies

de-odex commented 2 years ago

workaround for this issue is this

import std/[typetraits, os]
const clangIncludeDir {.strdefine.} = "/usr/lib/clang/13.0.0/include"
type CFile* = typeof(File.pointerBase)
importc:
  absPath clangIncludeDir
  path currentSourcePath() / ".." / ".." / "notcurses/include/notcurses/"
  rename FILE, CFile
  "notcurses.h"
de-odex commented 2 years ago

Okay, I think I fully understand the idea now, what is needed is better retyping for other types like function parameter types and so so that the workaround doesn’t need to muck around redefining an internal type and could just change ptr File into File

PMunch commented 2 years ago

What do you mean by better retyping? Isn't it working well for you? The problem here is that I want to keep Futhark as automatic as absolutely possible, so adding overrides for defined things isn't a great solution. But I thought FILE was actually working, I could've sworn I've used those with a Futhark wrapped library before..

de-odex commented 2 years ago

It only "works" if I manually get what the pointer base of File is and rename FILE to that. I was thinking better utilities like being able to rename a not-named type like FILE* itself into File without needing to get the pointer base