From 4fffa0960f6c17e9e1e6a20665c97ac34ea678bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20M=20G=C3=B3mez?= Date: Thu, 28 Sep 2023 17:08:42 +0100 Subject: [PATCH] C++ Adds support for default arg using object construction syntax. Fixes a compiler crash (#22768) `Foo()` below makes the compiler crash. ```nim proc makeBoo(a:cint = 10, b:cstring = "hello", foo: Foo = Foo()): Boo {.importcpp, constructor.} ``` --- compiler/ccgstmts.nim | 7 ++++++- tests/cpp/tinitializers.nim | 31 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index c212ca0cbf..c8f68c124e 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -296,7 +296,12 @@ proc genCppParamsForCtor(p: BProc; call: PNode): string = assert(typ.kind == tyProc) for i in 1.. 0: result.add "," + result.add genCppInitializer(p.module, p, call[i][0].sym.typ) + else: + genOtherArg(p, call, i, typ, result, argsCounter) proc genCppVarForCtor(p: BProc; call: PNode; decl: var Rope) = let params = genCppParamsForCtor(p, call) diff --git a/tests/cpp/tinitializers.nim b/tests/cpp/tinitializers.nim index 868cf825ca..0199fb96bc 100644 --- a/tests/cpp/tinitializers.nim +++ b/tests/cpp/tinitializers.nim @@ -1,5 +1,5 @@ discard """ - targets: "cpp" + cmd: "nim cpp $file" """ {.emit:"""/*TYPESECTION*/ @@ -30,4 +30,31 @@ proc main = discard returnCppStruct() #generates result = { 10 } discard initChildStruct() #generates ChildStruct temp ({}) bypassed with makeChildStruct (proc (s:CppStruct) = discard)(CppStruct()) #CppStruct temp ({10}) -main() \ No newline at end of file +main() + + +#Should handle ObjectCalls +{.emit:"""/*TYPESECTION*/ +struct Foo { +}; +struct Boo { + Boo(int x, char* y, Foo f): x(x), y(y), foo(f){} + int x; + char* y; + Foo foo; +}; +""".} +type + Foo {.importcpp, inheritable, bycopy.} = object + Boo {.importcpp, inheritable.} = object + x: int32 + y: cstring + foo: Foo + +proc makeBoo(a:cint = 10, b:cstring = "hello", foo: Foo = Foo()): Boo {.importcpp, constructor.} + +proc main2() = + let cppStruct = makeBoo() + (proc (s:Boo) = discard)(Boo()) + +main2() \ No newline at end of file