mirror of
https://github.com/mappu/miqt.git
synced 2024-12-23 01:18:37 +00:00
genbindings: handle QString return types
This commit is contained in:
parent
6eed71723e
commit
9264bc6cc6
@ -14,14 +14,26 @@ func (p CppParameter) RenderTypeCpp() string {
|
|||||||
return ret // ignore const
|
return ret // ignore const
|
||||||
}
|
}
|
||||||
|
|
||||||
func emitParametersCpp(params []CppParameter, selfType string) string {
|
func emitReturnTypeCabi(p CppParameter) string {
|
||||||
tmp := make([]string, 0, len(params)+1)
|
if p.ParameterType == "QString" {
|
||||||
|
return "void" // Will be handled separately
|
||||||
|
|
||||||
|
} else if (p.Pointer || p.ByRef) && p.QtClassType() {
|
||||||
|
return "P" + p.ParameterType // CABI type
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return p.RenderTypeCpp()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func emitParametersCabi(m CppMethod, selfType string) string {
|
||||||
|
tmp := make([]string, 0, len(m.Parameters)+1)
|
||||||
|
|
||||||
if selfType != "" {
|
if selfType != "" {
|
||||||
tmp = append(tmp, selfType+" self")
|
tmp = append(tmp, selfType+" self")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range params {
|
for _, p := range m.Parameters {
|
||||||
if p.ParameterType == "QString" {
|
if p.ParameterType == "QString" {
|
||||||
// The Go code has called this with two arguments: char* and len
|
// The Go code has called this with two arguments: char* and len
|
||||||
// Declare that we take two parameters
|
// Declare that we take two parameters
|
||||||
@ -36,6 +48,16 @@ func emitParametersCpp(params []CppParameter, selfType string) string {
|
|||||||
tmp = append(tmp, p.RenderTypeCpp()+" "+p.ParameterName)
|
tmp = append(tmp, p.RenderTypeCpp()+" "+p.ParameterName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the return type is QString, we need to handle returns via extra CABI
|
||||||
|
// parameters
|
||||||
|
// Qt C++: memory is in QString RAII
|
||||||
|
// CABI: memory is moved into C.malloc/C.free
|
||||||
|
// Go: converted to native Go string
|
||||||
|
if m.ReturnType.ParameterType == "QString" {
|
||||||
|
tmp = append(tmp, "char** _out, size_t* _out_Strlen")
|
||||||
|
}
|
||||||
|
|
||||||
return strings.Join(tmp, ", ")
|
return strings.Join(tmp, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,19 +140,18 @@ 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, maybeSuffix(i), emitParametersCpp(ctor.Parameters, "")))
|
ret.WriteString(fmt.Sprintf("P%s %s_new%s(%s);\n", 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", m.ReturnType.RenderTypeCpp(), c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName)))
|
ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, "P"+c.ClassName)))
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.WriteString("\n")
|
ret.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.WriteString(`
|
ret.WriteString(
|
||||||
|
`#ifdef __cplusplus
|
||||||
#ifdef __cplusplus
|
|
||||||
} /* extern C */
|
} /* extern C */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -157,7 +178,7 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
|
|||||||
"\treturn new %s(%s);\n"+
|
"\treturn new %s(%s);\n"+
|
||||||
"}\n"+
|
"}\n"+
|
||||||
"\n",
|
"\n",
|
||||||
c.ClassName, maybeSuffix(i), emitParametersCpp(ctor.Parameters, ""),
|
c.ClassName, maybeSuffix(i), emitParametersCabi(ctor, ""),
|
||||||
preamble,
|
preamble,
|
||||||
c.ClassName, forwarding,
|
c.ClassName, forwarding,
|
||||||
))
|
))
|
||||||
@ -167,8 +188,18 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
|
|||||||
// Need to take an extra 'self' parameter
|
// Need to take an extra 'self' parameter
|
||||||
|
|
||||||
shouldReturn := "return "
|
shouldReturn := "return "
|
||||||
|
afterCall := ""
|
||||||
|
|
||||||
if m.ReturnType.ParameterType == "void" {
|
if m.ReturnType.ParameterType == "void" {
|
||||||
shouldReturn = ""
|
shouldReturn = ""
|
||||||
|
|
||||||
|
} else if m.ReturnType.ParameterType == "QString" {
|
||||||
|
shouldReturn = "QString ret = "
|
||||||
|
afterCall = "\t// Convert QString from UTF-16 in C++ RAII memory to UTF-8 in manually-managed C memory\n"
|
||||||
|
afterCall += "\tQByteArray b = ret.toUtf8();\n"
|
||||||
|
afterCall += "\t*_out = static_cast<char*>(malloc(b.length()));\n"
|
||||||
|
afterCall += "\tmemcpy(*_out, b.data(), b.length());\n"
|
||||||
|
afterCall += "\t*_out_Strlen = b.length();\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
preamble, forwarding := emitParametersCABI2CppForwarding(m.Parameters, c.ClassName)
|
preamble, forwarding := emitParametersCABI2CppForwarding(m.Parameters, c.ClassName)
|
||||||
@ -177,11 +208,13 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
|
|||||||
"%s %s_%s(%s) {\n"+
|
"%s %s_%s(%s) {\n"+
|
||||||
"%s"+
|
"%s"+
|
||||||
"\t%sstatic_cast<%s*>(self)->%s(%s);\n"+
|
"\t%sstatic_cast<%s*>(self)->%s(%s);\n"+
|
||||||
|
"%s"+
|
||||||
"}\n"+
|
"}\n"+
|
||||||
"\n",
|
"\n",
|
||||||
m.ReturnType.RenderTypeCpp(), c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName),
|
emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, "P"+c.ClassName),
|
||||||
preamble,
|
preamble,
|
||||||
shouldReturn, c.ClassName, m.MethodName, forwarding,
|
shouldReturn, c.ClassName, m.MethodName, forwarding,
|
||||||
|
afterCall,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,10 @@ func emitParametersGo(params []CppParameter) string {
|
|||||||
return strings.Join(tmp, ", ")
|
return strings.Join(tmp, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func emitParametersGo2CABIForwarding(params []CppParameter) (preamble string, fowarding string) {
|
func emitParametersGo2CABIForwarding(m CppMethod) (preamble string, fowarding string) {
|
||||||
tmp := make([]string, 0, len(params))
|
tmp := make([]string, 0, len(m.Parameters))
|
||||||
for _, p := range params {
|
|
||||||
|
for _, p := range m.Parameters {
|
||||||
if p.ParameterType == "QString" {
|
if p.ParameterType == "QString" {
|
||||||
// Go: convert string -> char* and len
|
// Go: convert string -> char* and len
|
||||||
// CABI: convert char* and len -> real QString
|
// CABI: convert char* and len -> real QString
|
||||||
@ -59,6 +60,10 @@ func emitParametersGo2CABIForwarding(params []CppParameter) (preamble string, fo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.ReturnType.ParameterType == "QString" {
|
||||||
|
tmp = append(tmp, "&_out, &_out_Strlen")
|
||||||
|
}
|
||||||
|
|
||||||
return preamble, strings.Join(tmp, ", ")
|
return preamble, strings.Join(tmp, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +101,7 @@ import "C"
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
for i, ctor := range c.Ctors {
|
for i, ctor := range c.Ctors {
|
||||||
preamble, forwarding := emitParametersGo2CABIForwarding(ctor.Parameters)
|
preamble, forwarding := emitParametersGo2CABIForwarding(ctor)
|
||||||
ret.WriteString(`
|
ret.WriteString(`
|
||||||
// New` + c.ClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object.
|
// New` + c.ClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object.
|
||||||
func New` + c.ClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) {
|
func New` + c.ClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) {
|
||||||
@ -108,21 +113,32 @@ import "C"
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range c.Methods {
|
for _, m := range c.Methods {
|
||||||
// TODO for any known pointer type, call its cPointer() method instead of passing it directly
|
preamble, forwarding := emitParametersGo2CABIForwarding(m)
|
||||||
|
|
||||||
shouldReturn := "return "
|
shouldReturn := "return "
|
||||||
|
afterword := ""
|
||||||
returnTypeDecl := m.ReturnType.ParameterType // FIXME handle byRef/const here too
|
returnTypeDecl := m.ReturnType.ParameterType // FIXME handle byRef/const here too
|
||||||
if returnTypeDecl == "void" {
|
|
||||||
|
if m.ReturnType.ParameterType == "void" {
|
||||||
shouldReturn = ""
|
shouldReturn = ""
|
||||||
returnTypeDecl = ""
|
returnTypeDecl = ""
|
||||||
}
|
|
||||||
|
|
||||||
preamble, forwarding := emitParametersGo2CABIForwarding(m.Parameters)
|
} else if m.ReturnType.ParameterType == "QString" {
|
||||||
|
shouldReturn = ""
|
||||||
|
returnTypeDecl = "string"
|
||||||
|
|
||||||
|
preamble += "var _out *C.char = nil\n"
|
||||||
|
preamble += "var _out_Strlen C.size_t = 0\n"
|
||||||
|
afterword += "ret := C.GoStringN(_out, _out_Strlen)\n"
|
||||||
|
afterword += "C.free(_out)\n"
|
||||||
|
afterword += "return ret"
|
||||||
|
}
|
||||||
|
|
||||||
ret.WriteString(`
|
ret.WriteString(`
|
||||||
func (this *` + c.ClassName + `) ` + m.SafeMethodName() + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {
|
func (this *` + c.ClassName + `) ` + m.SafeMethodName() + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {
|
||||||
` + preamble + shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + forwarding + `)
|
` + preamble +
|
||||||
}
|
shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + forwarding + `)
|
||||||
|
` + afterword + `}
|
||||||
|
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user