genbindings/go: implement cabi type roundtrips for pointer types

This commit is contained in:
mappu 2024-11-15 15:09:29 +13:00
parent 5a3c3556a1
commit caee076af8
2 changed files with 20 additions and 0 deletions

View File

@ -478,6 +478,13 @@ func emitAssignCppToCabi(assignExpression string, p CppParameter, rvalue string)
} else if p.Const { } else if p.Const {
shouldReturn += "(" + p.RenderTypeCabi() + ") " shouldReturn += "(" + p.RenderTypeCabi() + ") "
} else {
// Basic type
if p.ByRef {
// The C++ type is a reference, the CABI type is a pointer type
shouldReturn += "&"
}
} }
return indent + shouldReturn + rvalue + ";\n" + afterCall return indent + shouldReturn + rvalue + ";\n" + afterCall

View File

@ -169,6 +169,11 @@ func (p CppParameter) parameterTypeCgo() string {
return "C.struct_miqt_map" return "C.struct_miqt_map"
} }
// Cgo internally binds void* as unsafe.Pointer{}
if p.ParameterType == "void" && p.Pointer {
return "unsafe.Pointer"
}
tmp := strings.Replace(p.RenderTypeCabi(), `*`, "", -1) tmp := strings.Replace(p.RenderTypeCabi(), `*`, "", -1)
if strings.HasPrefix(tmp, "const ") && tmp != "const char" { // Special typedef to make this work for const char* signal parameters if strings.HasPrefix(tmp, "const ") && tmp != "const char" { // Special typedef to make this work for const char* signal parameters
@ -542,6 +547,14 @@ func (gfs *goFileState) emitCabiToGo(assignExpr string, rt CppParameter, rvalue
return shouldReturn + " " + rvalue + "\n" + afterword return shouldReturn + " " + rvalue + "\n" + afterword
} else if rt.IntType() || rt.IsKnownEnum() || rt.IsFlagType() || rt.ParameterType == "bool" || rt.QtCppOriginalType != nil { } else if rt.IntType() || rt.IsKnownEnum() || rt.IsFlagType() || rt.ParameterType == "bool" || rt.QtCppOriginalType != nil {
if rt.Pointer || rt.ByRef {
// Cast must go via unsafe.Pointer
gfs.imports["unsafe"] = struct{}{}
return assignExpr + "(" + rt.RenderTypeGo(gfs) + ")(unsafe.Pointer(" + rvalue + "))\n"
}
// Need to cast Cgo type to Go int type // Need to cast Cgo type to Go int type
// Optimize assignment to avoid temporary // Optimize assignment to avoid temporary
return assignExpr + "(" + rt.RenderTypeGo(gfs) + ")(" + rvalue + ")\n" return assignExpr + "(" + rt.RenderTypeGo(gfs) + ")(" + rvalue + ")\n"