aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2020-05-11 16:49:44 -0500
committerGanesh Viswanathan <dev@genotrance.com>2020-05-11 22:56:46 -0500
commit8d4866160ef91084ea13b526694e56ce9c805dd2 (patch)
tree0ac2ee190451049da23be18f89ce5cab682dd3cf
parentecb5f2ede6bbc793c81ce3bc0f1ea973deb95d2a (diff)
downloadnimterop-issue196-2.tar.gz
nimterop-issue196-2.zip
Fix #196 - allow remapping of typesissue196-2
-rw-r--r--CHANGES.md2
-rw-r--r--README.md1
-rw-r--r--nimterop/getters.nim2
-rw-r--r--nimterop/globals.nim2
-rw-r--r--nimterop/toast.nim17
-rw-r--r--tests/include/tast2.h12
-rw-r--r--tests/tast2.nim7
7 files changed, 38 insertions, 5 deletions
diff --git a/CHANGES.md b/CHANGES.md
index ef66c79..01080a6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -56,6 +56,8 @@ https://github.com/nimterop/nimterop/compare/v0.4.4...v0.5.0
- `toast` now includes `--replace | -G` to manipulate identifier names beyond `--prefix` and `--suffix`. `-G:X=Y` replaces X with Y and `-G:@_[_]+=_` replaces multiple `_` with a single instance using the `@` prefix to enable regular expressions.
+- `toast` also includes `--typeMap | -T` to map C types to another type. E.g. `--typeMap:GLint64=int64` generates a wrapper where all instances of `GLint64` are remapped to the Nim type `int64` and `GLint64` is not defined. (since v0.5.2)
+
- Nimterop is now able to detect Nim configuration of projects and can better handle cases where defaults such as `nimcacheDir` or `nimblePath` are overridden. This especially enables better interop with workflows that do not depend on Nimble. [#151][i151] [#153][i153]
- Nimterop defaults to `cmake`, followed by `autoconf` for building libraries with `getHeader()`. It is now possible to change the order of discovery with the `buildType` value. [#200][i200]
diff --git a/README.md b/README.md
index 869ac4e..11952e8 100644
--- a/README.md
+++ b/README.md
@@ -229,6 +229,7 @@ Options:
-s, --stub bool false stub out undefined type references as objects
-F=, --suffix= strings {} strip suffix from identifiers
-O=, --symOverride= strings {} skip generating specified symbols
+ -T=, --typeMap= strings {} map instances of type X to Y - e.g. ABC=cint
```
## Why nimterop
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index f834370..e600b18 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -28,7 +28,7 @@ yield""".split(Whitespace).toHashSet()
# Types related
-const
+var
gTypeMap* = {
# char
"char": "cchar",
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index 05f507c..46203ea 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -76,6 +76,8 @@ type
# `--replace | -G` replacement rules for identifiers
suffix*: seq[string] # `--suffix` strings to strip from end of identifiers
symOverride*: seq[string] # `cSkipSymbol()`, `cOverride()` and `--symOverride | -O` symbols to skip during wrapping
+ typeMap*: TableRef[string, string]
+ # `--typeMap | -T` to map instances of type X to Y - e.g. ABC=cint
# cimport.nim specific
compile*: seq[string] # `cCompile()` list of files already processed
diff --git a/nimterop/toast.nim b/nimterop/toast.nim
index a4fe6fa..3a58749 100644
--- a/nimterop/toast.nim
+++ b/nimterop/toast.nim
@@ -1,4 +1,4 @@
-import os, osproc, strformat, strutils, tables, times
+import os, osproc, sets, strformat, strutils, tables, times
import "."/treesitter/[api, c, cpp]
@@ -51,6 +51,7 @@ proc main(
stub = false,
suffix: seq[string] = @[],
symOverride: seq[string] = @[],
+ typeMap: seq[string] = @[],
source: seq[string]
) =
@@ -93,6 +94,14 @@ proc main(
value = if nv.len == 2: nv[1] else: ""
gState.replace[name] = value
+ # typeMap => getters.gTypeMap
+ for i in typeMap.getSplitComma():
+ let
+ nv = i.split("=", maxsplit = 1)
+ doAssert nv.len == 2, "`--typeMap` requires X=Y format"
+ gTypeMap[nv[0]] = nv[1]
+ gTypeMapValues.incl nv[1]
+
if pluginSourcePath.nBl:
gState.loadPlugin(pluginSourcePath)
@@ -217,7 +226,8 @@ when isMainModule:
"source" : "C/C++ source/header",
"stub": "stub out undefined type references as objects",
"suffix": "strip suffix from identifiers",
- "symOverride": "skip generating specified symbols"
+ "symOverride": "skip generating specified symbols",
+ "typeMap": "map instances of type X to Y - e.g. ABC=cint"
}, short = {
"check": 'k',
"convention": 'C',
@@ -238,5 +248,6 @@ when isMainModule:
"replace": 'G',
"stub": 's',
"suffix": 'F',
- "symOverride": 'O'
+ "symOverride": 'O',
+ "typeMap": 'T'
})
diff --git a/tests/include/tast2.h b/tests/include/tast2.h
index 878a3a2..bbb3315 100644
--- a/tests/include/tast2.h
+++ b/tests/include/tast2.h
@@ -243,6 +243,12 @@ static inline int sitest1(int f1) {
return f1 * 2;
}
+// Issue #196
+typedef int MyInt;
+struct TestMyInt {
+ MyInt f1;
+};
+
// DUPLICATES
@@ -439,6 +445,12 @@ static inline int sitest1(int f1) {
return f1 * 2;
}
+// Issue #196
+typedef int MyInt;
+struct TestMyInt {
+ MyInt f1;
+};
+
#endif
diff --git a/tests/tast2.nim b/tests/tast2.nim
index bd040e2..4fd16cc 100644
--- a/tests/tast2.nim
+++ b/tests/tast2.nim
@@ -35,7 +35,7 @@ cOverride:
type
A1* = A0
-cImport(path, flags="-f:ast2 -ENK_,SDL_ -GVICE=SLICE" & flags)
+cImport(path, flags="-f:ast2 -ENK_,SDL_ -GVICE=SLICE -TMyInt=cint" & flags)
proc getPragmas(n: NimNode): HashSet[string] =
# Find all pragmas in AST, return as "name" or "name:value" in set
@@ -478,3 +478,8 @@ checkPragmas(nested, pHeaderImpBy)
when not defined(NOHEADER):
assert sitest1(5) == 10
assert sitest1(10) == 20
+
+when declared(MyInt):
+ assert false, "MyInt is defined!"
+testFields(TestMyInt, "f1!cint")
+checkPragmas(TestMyInt, pHeaderBy, isType = false) \ No newline at end of file