Fix template codegen with static parameters (#8110)
* Fix template codegen with static parameters Fixes #7477 * Move the test case to tstaticimportcpp
This commit is contained in:
parent
e66b7f3909
commit
cecce80d42
2 changed files with 17 additions and 9 deletions
|
|
@ -734,6 +734,16 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
|
||||||
let cppName = getTypeName(m, t, sig)
|
let cppName = getTypeName(m, t, sig)
|
||||||
var i = 0
|
var i = 0
|
||||||
var chunkStart = 0
|
var chunkStart = 0
|
||||||
|
|
||||||
|
template addResultType(ty: untyped) =
|
||||||
|
if ty == nil or ty.kind == tyVoid:
|
||||||
|
result.add(~"void")
|
||||||
|
elif ty.kind == tyStatic:
|
||||||
|
internalAssert m.config, ty.n != nil
|
||||||
|
result.add ty.n.renderTree
|
||||||
|
else:
|
||||||
|
result.add getTypeDescAux(m, ty, check)
|
||||||
|
|
||||||
while i < cppName.data.len:
|
while i < cppName.data.len:
|
||||||
if cppName.data[i] == '\'':
|
if cppName.data[i] == '\'':
|
||||||
var chunkEnd = i-1
|
var chunkEnd = i-1
|
||||||
|
|
@ -743,13 +753,7 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
|
||||||
chunkStart = i
|
chunkStart = i
|
||||||
|
|
||||||
let typeInSlot = resolveStarsInCppType(origTyp, idx + 1, stars)
|
let typeInSlot = resolveStarsInCppType(origTyp, idx + 1, stars)
|
||||||
if typeInSlot == nil or typeInSlot.kind == tyVoid:
|
addResultType(typeInSlot)
|
||||||
result.add(~"void")
|
|
||||||
elif typeInSlot.kind == tyStatic:
|
|
||||||
internalAssert m.config, typeInSlot.n != nil
|
|
||||||
result.add typeInSlot.n.renderTree
|
|
||||||
else:
|
|
||||||
result.add getTypeDescAux(m, typeInSlot, check)
|
|
||||||
else:
|
else:
|
||||||
inc i
|
inc i
|
||||||
|
|
||||||
|
|
@ -759,7 +763,7 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
|
||||||
result = cppName & "<"
|
result = cppName & "<"
|
||||||
for i in 1 .. origTyp.len-2:
|
for i in 1 .. origTyp.len-2:
|
||||||
if i > 1: result.add(" COMMA ")
|
if i > 1: result.add(" COMMA ")
|
||||||
result.add(getTypeDescAux(m, origTyp.sons[i], check))
|
addResultType(origTyp.sons[i])
|
||||||
result.add("> ")
|
result.add("> ")
|
||||||
# always call for sideeffects:
|
# always call for sideeffects:
|
||||||
assert t.kind != tyTuple
|
assert t.kind != tyTuple
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
discard """
|
discard """
|
||||||
targets: "cpp"
|
targets: "cpp"
|
||||||
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]"
|
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]\n4"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
{.emit: """/*TYPESECTION*/
|
{.emit: """/*TYPESECTION*/
|
||||||
|
|
@ -26,6 +26,8 @@ type
|
||||||
GenericIntType {.importcpp: "GenericIntType<'0, '1>".} [N: static[int]; T] = object
|
GenericIntType {.importcpp: "GenericIntType<'0, '1>".} [N: static[int]; T] = object
|
||||||
data: array[N, T]
|
data: array[N, T]
|
||||||
|
|
||||||
|
GenericIntTypeAlt {.importcpp: "GenericIntType".} [N: static[int]; T] = object
|
||||||
|
|
||||||
GenericTType {.importcpp: "GenericTType<'0>".} [T] = object
|
GenericTType {.importcpp: "GenericTType<'0>".} [T] = object
|
||||||
field: T
|
field: T
|
||||||
|
|
||||||
|
|
@ -40,6 +42,7 @@ var
|
||||||
c = GenericTType[float]()
|
c = GenericTType[float]()
|
||||||
d = SimpleStruct(field: 15)
|
d = SimpleStruct(field: 15)
|
||||||
e = GenericTType[string](field: "test")
|
e = GenericTType[string](field: "test")
|
||||||
|
f = GenericIntTypeAlt[4, int8]()
|
||||||
|
|
||||||
a.data[2] = 10
|
a.data[2] = 10
|
||||||
b.field = 5
|
b.field = 5
|
||||||
|
|
@ -57,3 +60,4 @@ proc plus(a, b: GenInt4): GenInt4 =
|
||||||
|
|
||||||
echo plus(a, a).data
|
echo plus(a, a).data
|
||||||
|
|
||||||
|
echo sizeof(f)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue