mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 08:54:53 +00:00
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.}
```
This commit is contained in:
@@ -296,7 +296,12 @@ proc genCppParamsForCtor(p: BProc; call: PNode): string =
|
||||
assert(typ.kind == tyProc)
|
||||
for i in 1..<call.len:
|
||||
assert(typ.len == typ.n.len)
|
||||
genOtherArg(p, call, i, typ, result, argsCounter)
|
||||
#if it's a type we can just generate here another initializer as we are in an initializer context
|
||||
if call[i].kind == nkCall and call[i][0].kind == nkSym and call[i][0].sym.kind == skType:
|
||||
if argsCounter > 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)
|
||||
|
||||
@@ -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()
|
||||
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()
|
||||
Reference in New Issue
Block a user