aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-05-07 11:44:32 -0500
committerGanesh Viswanathan <dev@genotrance.com>2019-05-07 11:44:32 -0500
commit2457671f9eef2c90fd5f403fbcde97c69c5aa64b (patch)
tree21bbc0535aa78cf2eb3ccb1ef71352dea47eb45c
parent69cfce561a2114616e57c64effc2f6d36db1967d (diff)
downloadnimterop-2457671f9eef2c90fd5f403fbcde97c69c5aa64b.tar.gz
nimterop-2457671f9eef2c90fd5f403fbcde97c69c5aa64b.zip
Pragma cleanup and fixes
-rw-r--r--nimterop/ast.nim1
-rw-r--r--nimterop/grammar.nim50
-rw-r--r--tests/include/test.h1
-rw-r--r--tests/tnimterop_c.nim2
4 files changed, 38 insertions, 16 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index c66d504..c40e8c8 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -142,7 +142,6 @@ proc printNimHeader*() =
# Command line:
# $2 $3
-{.experimental: "codeReordering".}
{.hint[ConvFromXtoItselfNotNeeded]: off.}
import nimterop/types
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index 2a8b330..daf5f15 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -7,10 +7,19 @@ import "."/[getters, globals, lisp, treesitter/api]
type
Grammar = seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode, nimState: NimState) {.nimcall.}]]
-proc genImportC(origName, nimName: string): string =
+proc genImportC(nimState: NimState, origName, nimName: string): string =
result = "importc"
if nimName != origName:
result.add &": \"{origName}\"" # used as {.importc: "foo".}
+ result.add &", header: {nimState.currentHeader}"
+
+proc genPragma(pragmas: varargs[string]): string =
+ result = ""
+ for pragma in pragmas.items():
+ if pragma.len != 0:
+ result &= pragma & ", "
+ if result.len != 0:
+ result = " {." & result[0 .. ^2] & ".}"
proc initGrammar(): Grammar =
# #define X Y
@@ -141,6 +150,7 @@ proc initGrammar(): Grammar =
i = 0
typ = nimState.data[i].val.getIdentifier(nskType)
name = ""
+ nname = ""
tptr = ""
aptr = ""
@@ -155,13 +165,17 @@ proc initGrammar(): Grammar =
i += 1
if i < nimState.data.len:
- name = nimState.data[i].val.getIdentifier(nskType)
+ name = nimState.data[i].val
+ nname = nimState.data[i].val.getIdentifier(nskType)
i += 1
- if typ.nBl and name.nBl and nimState.identifiers.addNewIdentifer(name):
+ let
+ pragma = genPragma(nimState.genImportC(name, nname))
+
+ if typ.nBl and nname.nBl and nimState.identifiers.addNewIdentifer(nname):
if i < nimState.data.len and nimState.data[^1].name == "function_declarator":
var
- fname = name
+ fname = nname
pout, pname, ptyp, pptr = ""
count = 1
@@ -175,9 +189,9 @@ proc initGrammar(): Grammar =
pout = pout[0 .. ^3]
if tptr.len != 0 or typ != "object":
- nimState.typeStr &= &"\n {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}"
+ nimState.typeStr &= &"\n {nname}*{pragma} = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}"
else:
- nimState.typeStr &= &"\n {name}* = proc({pout}) {{.nimcall.}}"
+ nimState.typeStr &= &"\n {nname}*{pragma} = proc({pout}) {{.nimcall.}}"
else:
if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
var
@@ -185,12 +199,12 @@ proc initGrammar(): Grammar =
if nimState.data[i].name == "identifier":
flen = flen.getIdentifier(nskConst, name)
- nimState.typeStr &= &"\n {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
+ nimState.typeStr &= &"\n {nname}*{pragma} = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
else:
if name == typ:
- nimState.typeStr &= &"\n {name}* = object"
+ nimState.typeStr &= &"\n {nname}*{pragma} = object"
else:
- nimState.typeStr &= &"\n {name}* = {getPtrType(tptr&typ)}"
+ nimState.typeStr &= &"\n {nname}*{pragma} = {getPtrType(tptr&typ)}"
))
proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) =
@@ -212,8 +226,10 @@ proc initGrammar(): Grammar =
nimState.enumStr &= &"\ntype {ndname}* = {dptr}{nname}"
else:
if nimState.identifiers.addNewIdentifer(ndname):
+ let
+ pragma = genPragma(nimState.genImportc(dname, ndname), "bycopy")
nimState.typeStr &=
- &"\n {ndname}* {{.{genImportC(dname, ndname)}, header: {nimState.currentHeader}, bycopy.}} = {dptr}{nname}"
+ &"\n {ndname}*{pragma} = {dptr}{nname}"
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) =
if nimState.debug:
@@ -250,8 +266,9 @@ proc initGrammar(): Grammar =
if nimState.data.len == 1:
nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}"
else:
- let importCDecl = genImportC(prefix & name, nname)
- nimState.typeStr &= &"\n {nname}* {{.{importCDecl}, header: {nimState.currentHeader}, bycopy.}} = object{union}"
+ let
+ pragma = genPragma(nimState.genImportC(prefix & name, nname), "bycopy")
+ nimState.typeStr &= &"\n {nname}*{pragma} = object{union}"
var
i = fstart
@@ -560,11 +577,14 @@ proc initGrammar(): Grammar =
pout = pout[0 .. ^3]
if fnname.nBl and nimState.identifiers.addNewIdentifer(fnname):
- let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname)
+ let
+ ftyp = nimState.data[0].val.getIdentifier(nskType, fnname)
+ pragma = genPragma(nimState.genImportC(fname, fnname))
+
if fptr.len != 0 or ftyp != "object":
- nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.{genImportC(fname, fnname)}, header: {nimState.currentHeader}.}}"
+ nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
else:
- nimState.procStr &= &"\nproc {fnname}*({pout}) {{.{genImportC(fname, fnname)}, header: {nimState.currentHeader}.}}"
+ nimState.procStr &= &"\nproc {fnname}*({pout}){pragma}"
))
proc initRegex(ast: ref Ast) =
diff --git a/tests/include/test.h b/tests/include/test.h
index 8138e54..2e4cf08 100644
--- a/tests/include/test.h
+++ b/tests/include/test.h
@@ -27,6 +27,7 @@ extern "C" {
typedef uint8_t PRIMTYPE;
typedef PRIMTYPE CUSTTYPE;
+typedef CUSTTYPE _CCUSTTYPE_;
struct STRUCT0;
diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim
index 01fa70b..0eafa11 100644
--- a/tests/tnimterop_c.nim
+++ b/tests/tnimterop_c.nim
@@ -40,6 +40,7 @@ block:
var
pt: PRIMTYPE
ct: CUSTTYPE
+ cct: CCUSTTYPE
s0: STRUCT0
s1: STRUCT1
@@ -64,6 +65,7 @@ var
pt = 3
ct = 4
+cct = 5
s1.field1 = 5
s2.field1 = 6