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 1d4f014
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
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
2 changes: 1 addition & 1 deletion tests/cpp/tcovariancerules.nim
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ reject wantsVarPointer2(pcat)

# covariance may be allowed for certain extern types

{.emit: """
{.emit: """/*TYPESECTION*/
template <class T> struct FN { typedef void (*type)(T); };
template <class T> struct ARR { typedef T DataType[2]; DataType data; };
""".}
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/tvector_iterator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ discard """
targets: "cpp"
"""

{.emit: """
{.emit: """/*TYPESECTION*/
template <class T>
struct Vector {
Expand Down
10 changes: 8 additions & 2 deletions tests/statictypes/tstaticimportcpp.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
discard """
targets: "cpp"
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest"
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]"
"""

{.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 1d4f014

Please sign in to comment.