Skip to content

Commit

Permalink
Support using imported C++ generic types in proc signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Mar 15, 2018
1 parent bf75b41 commit 06fd219
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,13 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
# always call for sideeffects:
assert t.kind != tyTuple
discard getRecordDesc(m, t, result, check)
# The resulting type will include commas and these won't play well
# with the C macros for defining procs such as N_NIMCALL. We must
# create a typedef for the type and use it in the proc signature:
let typedefName = ~"TY" & $sig
addf(m.s[cfsTypes], "typedef $1 $2;$n", [result, typedefName])
m.typeCache[sig] = typedefName
result = typedefName
else:
when false:
if t.sym != nil and t.sym.name.s == "KeyValuePair":
Expand Down
8 changes: 7 additions & 1 deletion tests/statictypes/tstaticimportcpp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ targets: "cpp"
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest"
"""

{.emit: """
{.emit: """/*TYPESECTION*/
template <int N, class T>
struct GenericIntType {
Expand Down Expand Up @@ -51,3 +51,9 @@ echo c.field
echo d.field
echo e.field

proc plus(a, b: GenInt4): GenInt4 =
for i in 0 ..< result.data.len:
result.data[i] = a.data[i] + b.data[i]

echo plus(a, a).data

0 comments on commit 06fd219

Please sign in to comment.