genbindings: remove Pvoid types, replace with incomplete C pointers

This commit is contained in:
mappu 2024-08-14 17:31:10 +12:00
parent 027c059b0b
commit c7f119df80
2 changed files with 23 additions and 23 deletions

View File

@ -22,11 +22,11 @@ func emitReturnTypeCabi(p CppParameter) string {
return "void" // Will be handled separately return "void" // Will be handled separately
} else if (p.Pointer || p.ByRef) && p.QtClassType() { } else if (p.Pointer || p.ByRef) && p.QtClassType() {
return "P" + p.ParameterType // CABI type return p.ParameterType + "*" // CABI type
} else if p.QtClassType() && !p.Pointer { } else if p.QtClassType() && !p.Pointer {
// Even if C++ returns by value, CABI is returning a heap copy (new'd, not malloc'd) // Even if C++ returns by value, CABI is returning a heap copy (new'd, not malloc'd)
return "P" + p.ParameterType // CABI type return p.ParameterType + "*" // CABI type
// return "void" // Handled separately with an _out pointer // return "void" // Handled separately with an _out pointer
} else { } else {
@ -51,18 +51,18 @@ func emitParametersCabi(m CppMethod, selfType string) string {
// The Go code has called this with two arguments: T* and len // The Go code has called this with two arguments: T* and len
// Declare that we take two parameters // Declare that we take two parameters
// TODO support QList<int> // TODO support QList<int>
tmp = append(tmp, "P"+t.ParameterType+" "+p.ParameterName+", size_t "+p.ParameterName+"_len") tmp = append(tmp, t.ParameterType+"* "+p.ParameterName+", size_t "+p.ParameterName+"_len")
} else if (p.ByRef || p.Pointer) && p.QtClassType() { } else if (p.ByRef || p.Pointer) && p.QtClassType() {
// Pointer to Qt type // Pointer to Qt type
// Replace with taking our PQ typedef by value // Replace with taking our PQ typedef by value
tmp = append(tmp, "P"+p.ParameterType+" "+p.ParameterName) tmp = append(tmp, p.ParameterType+"* "+p.ParameterName)
} else if p.QtClassType() { } else if p.QtClassType() {
// Qt type passed by value // Qt type passed by value
// The CABI will unconditionally take these by pointer and dereference them // The CABI will unconditionally take these by pointer and dereference them
// when passing to C++ // when passing to C++
tmp = append(tmp, "P"+p.ParameterType+" "+p.ParameterName) tmp = append(tmp, p.ParameterType+"* "+p.ParameterName)
} else { } else {
// RenderTypeCpp renders both pointer+reference as pointers // RenderTypeCpp renders both pointer+reference as pointers
@ -79,7 +79,7 @@ func emitParametersCabi(m CppMethod, selfType string) string {
tmp = append(tmp, "char** _out, size_t* _out_Strlen") tmp = append(tmp, "char** _out, size_t* _out_Strlen")
} else if t, ok := m.ReturnType.QListOf(); ok { } else if t, ok := m.ReturnType.QListOf(); ok {
tmp = append(tmp, "P"+t.ParameterType+"* _out, size_t* _out_len") tmp = append(tmp, t.ParameterType+"** _out, size_t* _out_len")
} }
@ -102,7 +102,7 @@ func emitParametersCABI2CppForwarding(params []CppParameter) (preamble string, f
preamble += "\t" + p.RenderTypeCpp() + " " + p.ParameterName + "_QList;\n" preamble += "\t" + p.RenderTypeCpp() + " " + p.ParameterName + "_QList;\n"
preamble += "\t" + p.ParameterName + "_QList.reserve(" + p.ParameterName + "_len);\n" preamble += "\t" + p.ParameterName + "_QList.reserve(" + p.ParameterName + "_len);\n"
preamble += "\tfor(size_t i = 0; i < " + p.ParameterName + "_len; ++i) {\n" preamble += "\tfor(size_t i = 0; i < " + p.ParameterName + "_len; ++i) {\n"
preamble += "\t\t" + p.ParameterName + "_QList.push_back(" + p.ParameterName + "[i]);\n" preamble += "\t\t" + p.ParameterName + "_QList.push_back(" + p.ParameterName + "++);\n"
preamble += "\t}\n" preamble += "\t}\n"
tmp = append(tmp, p.ParameterName+"_QList") tmp = append(tmp, p.ParameterName+"_QList")
@ -110,17 +110,17 @@ func emitParametersCABI2CppForwarding(params []CppParameter) (preamble string, f
// We changed RenderTypeCpp() to render this as a pointer // We changed RenderTypeCpp() to render this as a pointer
// Need to dereference so we can pass as reference to the actual Qt C++ function // Need to dereference so we can pass as reference to the actual Qt C++ function
//tmp = append(tmp, "*"+p.ParameterName) //tmp = append(tmp, "*"+p.ParameterName)
tmp = append(tmp, "*static_cast<"+p.ParameterType+"*>("+p.ParameterName+")") tmp = append(tmp, "*"+p.ParameterName)
} else if p.QtClassType() && !p.Pointer { } else if p.QtClassType() && !p.Pointer {
// CABI takes all Qt types by pointer, even if C++ wants them by value // CABI takes all Qt types by pointer, even if C++ wants them by value
// Dereference the passed-in pointer // Dereference the passed-in pointer
tmp = append(tmp, "*static_cast<"+p.ParameterType+"*>("+p.ParameterName+")") tmp = append(tmp, "*"+p.ParameterName)
} else if p.QtClassType() && p.Pointer { // } else if p.QtClassType() && p.Pointer {
// We need this static_cast<> anyway to convert from PQt (void*) to // We need this static_cast<> anyway to convert from PQt (void*) to
// the real Qt type // the real Qt type
tmp = append(tmp, "static_cast<"+p.ParameterType+"*>("+p.ParameterName+")") // tmp = append(tmp, "static_cast<"+p.ParameterType+"*>("+p.ParameterName+")")
} else { } else {
tmp = append(tmp, p.ParameterName) tmp = append(tmp, p.ParameterName)
@ -191,15 +191,15 @@ extern "C" {
for _, c := range src.Classes { for _, c := range src.Classes {
for i, ctor := range c.Ctors { for i, ctor := range c.Ctors {
ret.WriteString(fmt.Sprintf("P%s %s_new%s(%s);\n", c.ClassName, c.ClassName, maybeSuffix(i), emitParametersCabi(ctor, ""))) ret.WriteString(fmt.Sprintf("%s %s_new%s(%s);\n", c.ClassName+"*", c.ClassName, maybeSuffix(i), emitParametersCabi(ctor, "")))
} }
for _, m := range c.Methods { for _, m := range c.Methods {
ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, "P"+c.ClassName))) ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, c.ClassName+"*")))
} }
// delete // delete
ret.WriteString(fmt.Sprintf("void %s_Delete(P%s self);\n", c.ClassName, c.ClassName)) ret.WriteString(fmt.Sprintf("void %s_Delete(%s* self);\n", c.ClassName, c.ClassName))
ret.WriteString("\n") ret.WriteString("\n")
} }
@ -227,7 +227,7 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
for i, ctor := range c.Ctors { for i, ctor := range c.Ctors {
preamble, forwarding := emitParametersCABI2CppForwarding(ctor.Parameters) preamble, forwarding := emitParametersCABI2CppForwarding(ctor.Parameters)
ret.WriteString(fmt.Sprintf( ret.WriteString(fmt.Sprintf(
"P%s %s_new%s(%s) {\n"+ "%s* %s_new%s(%s) {\n"+
"%s"+ "%s"+
"\treturn new %s(%s);\n"+ "\treturn new %s(%s);\n"+
"}\n"+ "}\n"+
@ -269,7 +269,7 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
// afterCall = "\t// Copy-construct value returned type into Go-allocated POCO\n" // afterCall = "\t// Copy-construct value returned type into Go-allocated POCO\n"
// afterCall += "\t_*out = ret;\n" // afterCall += "\t_*out = ret;\n"
afterCall = "\t// Copy-construct value returned type into heap-allocated copy\n" afterCall = "\t// Copy-construct value returned type into heap-allocated copy\n"
afterCall += "\treturn static_cast<P" + m.ReturnType.ParameterType + ">(new " + m.ReturnType.ParameterType + "(ret));\n" afterCall += "\treturn static_cast<" + m.ReturnType.ParameterType + "*>(new " + m.ReturnType.ParameterType + "(ret));\n"
} else if m.ReturnType.Const { } else if m.ReturnType.Const {
shouldReturn += "(" + emitReturnTypeCabi(m.ReturnType) + ") " shouldReturn += "(" + emitReturnTypeCabi(m.ReturnType) + ") "
@ -289,7 +289,7 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
"%s"+ "%s"+
"}\n"+ "}\n"+
"\n", "\n",
emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, "P"+c.ClassName), emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, c.ClassName+"*"),
preamble, preamble,
shouldReturn, c.ClassName, nativeMethodName, forwarding, shouldReturn, c.ClassName, nativeMethodName, forwarding,
afterCall, afterCall,
@ -298,7 +298,7 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
// Delete // Delete
ret.WriteString(fmt.Sprintf( ret.WriteString(fmt.Sprintf(
"void %s_Delete(P%s self) {\n"+ "void %s_Delete(%s* self) {\n"+
"\tdelete static_cast<%s*>(self);\n"+ "\tdelete static_cast<%s*>(self);\n"+
"}\n"+ "}\n"+
"\n", "\n",

View File

@ -59,7 +59,7 @@ func emitParametersGo2CABIForwarding(m CppMethod) (preamble string, fowarding st
// TODO handle QList<int> // TODO handle QList<int>
preamble += "// For the C ABI, malloc a C array of raw pointers\n" preamble += "// For the C ABI, malloc a C array of raw pointers\n"
preamble += p.ParameterName + "_CArray := (*[0xffff]C.P" + listType.ParameterType + ")(C.malloc(c.ulong(8 * len(" + p.ParameterName + "))))\n" preamble += p.ParameterName + "_CArray := (*[0xffff]*C." + listType.ParameterType + ")(C.malloc(c.ulong(8 * len(" + p.ParameterName + "))))\n"
preamble += "defer C.free(" + p.ParameterName + "_CArray)\n" preamble += "defer C.free(" + p.ParameterName + "_CArray)\n"
preamble += "for i := range " + p.ParameterName + "{\n" preamble += "for i := range " + p.ParameterName + "{\n"
preamble += p.ParameterName + "_CArray[i] = " + p.ParameterName + "[i].cPointer()\n" preamble += p.ParameterName + "_CArray[i] = " + p.ParameterName + "[i].cPointer()\n"
@ -117,7 +117,7 @@ import "C"
ret.WriteString(` ret.WriteString(`
type ` + c.ClassName + ` struct { type ` + c.ClassName + ` struct {
h C.P` + c.ClassName + ` h *C.` + c.ClassName + `
`) `)
// Embed all inherited types to directly allow calling inherited methods // Embed all inherited types to directly allow calling inherited methods
@ -128,7 +128,7 @@ import "C"
ret.WriteString(` ret.WriteString(`
} }
func (this *` + c.ClassName + `) cPointer() C.P` + c.ClassName + ` { func (this *` + c.ClassName + `) cPointer() *C.` + c.ClassName + ` {
if this == nil { if this == nil {
return nil return nil
} }
@ -143,7 +143,7 @@ import "C"
} }
ret.WriteString(` ret.WriteString(`
func new` + c.ClassName + `(h C.P` + c.ClassName + `) { func new` + c.ClassName + `(h *C.` + c.ClassName + `) {
return &` + c.ClassName + `{` + localInit + `} return &` + c.ClassName + `{` + localInit + `}
} }
@ -189,7 +189,7 @@ import "C"
shouldReturn = "" shouldReturn = ""
returnTypeDecl = "[]" + t.RenderTypeGo() returnTypeDecl = "[]" + t.RenderTypeGo()
preamble += "var _out *C.P" + t.ParameterType + " = nil\n" preamble += "var _out **C." + t.ParameterType + " = nil\n"
preamble += "var _out_len C.size_t = 0\n" preamble += "var _out_len C.size_t = 0\n"
afterword += "ret := make([]" + t.RenderTypeGo() + ", _out_Strlen)\n" afterword += "ret := make([]" + t.RenderTypeGo() + ", _out_Strlen)\n"
afterword += "for i := 0; i < _out_len; i++ {\n" afterword += "for i := 0; i < _out_len; i++ {\n"