aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2020-04-07 17:40:59 -0500
committerGanesh Viswanathan <dev@genotrance.com>2020-04-07 17:40:59 -0500
commit4ccde62d4f3ed699832c845fde83598027820d8e (patch)
treecc2964349fd045dc780991809d583ff1e45a95ff
parent4c513a50de64d7675b652e08deeb4c7c0db4124a (diff)
downloadnimterop-4ccde62d4f3ed699832c845fde83598027820d8e.tar.gz
nimterop-4ccde62d4f3ed699832c845fde83598027820d8e.zip
ast2 fix issue 156 - abstract function pointer
-rw-r--r--nimterop.nimble2
-rw-r--r--nimterop/ast2.nim29
-rw-r--r--tests/include/tast2.h24
-rw-r--r--tests/tast2.nim12
-rw-r--r--tests/tmath.nim3
5 files changed, 63 insertions, 7 deletions
diff --git a/nimterop.nimble b/nimterop.nimble
index c296ab5..58a99b0 100644
--- a/nimterop.nimble
+++ b/nimterop.nimble
@@ -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
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim
index 09f5e9d..7a4b70a 100644
--- a/nimterop/ast2.nim
+++ b/nimterop/ast2.nim
@@ -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
diff --git a/tests/include/tast2.h b/tests/include/tast2.h
index 75e9446..348e225 100644
--- a/tests/include/tast2.h
+++ b/tests/include/tast2.h
@@ -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
diff --git a/tests/tast2.nim b/tests/tast2.nim
index 4e60172..d1b0f26 100644
--- a/tests/tast2.nim
+++ b/tests/tast2.nim
@@ -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.} \ No newline at end of file
+ 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.}
diff --git a/tests/tmath.nim b/tests/tmath.nim
index 9f3df79..1f804ab 100644
--- a/tests/tmath.nim
+++ b/tests/tmath.nim
@@ -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