2024-08-06 01:03:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-07 06:51:51 +00:00
|
|
|
"go/format"
|
|
|
|
"log"
|
2024-08-09 22:28:02 +00:00
|
|
|
"sort"
|
2024-08-06 02:29:12 +00:00
|
|
|
"strings"
|
2024-08-06 01:03:23 +00:00
|
|
|
)
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
func (p CppParameter) RenderTypeGo() string {
|
|
|
|
if p.Pointer && p.ParameterType == "char" {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
if p.ParameterType == "QString" {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := ""
|
|
|
|
if p.ByRef || p.Pointer {
|
|
|
|
ret += "*"
|
|
|
|
}
|
|
|
|
ret += p.ParameterType
|
|
|
|
return ret // ignore const
|
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
func emitParametersGo(params []CppParameter) string {
|
2024-08-06 02:29:12 +00:00
|
|
|
tmp := make([]string, 0, len(params))
|
|
|
|
for _, p := range params {
|
2024-08-08 05:51:29 +00:00
|
|
|
tmp = append(tmp, p.ParameterName+" "+p.RenderTypeGo())
|
2024-08-06 02:29:12 +00:00
|
|
|
}
|
|
|
|
return strings.Join(tmp, ", ")
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
func emitParametersGo2CABIForwarding(m CppMethod) (preamble string, fowarding string) {
|
2024-08-09 22:33:12 +00:00
|
|
|
tmp := make([]string, 0, len(m.Parameters)+2)
|
|
|
|
|
|
|
|
tmp = append(tmp, "this.h")
|
2024-08-09 06:41:29 +00:00
|
|
|
|
|
|
|
for _, p := range m.Parameters {
|
2024-08-08 06:54:13 +00:00
|
|
|
if p.ParameterType == "QString" {
|
|
|
|
// Go: convert string -> char* and len
|
|
|
|
// CABI: convert char* and len -> real QString
|
|
|
|
preamble += p.ParameterName + "_Cstring := C.CString(" + p.ParameterName + ")\n"
|
|
|
|
preamble += "defer C.free(" + p.ParameterName + "_Cstring)\n"
|
|
|
|
tmp = append(tmp, p.ParameterName+"_Cstring, len("+p.ParameterName+")")
|
|
|
|
|
|
|
|
// TODO handle the return type as a pointer parameter
|
|
|
|
|
|
|
|
} else if p.Pointer && p.ParameterType == "char" {
|
|
|
|
// Single char* argument
|
|
|
|
preamble += p.ParameterName + "_Cstring := C.CString(" + p.ParameterName + ")\n"
|
|
|
|
preamble += "defer C.free(" + p.ParameterName + "_Cstring)\n"
|
|
|
|
tmp = append(tmp, p.ParameterName+"_Cstring")
|
|
|
|
|
2024-08-08 07:06:14 +00:00
|
|
|
} else if (p.Pointer || p.ByRef) && p.QtClassType() {
|
2024-08-08 06:54:13 +00:00
|
|
|
// The C++ type is a pointer to Qt class
|
|
|
|
// We want our functions to accept the Go wrapper type, and forward as cPointer()
|
|
|
|
tmp = append(tmp, p.ParameterName+".cPointer()")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Default
|
|
|
|
tmp = append(tmp, p.ParameterName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
if m.ReturnType.ParameterType == "QString" {
|
|
|
|
tmp = append(tmp, "&_out, &_out_Strlen")
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
return preamble, strings.Join(tmp, ", ")
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:36 +00:00
|
|
|
func emitGo(src *CppParsedHeader, headerName string) (string, error) {
|
2024-08-07 06:51:51 +00:00
|
|
|
|
2024-08-06 02:29:12 +00:00
|
|
|
ret := strings.Builder{}
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(`package miqt
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
/*
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
#cgo CFLAGS: -fPIC
|
|
|
|
#cgo pkg-config: Qt5Widgets
|
2024-08-08 06:54:36 +00:00
|
|
|
#include "gen_` + headerName + `"
|
|
|
|
#include <stdlib.h>
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-09 22:28:02 +00:00
|
|
|
%%_IMPORTLIBS_%%
|
2024-08-06 02:29:12 +00:00
|
|
|
`)
|
|
|
|
|
2024-08-09 22:28:02 +00:00
|
|
|
imports := map[string]struct{}{}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, c := range src.Classes {
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(`
|
2024-08-07 06:56:14 +00:00
|
|
|
type ` + c.ClassName + ` struct {
|
|
|
|
h C.P` + c.ClassName + `
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
func (this *` + c.ClassName + `) cPointer() C.P` + c.ClassName + ` {
|
2024-08-07 06:51:51 +00:00
|
|
|
if this == nil {
|
|
|
|
return nil
|
2024-08-06 02:29:12 +00:00
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
return this.h
|
2024-08-06 02:29:12 +00:00
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
|
|
|
|
`)
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for i, ctor := range c.Ctors {
|
2024-08-09 06:41:29 +00:00
|
|
|
preamble, forwarding := emitParametersGo2CABIForwarding(ctor)
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(`
|
2024-08-07 06:56:14 +00:00
|
|
|
// New` + c.ClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object.
|
|
|
|
func New` + c.ClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) {
|
2024-08-08 06:54:13 +00:00
|
|
|
` + preamble + ` ret := C.` + c.ClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `)
|
2024-08-07 06:56:14 +00:00
|
|
|
return &` + c.ClassName + `{h: ret}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2024-08-06 02:29:12 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, m := range c.Methods {
|
2024-08-09 06:41:29 +00:00
|
|
|
preamble, forwarding := emitParametersGo2CABIForwarding(m)
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
shouldReturn := "return "
|
2024-08-09 06:41:29 +00:00
|
|
|
afterword := ""
|
2024-08-08 05:51:29 +00:00
|
|
|
returnTypeDecl := m.ReturnType.ParameterType // FIXME handle byRef/const here too
|
2024-08-09 06:41:29 +00:00
|
|
|
|
2024-08-09 22:35:40 +00:00
|
|
|
if m.ReturnType.ParameterType == "void" && !m.ReturnType.Pointer {
|
2024-08-07 06:51:51 +00:00
|
|
|
shouldReturn = ""
|
|
|
|
returnTypeDecl = ""
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
} 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"
|
2024-08-09 22:34:54 +00:00
|
|
|
|
|
|
|
} else if m.ReturnType.QtClassType() {
|
|
|
|
// Construct our Go type based on this inner CABI type
|
|
|
|
shouldReturn = "ret := "
|
|
|
|
|
|
|
|
if m.ReturnType.Pointer {
|
|
|
|
afterword = "return " + m.ReturnType.ParameterType + "{h: ret}"
|
|
|
|
} else {
|
|
|
|
// This is return by value, but CABI has new'd it into a
|
|
|
|
// heap type for us
|
|
|
|
// To preserve Qt's approximate semantics, add a runtime
|
|
|
|
// finalizer to automatically Delete once the type goes out
|
|
|
|
// of Go scope
|
|
|
|
imports["runtime"] = struct{}{}
|
|
|
|
afterword = "// Qt uses pass-by-value semantics for this type. Mimic with finalizer\n"
|
|
|
|
afterword += "ret1 := &" + m.ReturnType.ParameterType + "{h: ret}\n"
|
|
|
|
afterword += "runtime.SetFinalizer(ret1, func(ret2 *" + m.ReturnType.ParameterType + ") {\n"
|
|
|
|
afterword += "ret2.Delete()\n"
|
|
|
|
afterword += "runtime.KeepAlive(ret2.h)\n"
|
|
|
|
afterword += "})\n"
|
|
|
|
afterword += "return ret1"
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
}
|
2024-08-08 06:54:13 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(`
|
2024-08-08 05:51:29 +00:00
|
|
|
func (this *` + c.ClassName + `) ` + m.SafeMethodName() + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {
|
2024-08-09 06:41:29 +00:00
|
|
|
` + preamble +
|
|
|
|
shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + forwarding + `)
|
|
|
|
` + afterword + `}
|
2024-08-07 06:51:51 +00:00
|
|
|
|
|
|
|
`)
|
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-09 22:32:57 +00:00
|
|
|
ret.WriteString(`
|
|
|
|
func (this *` + c.ClassName + `) Delete() {
|
|
|
|
C.` + c.ClassName + `_Delete(this.h)
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-09 22:28:02 +00:00
|
|
|
goSrc := ret.String()
|
|
|
|
|
|
|
|
// Fixup imports
|
|
|
|
if len(imports) > 0 {
|
|
|
|
allImports := make([]string, 0, len(imports))
|
|
|
|
for k, _ := range imports {
|
|
|
|
allImports = append(allImports, `"`+k+`"`)
|
|
|
|
}
|
|
|
|
sort.Strings(allImports)
|
|
|
|
goSrc = strings.Replace(goSrc, `%%_IMPORTLIBS_%%`, "import (\n\t"+strings.Join(allImports, "\n\t")+"\n)", 1)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
goSrc = strings.Replace(goSrc, `%%_IMPORTLIBS_%%`, "", 1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
// Run gofmt over the result
|
2024-08-09 22:28:02 +00:00
|
|
|
formattedSrc, err := format.Source([]byte(goSrc))
|
2024-08-07 06:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("gofmt failure: %v", err)
|
|
|
|
formattedSrc = []byte(ret.String())
|
2024-08-06 02:29:12 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
return string(formattedSrc), nil
|
2024-08-06 01:03:23 +00:00
|
|
|
}
|