Fix #196 - allow remapping of types

This commit is contained in:
Ganesh Viswanathan 2020-05-11 16:49:44 -05:00
commit 8d4866160e
7 changed files with 38 additions and 5 deletions

View file

@ -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]

View file

@ -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

View file

@ -28,7 +28,7 @@ yield""".split(Whitespace).toHashSet()
# Types related
const
var
gTypeMap* = {
# char
"char": "cchar",

View file

@ -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

View file

@ -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'
})

View file

@ -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

View file

@ -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)