ast2 fix issue 156 - abstract function pointer

This commit is contained in:
Ganesh Viswanathan 2020-04-07 17:40:59 -05:00
commit 4ccde62d4f
5 changed files with 63 additions and 7 deletions

View file

@ -10,7 +10,7 @@ installDirs = @["nimterop"]
installFiles = @["config.nims"]
# Dependencies
requires "nim >= 0.20.2", "regex >= 0.13.1", "cligen >= 0.9.43"
requires "nim >= 0.20.2", "regex#v0.13.1", "cligen >= 0.9.43"
import nimterop/docs

View file

@ -531,10 +531,11 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn
result = nil
else:
let
fdecl = node[start+1].anyChildInTree("function_declarator")
adecl = node[start+1].anyChildInTree("array_declarator")
fdecl = node[start+1].firstChildInTree("function_declarator")
afdecl = node[start+1].firstChildInTree("abstract_function_declarator")
adecl = node[start+1].firstChildInTree("array_declarator")
abst = node[start+1].getName() == "abstract_pointer_declarator"
if fdecl.isNil and adecl.isNil:
if fdecl.isNil and afdecl.isNil and adecl.isNil:
if abst:
# Only for proc with no named param with pointer type
# Create a param name based on offset
@ -577,6 +578,18 @@ proc newIdentDefs(nimState: NimState, name: string, node: TSNode, offset: SomeIn
result.add pident
result.add nimState.getTypeProc(name, node[start+1], node[start])
result.add newNode(nkEmpty)
elif not afdecl.isNil:
# Only for proc with no named param with function pointer type
# Create a param name based on offset
#
# int func(int (*)(int *));
let
pname = "a" & $(offset+1)
pident = nimState.getIdent(pname, tinfo, exported)
procTy = nimState.getTypeProc(name, node[start+1], node[start])
result.add pident
result.add procTy
result.add newNode(nkEmpty)
elif not adecl.isNil:
# Named param, array type
let
@ -963,6 +976,9 @@ proc getTypeProc(nimState: NimState, name: string, node, rnode: TSNode): PNode =
# node could have nested pointers
tcount = node.getPtrCount()
# Nameless function pointer
afdecl = node.firstChildInTree("abstract_function_declarator")
# Name could be nested pointer to function
#
# (..
@ -976,7 +992,12 @@ proc getTypeProc(nimState: NimState, name: string, node, rnode: TSNode): PNode =
# )
# )
# )
ncount = node.getAtom().tsNodeParent().getPtrCount(reverse = true)
ncount =
if not afdecl.isNil:
# Pointer to function pointer
afdecl[0].getXCount("abstract_pointer_declarator")
else:
node.getAtom().tsNodeParent().getPtrCount(reverse = true)
# Return type
var

View file

@ -123,6 +123,17 @@ typedef MagickBooleanType
const int,void *),
(*UpdateImageViewMethod)(ImageView *,const size_t,const int,void *);
// Issue #156, math.h
void
*absfunptr1 (int (*)(struct A0 *)),
**absfunptr2 (int (**)(struct A1 *)),
absfunptr3 (int *(*)(struct A2 *)),
*absfunptr4 (int *(**)(struct A3 *)),
absfunptr5 (int (*a)(A4 *));
int sqlite3_bind_blob(struct A1*, int, const void*, int n, void(*)(void*));
@ -241,6 +252,7 @@ void
(*pcre_free)(void *),
*(*pcre_stack_malloc)(size_t);
typedef int ImageView, MagickBooleanType;
typedef MagickBooleanType
(*DuplexTransferImageViewMethod)(const ImageView *,const ImageView *,
ImageView *,const size_t,const int,void *),
@ -250,6 +262,18 @@ typedef MagickBooleanType
const int,void *),
(*UpdateImageViewMethod)(ImageView *,const size_t,const int,void *);
// Issue #156, math.h
void
*absfunptr1 (int (*)(struct A0 *)),
**absfunptr2 (int (**)(struct A1 *)),
absfunptr3 (int *(*)(struct A2 *)),
*absfunptr4 (int *(**)(struct A3 *)),
absfunptr5 (int (*a)(A4 *));
int sqlite3_bind_blob(struct A1*, int, const void*, int n, void(*)(void*));
#endif

View file

@ -324,4 +324,14 @@ assert TransferImageViewMethod is
proc (a1: ptr ImageView; a2: ptr ImageView; a3: uint; a4: cint; a5: pointer): MagickBooleanType {.cdecl.}
assert UpdateImageViewMethod is
proc (a1: ptr ImageView; a2: uint; a3: cint; a4: pointer): MagickBooleanType {.cdecl.}
proc (a1: ptr ImageView; a2: uint; a3: cint; a4: pointer): MagickBooleanType {.cdecl.}
# Issue #156, math.h
assert absfunptr1 is proc(a1: proc(a1: ptr A0): cint {.cdecl.}): pointer {.cdecl.}
assert absfunptr2 is proc(a1: ptr proc(a1: ptr A1): cint {.cdecl.}): ptr pointer {.cdecl.}
assert absfunptr3 is proc(a1: proc(a1: ptr A2): ptr cint {.cdecl.}) {.cdecl.}
assert absfunptr4 is proc(a1: ptr proc(a1: ptr A3): ptr cint {.cdecl.}): pointer {.cdecl.}
assert absfunptr5 is proc(a1: proc(a1: ptr A4): cint {.cdecl.}) {.cdecl.}
assert sqlite3_bind_blob is
proc(a1: ptr A1, a2: cint, a3: pointer, n: cint, a5: proc(a1: pointer) {.cdecl.}): cint {.cdecl.}

View file

@ -22,7 +22,8 @@ cPlugin:
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
sym.name = sym.name.strip(chars={'_'}).replace("__", "_")
cImport cSearchPath("math.h")
const FLAGS {.strdefine.} = ""
cImport(cSearchPath("math.h"), flags = FLAGS)
check sin(5) == -0.9589242746631385
check abs(-5) == 5