aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-01-22 23:03:51 -0600
committerGanesh Viswanathan <dev@genotrance.com>2019-01-22 23:03:51 -0600
commitd6e9a491946a153e41b25eda524bd9335f1baaa6 (patch)
treee2e3ad5e8d36fe2f95f5be998650091929214a30
parentbf685d1dfc356f41e07e8c42e28969381452df7e (diff)
downloadnimterop-d6e9a491946a153e41b25eda524bd9335f1baaa6.tar.gz
nimterop-d6e9a491946a153e41b25eda524bd9335f1baaa6.zip
Partial fix for #52 - only when names differ
-rw-r--r--nimterop/grammar.nim82
-rw-r--r--tests/include/test.h14
-rw-r--r--tests/tnimterop_c.nim9
3 files changed, 80 insertions, 25 deletions
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index 9a434fe..f86db19 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -154,6 +154,25 @@ proc initGrammar() =
gStateRT.typeStr &= &" {name}* = {getPtrType(tptr&typ)}\n"
))
+ proc pDupTypeCommon(nname: string, fend: int, isEnum=false) =
+ var
+ dname = gStateRT.data[^1].val
+ ndname = gStateRT.data[^1].val.getIdentifier()
+ dptr =
+ if fend == 2:
+ "ptr "
+ else:
+ ""
+
+ if ndname != nname:
+ if isEnum:
+ if gStateRT.enums.addNewIdentifer(ndname):
+ gStateRT.enumStr &= &"type {ndname}* = {dptr}{nname}\n"
+ else:
+ if gStateRT.types.addNewIdentifer(ndname):
+ gStateRT.typeStr &=
+ &" {ndname}* {{.importc: \"{dname}\", header: {gStateRT.currentHeader}, bycopy.}} = {dptr}{nname}\n"
+
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) =
var
nname = name.getIdentifier()
@@ -250,12 +269,7 @@ proc initGrammar() =
if node.tsNodeType() == "type_definition" and
gStateRT.data[^1].name == "type_identifier" and gStateRT.data[^1].val.len != 0:
- let
- dname = gStateRT.data[^1].val
- ndname = gStateRT.data[^1].val.getIdentifier()
-
- if gStateRT.types.addNewIdentifer(ndname):
- gStateRT.typeStr &= &" {ndname}* {{.importc: \"{dname}\", header: {gStateRT.currentHeader}, bycopy.}} = {nname}\n"
+ pDupTypeCommon(nname, fend, false)
let
fieldGrammar = &"""
@@ -313,16 +327,20 @@ proc initGrammar() =
""",
proc (ast: ref Ast, node: TSNode) =
var
- offset = 0
+ fstart = 0
+ fend = 1
+
+ if gStateRT.data[^2].name == "pointer_declarator":
+ fend = 2
if gStateRT.data.len > 1 and
gStateRT.data[0].name == "type_identifier" and
gStateRT.data[1].name != "field_identifier":
- offset = 1
- pStructCommon(ast, node, gStateRT.data[0].val, offset, 1)
+ fstart = 1
+ pStructCommon(ast, node, gStateRT.data[0].val, fstart, fend)
else:
- pStructCommon(ast, node, gStateRT.data[^1].val, offset, 1)
+ pStructCommon(ast, node, gStateRT.data[^1].val, fstart, fend)
))
proc pEnumCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) =
@@ -347,19 +365,24 @@ proc initGrammar() =
i += 1
continue
- if gStateRT.consts.addNewIdentifer(fname):
- if i+1 < gStateRT.data.len-fend and
- gStateRT.data[i+1].name in gEnumVals:
+ if i+1 < gStateRT.data.len-fend and
+ gStateRT.data[i+1].name in gEnumVals:
+ if gStateRT.consts.addNewIdentifer(fname):
gStateRT.constStr &= &" {fname}* = ({gStateRT.data[i+1].val.getNimExpression()}).{nname}\n"
- try:
- count = gStateRT.data[i+1].val.parseInt() + 1
- except:
- count += 1
- i += 2
- else:
- gStateRT.constStr &= &" {fname}* = {count}.{nname}\n"
- i += 1
+ try:
+ count = gStateRT.data[i+1].val.parseInt() + 1
+ except:
count += 1
+ i += 2
+ else:
+ if gStateRT.consts.addNewIdentifer(fname):
+ gStateRT.constStr &= &" {fname}* = {count}.{nname}\n"
+ i += 1
+ count += 1
+
+ if node.tsNodeType() == "type_definition" and
+ gStateRT.data[^1].name == "type_identifier" and gStateRT.data[^1].val.len != 0:
+ pDupTypeCommon(nname, fend, true)
# enum X {}
gStateRT.grammar.add(("""
@@ -389,17 +412,26 @@ proc initGrammar() =
gStateRT.grammar.add((&"""
(type_definition
{gStateRT.grammar[^1].grammar}
- (type_identifier)
+ (type_identifier!)
+ (pointer_declarator
+ (type_identifier)
+ )
)
""",
proc (ast: ref Ast, node: TSNode) =
var
- offset = 0
+ fstart = 0
+ fend = 1
+
+ if gStateRT.data[^2].name == "pointer_declarator":
+ fend = 2
if gStateRT.data[0].name == "type_identifier":
- offset = 1
+ fstart = 1
- pEnumCommon(ast, node, gStateRT.data[^1].val, offset, 1)
+ pEnumCommon(ast, node, gStateRT.data[0].val, fstart, fend)
+ else:
+ pEnumCommon(ast, node, gStateRT.data[^1].val, fstart, fend)
))
# typ function(typ param1, ...)
diff --git a/tests/include/test.h b/tests/include/test.h
index 12f052c..29048a7 100644
--- a/tests/include/test.h
+++ b/tests/include/test.h
@@ -115,6 +115,20 @@ void
*multiline2(void),
multiline3(void);
+// Issue #52
+typedef struct struct6 { char name; } *STRUCT6;
+typedef enum enum6t { enum16 } *ENUM6;
+typedef union union3 { char name; } *UNION3;
+
+struct struct6 test_call_stype6();
+STRUCT6 test_call_stype_ptr6();
+
+enum enum6t test_call_etype6();
+ENUM6 test_call_etype_ptr6();
+
+union union3 test_call_utype3();
+UNION3 test_call_etype_ptr3();
+
#ifdef __cplusplus
}
#endif \ No newline at end of file
diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim
index d3cb26e..f0d5ed5 100644
--- a/tests/tnimterop_c.nim
+++ b/tests/tnimterop_c.nim
@@ -115,6 +115,15 @@ multiline1()
let p = multiline2()
multiline3()
+# Issue #52
+var
+ s6: struct6
+ s6p: STRUCT6
+ e6: enum6t
+ e6p: ENUM6
+ u3: union3
+ u3p: UNION3
+
cAddStdDir()
## failing tests