2024-08-06 01:03:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-15 07:53:01 +00:00
|
|
|
"C"
|
2024-09-14 08:32:55 +00:00
|
|
|
"fmt"
|
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-15 07:49:38 +00:00
|
|
|
func goReservedWord(s string) bool {
|
|
|
|
switch s {
|
2024-08-25 03:32:08 +00:00
|
|
|
case "default", "const", "func", "var", "type", "len", "new", "copy", "import", "range", "string", "map", "int", "select",
|
|
|
|
"ret": // not a language-reserved word, but a binding-reserved word
|
2024-08-15 07:49:38 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-08-11 04:37:18 +00:00
|
|
|
if t, ok := p.QListOf(); ok {
|
|
|
|
return "[]" + t.RenderTypeGo()
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
ret := ""
|
|
|
|
if p.ByRef || p.Pointer {
|
|
|
|
ret += "*"
|
|
|
|
}
|
2024-08-16 23:27:02 +00:00
|
|
|
|
|
|
|
switch p.ParameterType {
|
2024-08-29 05:15:41 +00:00
|
|
|
case "char", "qint8", "signed char", "unsigned char", "uchar", "quint8":
|
2024-08-17 02:10:17 +00:00
|
|
|
ret += "byte" // Strictly speaking, Go byte is unsigned and char may be signed
|
2024-08-16 23:27:02 +00:00
|
|
|
case "short", "qint16":
|
|
|
|
ret += "int16"
|
2024-08-19 07:12:37 +00:00
|
|
|
case "ushort", "quint16", "unsigned short":
|
2024-08-16 23:27:02 +00:00
|
|
|
ret += "uint16"
|
|
|
|
case "long":
|
|
|
|
// Windows ILP32 - 32-bits
|
|
|
|
// Linux LP64 - 64-bits
|
|
|
|
if C.sizeof_long == 4 {
|
|
|
|
ret += "int32"
|
|
|
|
} else {
|
|
|
|
ret += "int64"
|
|
|
|
}
|
|
|
|
case "ulong", "unsigned long":
|
|
|
|
if C.sizeof_long == 4 {
|
|
|
|
ret += "uint32"
|
|
|
|
} else {
|
|
|
|
ret += "uint64"
|
|
|
|
}
|
|
|
|
|
2024-08-19 07:12:37 +00:00
|
|
|
case "unsigned int":
|
2024-08-25 03:31:21 +00:00
|
|
|
ret += "uint"
|
2024-08-16 23:27:02 +00:00
|
|
|
case "qint32":
|
|
|
|
ret += "int32"
|
|
|
|
case "quint32":
|
|
|
|
ret += "uint32"
|
2024-08-19 07:12:37 +00:00
|
|
|
case "qlonglong", "qint64", "long long":
|
2024-08-16 23:27:02 +00:00
|
|
|
ret += "int64"
|
2024-08-19 07:12:37 +00:00
|
|
|
case "qulonglong", "quint64", "unsigned long long":
|
2024-08-16 23:27:02 +00:00
|
|
|
ret += "uint64"
|
|
|
|
case "float":
|
|
|
|
ret += "float32"
|
2024-08-18 06:56:09 +00:00
|
|
|
case "double", "qreal":
|
2024-08-16 23:27:02 +00:00
|
|
|
ret += "float64"
|
2024-08-26 10:51:21 +00:00
|
|
|
case "qsizetype", "size_t", "qptrdiff", "ptrdiff_t":
|
2024-08-17 02:10:17 +00:00
|
|
|
if C.sizeof_size_t == 4 {
|
|
|
|
ret += "uint32"
|
|
|
|
} else {
|
|
|
|
ret += "uint64"
|
|
|
|
}
|
2024-08-25 03:31:21 +00:00
|
|
|
case "qintptr", "uintptr_t", "intptr_t", "quintptr":
|
|
|
|
ret += "uintptr"
|
2024-08-16 23:27:02 +00:00
|
|
|
default:
|
2024-08-25 05:50:14 +00:00
|
|
|
|
2024-08-29 05:16:36 +00:00
|
|
|
if p.IsFlagType() {
|
2024-08-27 06:44:44 +00:00
|
|
|
ret += "int"
|
|
|
|
|
|
|
|
} else if strings.Contains(p.ParameterType, `::`) {
|
2024-09-04 06:54:10 +00:00
|
|
|
if p.IsKnownEnum() {
|
|
|
|
ret += cabiClassName(p.ParameterType)
|
|
|
|
|
|
|
|
} else if p.IsEnum() {
|
2024-08-27 08:05:38 +00:00
|
|
|
ret += "uintptr"
|
|
|
|
} else {
|
2024-08-26 10:48:37 +00:00
|
|
|
// Inner class
|
|
|
|
ret += cabiClassName(p.ParameterType)
|
|
|
|
}
|
2024-08-25 05:50:14 +00:00
|
|
|
} else {
|
|
|
|
// Do not transform this type
|
|
|
|
ret += p.ParameterType
|
|
|
|
}
|
2024-08-16 23:27:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
return ret // ignore const
|
|
|
|
}
|
|
|
|
|
2024-08-16 23:28:49 +00:00
|
|
|
func (p CppParameter) parameterTypeCgo() string {
|
2024-08-17 00:39:45 +00:00
|
|
|
if p.ParameterType == "QString" {
|
2024-09-14 07:31:39 +00:00
|
|
|
return "*C.struct_miqt_string"
|
2024-08-17 00:39:45 +00:00
|
|
|
}
|
2024-09-14 07:31:39 +00:00
|
|
|
|
|
|
|
if _, ok := p.QListOf(); ok {
|
|
|
|
return "*C.struct_miqt_array"
|
|
|
|
}
|
|
|
|
|
2024-08-20 08:19:38 +00:00
|
|
|
tmp := strings.Replace(p.RenderTypeCabi(), `*`, "", -1)
|
2024-08-29 05:38:18 +00:00
|
|
|
if strings.HasPrefix(tmp, "const ") {
|
|
|
|
tmp = tmp[6:] // Constness doesn't survive the CABI boundary
|
|
|
|
}
|
2024-08-16 23:28:49 +00:00
|
|
|
if strings.HasPrefix(tmp, "unsigned ") {
|
|
|
|
tmp = "u" + tmp[9:] // Cgo uses uchar, uint instead of full name
|
|
|
|
}
|
2024-08-29 07:01:12 +00:00
|
|
|
if strings.HasPrefix(tmp, "signed ") {
|
|
|
|
tmp = "s" + tmp[7:] // Cgo uses schar
|
|
|
|
}
|
2024-08-23 23:45:36 +00:00
|
|
|
tmp = strings.Replace(tmp, `long long`, `longlong`, -1)
|
2024-09-14 07:31:39 +00:00
|
|
|
tmp = "C." + strings.Replace(tmp, " ", "_", -1)
|
|
|
|
|
|
|
|
if p.QtClassType() || p.Pointer || p.ByRef {
|
|
|
|
return "*" + tmp
|
|
|
|
} else {
|
|
|
|
return tmp
|
|
|
|
}
|
2024-08-16 23:28:49 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2024-08-17 02:10:59 +00:00
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
skipNext := false
|
2024-08-17 02:10:59 +00:00
|
|
|
|
|
|
|
for i, p := range params {
|
|
|
|
|
2024-08-18 03:57:29 +00:00
|
|
|
if IsArgcArgv(params, i) {
|
2024-08-18 03:24:04 +00:00
|
|
|
skipNext = true
|
2024-08-17 02:10:59 +00:00
|
|
|
tmp = append(tmp, "args []string")
|
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
} else if skipNext {
|
2024-08-17 02:10:59 +00:00
|
|
|
// Skip this parameter, already handled
|
2024-08-25 00:49:43 +00:00
|
|
|
skipNext = false
|
2024-08-17 02:10:59 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Ordinary parameter
|
|
|
|
tmp = append(tmp, p.ParameterName+" "+p.RenderTypeGo())
|
|
|
|
|
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
}
|
|
|
|
return strings.Join(tmp, ", ")
|
|
|
|
}
|
|
|
|
|
2024-08-23 23:46:47 +00:00
|
|
|
type goFileState struct {
|
|
|
|
imports map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2024-08-25 03:31:59 +00:00
|
|
|
func (gfs *goFileState) emitParametersGo2CABIForwarding(m CppMethod) (preamble string, forwarding string) {
|
2024-08-09 22:33:12 +00:00
|
|
|
tmp := make([]string, 0, len(m.Parameters)+2)
|
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
if !m.IsStatic {
|
|
|
|
tmp = append(tmp, "this.h")
|
|
|
|
}
|
2024-08-09 06:41:29 +00:00
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
skipNext := false
|
2024-08-17 02:10:59 +00:00
|
|
|
|
|
|
|
for i, p := range m.Parameters {
|
|
|
|
|
2024-08-18 03:57:29 +00:00
|
|
|
if IsArgcArgv(m.Parameters, i) {
|
2024-08-18 03:24:04 +00:00
|
|
|
skipNext = true
|
2024-08-17 02:10:59 +00:00
|
|
|
// QApplication constructor. Convert 'args' into Qt's wanted types
|
|
|
|
// Qt has a warning in the docs saying these pointers must be valid
|
|
|
|
// for the entire lifetype of QApplication, so, malloc + never free
|
|
|
|
|
|
|
|
preamble += "// Convert []string to long-lived int& argc, char** argv, never call free()\n"
|
|
|
|
preamble += "argc := (*C.int)(C.malloc(8))\n"
|
2024-08-25 00:49:43 +00:00
|
|
|
preamble += "*argc = C.int(len(args))\n"
|
2024-09-01 05:51:17 +00:00
|
|
|
preamble += "argv := (*[0xffff]*C.char)(C.malloc(C.size_t(8 * len(args))))\n"
|
2024-08-17 02:10:59 +00:00
|
|
|
preamble += "for i := range args {\n"
|
2024-08-25 00:49:43 +00:00
|
|
|
preamble += "argv[i] = C.CString(args[i])\n"
|
2024-08-17 02:10:59 +00:00
|
|
|
preamble += "}\n"
|
|
|
|
|
2024-08-25 03:31:51 +00:00
|
|
|
tmp = append(tmp, "argc, &argv[0]")
|
2024-08-17 02:10:59 +00:00
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
} else if skipNext {
|
2024-08-17 02:10:59 +00:00
|
|
|
// Skip this parameter, already handled
|
2024-08-18 03:24:04 +00:00
|
|
|
skipNext = false
|
2024-08-17 02:10:59 +00:00
|
|
|
|
|
|
|
} else if p.ParameterType == "QString" {
|
2024-09-14 04:21:05 +00:00
|
|
|
// Go: convert string -> miqt_string*
|
|
|
|
// CABI: convert miqt_string* -> real QString
|
2024-08-23 23:46:47 +00:00
|
|
|
|
2024-09-14 04:21:05 +00:00
|
|
|
preamble += p.ParameterName + "_ms := miqt_strdupg(" + p.ParameterName + ")\n"
|
|
|
|
preamble += "defer C.free(" + p.ParameterName + "_ms)\n"
|
2024-09-14 06:26:59 +00:00
|
|
|
tmp = append(tmp, "(*C.struct_miqt_string)("+p.ParameterName+"_ms)")
|
2024-08-08 06:54:13 +00:00
|
|
|
|
2024-08-11 04:37:18 +00:00
|
|
|
} else if listType, ok := p.QListOf(); ok {
|
|
|
|
// QList<T>
|
|
|
|
// Go: convert T[] -> t* and len
|
|
|
|
// CABI: create a real QList<>
|
|
|
|
|
2024-09-14 07:31:39 +00:00
|
|
|
gfs.imports["runtime"] = struct{}{}
|
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
|
2024-08-15 07:50:30 +00:00
|
|
|
if listType.ParameterType == "QString" {
|
|
|
|
// Combo
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "// For the C ABI, malloc two C arrays; raw char* pointers and their lengths\n"
|
2024-09-14 07:31:39 +00:00
|
|
|
preamble += p.ParameterName + "_CArray := (*[0xffff]" + listType.parameterTypeCgo() + ")(C.malloc(C.size_t(8 * len(" + p.ParameterName + "))))\n"
|
2024-08-25 00:50:04 +00:00
|
|
|
preamble += "defer C.free(unsafe.Pointer(" + p.ParameterName + "_CArray))\n"
|
2024-09-14 06:26:59 +00:00
|
|
|
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "for i := range " + p.ParameterName + "{\n"
|
2024-09-14 06:26:59 +00:00
|
|
|
preamble += "single_ms := miqt_strdupg(" + p.ParameterName + "[i])\n"
|
2024-09-14 22:16:28 +00:00
|
|
|
preamble += "defer C.free(single_ms)\n"
|
|
|
|
preamble += p.ParameterName + "_CArray[i] = (*C.struct_miqt_string)(single_ms)\n"
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "}\n"
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-09-14 06:26:59 +00:00
|
|
|
preamble += p.ParameterName + "_ma := &C.struct_miqt_array{len: C.size_t(len(" + p.ParameterName + ")), data: unsafe.Pointer(" + p.ParameterName + "_CArray)}\n"
|
|
|
|
preamble += "defer runtime.KeepAlive(unsafe.Pointer(" + p.ParameterName + "_ma))\n"
|
|
|
|
|
2024-09-14 07:31:39 +00:00
|
|
|
tmp = append(tmp, p.ParameterName+"_ma")
|
2024-08-15 07:50:30 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
preamble += "// For the C ABI, malloc a C array of raw pointers\n"
|
2024-09-14 07:31:39 +00:00
|
|
|
preamble += p.ParameterName + "_CArray := (*[0xffff]" + listType.parameterTypeCgo() + ")(C.malloc(C.size_t(8 * len(" + p.ParameterName + "))))\n"
|
2024-08-25 00:50:04 +00:00
|
|
|
preamble += "defer C.free(unsafe.Pointer(" + p.ParameterName + "_CArray))\n"
|
2024-09-14 06:26:59 +00:00
|
|
|
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "for i := range " + p.ParameterName + "{\n"
|
2024-08-25 00:50:04 +00:00
|
|
|
if listType.QtClassType() {
|
|
|
|
preamble += p.ParameterName + "_CArray[i] = " + p.ParameterName + "[i].cPointer()\n"
|
|
|
|
} else {
|
|
|
|
preamble += p.ParameterName + "_CArray[i] = (" + listType.parameterTypeCgo() + ")(" + p.ParameterName + "[i])\n"
|
|
|
|
}
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "}\n"
|
|
|
|
|
2024-09-14 06:26:59 +00:00
|
|
|
preamble += p.ParameterName + "_ma := &C.struct_miqt_array{len: C.size_t(len(" + p.ParameterName + ")), data: unsafe.Pointer(" + p.ParameterName + "_CArray)}\n"
|
|
|
|
preamble += "defer runtime.KeepAlive(unsafe.Pointer(" + p.ParameterName + "_ma))\n"
|
|
|
|
|
2024-09-14 07:31:39 +00:00
|
|
|
tmp = append(tmp, p.ParameterName+"_ma")
|
2024-08-15 07:50:30 +00:00
|
|
|
}
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
} else if p.Pointer && p.ParameterType == "char" {
|
|
|
|
// Single char* argument
|
2024-08-23 23:46:47 +00:00
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
2024-08-08 06:54:13 +00:00
|
|
|
preamble += p.ParameterName + "_Cstring := C.CString(" + p.ParameterName + ")\n"
|
2024-08-23 23:46:47 +00:00
|
|
|
preamble += "defer C.free(unsafe.Pointer(" + p.ParameterName + "_Cstring))\n"
|
2024-08-08 06:54:13 +00:00
|
|
|
tmp = append(tmp, p.ParameterName+"_Cstring")
|
|
|
|
|
2024-08-25 03:31:51 +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()")
|
|
|
|
|
2024-08-23 23:45:36 +00:00
|
|
|
} else if p.IntType() || p.ParameterType == "bool" {
|
2024-08-25 03:31:51 +00:00
|
|
|
if p.Pointer || p.ByRef {
|
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
2024-09-14 07:31:39 +00:00
|
|
|
tmp = append(tmp, "("+p.parameterTypeCgo()+")(unsafe.Pointer("+p.ParameterName+"))") // n.b. This may not work if the integer type conversion was wrong
|
2024-08-25 00:50:04 +00:00
|
|
|
} else {
|
|
|
|
tmp = append(tmp, "("+p.parameterTypeCgo()+")("+p.ParameterName+")")
|
|
|
|
}
|
2024-08-23 23:46:47 +00:00
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
} else {
|
|
|
|
// Default
|
|
|
|
tmp = append(tmp, p.ParameterName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return preamble, strings.Join(tmp, ", ")
|
|
|
|
}
|
|
|
|
|
2024-09-14 08:32:55 +00:00
|
|
|
func (gfs *goFileState) emitCabiToGo(assignExpr string, rt CppParameter, rvalue string) string {
|
|
|
|
|
|
|
|
shouldReturn := "return "
|
|
|
|
afterword := ""
|
|
|
|
namePrefix := rt.ParameterName
|
|
|
|
|
|
|
|
if rt.ParameterType == "void" && !rt.Pointer {
|
|
|
|
shouldReturn = ""
|
|
|
|
|
|
|
|
} else if rt.ParameterType == "void" && rt.Pointer {
|
|
|
|
// ...
|
|
|
|
|
|
|
|
} else if rt.ParameterType == "char" && rt.Pointer {
|
|
|
|
// Qt functions normally return QString - anything returning char*
|
|
|
|
// is something like QByteArray.Data() where it returns an unsafe
|
|
|
|
// internal pointer
|
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
|
|
|
|
shouldReturn = namePrefix + "_ret := "
|
|
|
|
afterword += assignExpr + " (unsafe.Pointer)(" + namePrefix + "_ret)\n"
|
|
|
|
|
|
|
|
} else if rt.ParameterType == "QString" {
|
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
|
|
|
|
shouldReturn = "var " + namePrefix + "_ms *C.struct_miqt_string = "
|
2024-09-14 22:16:28 +00:00
|
|
|
afterword += namePrefix + "_ret := C.GoStringN(&" + namePrefix + "_ms.data, C.int(int64(" + namePrefix + "_ms.len)))\n"
|
2024-09-14 08:32:55 +00:00
|
|
|
afterword += "C.free(unsafe.Pointer(" + namePrefix + "_ms))\n"
|
2024-09-14 22:16:28 +00:00
|
|
|
afterword += assignExpr + namePrefix + "_ret"
|
2024-09-14 08:32:55 +00:00
|
|
|
|
|
|
|
} else if t, ok := rt.QListOf(); ok {
|
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
|
|
|
|
shouldReturn = "var " + namePrefix + "_ma *C.struct_miqt_array = "
|
|
|
|
if t.ParameterType == "QString" {
|
|
|
|
// Combo
|
|
|
|
|
|
|
|
afterword += namePrefix + "_ret := make([]string, int(" + namePrefix + "_ma.len))\n"
|
|
|
|
afterword += "_outCast := (*[0xffff]*C.struct_miqt_string)(unsafe.Pointer(" + namePrefix + "_ma.data)) // hey ya\n"
|
|
|
|
afterword += "for i := 0; i < int(" + namePrefix + "_ma.len); i++ {\n"
|
2024-09-14 22:16:28 +00:00
|
|
|
afterword += namePrefix + "_ret[i] = C.GoStringN(&_outCast[i].data, C.int(int64(_outCast[i].len)))\n"
|
2024-09-14 08:32:55 +00:00
|
|
|
afterword += "C.free(unsafe.Pointer(_outCast[i])) // free the inner miqt_string*\n"
|
|
|
|
afterword += "}\n"
|
|
|
|
afterword += "C.free(unsafe.Pointer(" + namePrefix + "_ma))\n"
|
|
|
|
afterword += assignExpr + " " + namePrefix + "_ret\n"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
afterword += "" + namePrefix + "_ret := make([]" + t.RenderTypeGo() + ", int(" + namePrefix + "_ma.len))\n"
|
2024-09-14 22:16:28 +00:00
|
|
|
afterword += "_outCast := (*[0xffff]" + t.parameterTypeCgo() + ")(unsafe.Pointer(" + namePrefix + "_ma.data)) // mrs jackson\n"
|
2024-09-14 08:32:55 +00:00
|
|
|
afterword += "for i := 0; i < int(" + namePrefix + "_ma.len); i++ {\n"
|
|
|
|
if t.QtClassType() {
|
|
|
|
if !t.Pointer {
|
|
|
|
// new, but then dereference it
|
|
|
|
afterword += "" + namePrefix + "_ret[i] = *new" + cabiClassName(t.ParameterType) + "(_outCast[i])\n"
|
|
|
|
} else {
|
|
|
|
afterword += "" + namePrefix + "_ret[i] = new" + cabiClassName(t.ParameterType) + "(_outCast[i])\n"
|
|
|
|
}
|
|
|
|
} else { // plain int type
|
|
|
|
afterword += "" + namePrefix + "_ret[i] = (" + t.RenderTypeGo() + ")(_outCast[i])\n"
|
|
|
|
}
|
|
|
|
afterword += "}\n"
|
|
|
|
afterword += "C.free(unsafe.Pointer(" + namePrefix + "_ma))\n"
|
|
|
|
afterword += assignExpr + " " + namePrefix + "_ret\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if rt.QtClassType() {
|
|
|
|
// Construct our Go type based on this inner CABI type
|
|
|
|
shouldReturn = "" + namePrefix + "_ret := "
|
|
|
|
|
|
|
|
if rt.Pointer || rt.ByRef {
|
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
afterword = assignExpr + " new" + cabiClassName(rt.ParameterType) + "_U(unsafe.Pointer(" + namePrefix + "_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
|
|
|
|
|
2024-09-14 22:16:28 +00:00
|
|
|
afterword += namePrefix + "_goptr := new" + cabiClassName(rt.ParameterType) + "(" + namePrefix + "_ret)\n"
|
|
|
|
afterword += namePrefix + "_goptr.GoGC() // Qt uses pass-by-value semantics for this type. Mimic with finalizer\n"
|
|
|
|
|
|
|
|
// If this is a function return, we have converted value-returned Qt types to pointers
|
|
|
|
// If this is a slot return, we haven't
|
|
|
|
// TODO standardize this
|
|
|
|
if strings.Contains(assignExpr, `return`) {
|
|
|
|
afterword += assignExpr + "" + namePrefix + "_goptr\n"
|
|
|
|
} else {
|
|
|
|
afterword += assignExpr + " *" + namePrefix + "_goptr\n"
|
|
|
|
}
|
2024-09-14 08:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if rt.IntType() || rt.ParameterType == "bool" {
|
|
|
|
// Need to cast Cgo type to Go int type
|
|
|
|
shouldReturn = "" + namePrefix + "_ret := "
|
|
|
|
afterword += assignExpr + "(" + rt.RenderTypeGo() + ")(" + namePrefix + "_ret)\n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return shouldReturn + " " + rvalue + "\n" + afterword
|
|
|
|
}
|
|
|
|
|
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-18 05:48:17 +00:00
|
|
|
ret.WriteString(`package qt
|
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-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-23 23:46:47 +00:00
|
|
|
gfs := goFileState{
|
|
|
|
imports: map[string]struct{}{},
|
|
|
|
}
|
2024-08-09 22:28:02 +00:00
|
|
|
|
2024-09-04 06:54:10 +00:00
|
|
|
for _, e := range src.Enums {
|
|
|
|
goEnumName := cabiClassName(e.EnumName)
|
|
|
|
|
|
|
|
ret.WriteString(`
|
|
|
|
type ` + goEnumName + ` ` + parseSingleTypeString(e.UnderlyingType).RenderTypeGo() + `
|
|
|
|
|
|
|
|
const (
|
|
|
|
`)
|
|
|
|
|
|
|
|
for _, ee := range e.Entries {
|
|
|
|
ret.WriteString(cabiClassName(goEnumName+"::"+ee.EntryName) + " " + goEnumName + " = " + ee.EntryValue + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString("\n)\n\n")
|
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, c := range src.Classes {
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-25 07:08:28 +00:00
|
|
|
goClassName := cabiClassName(c.ClassName)
|
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(`
|
2024-08-25 07:08:28 +00:00
|
|
|
type ` + goClassName + ` struct {
|
|
|
|
h *C.` + goClassName + `
|
2024-08-10 00:54:26 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
// Embed all inherited types to directly allow calling inherited methods
|
|
|
|
for _, base := range c.Inherits {
|
2024-08-23 23:46:47 +00:00
|
|
|
ret.WriteString("*" + base + "\n")
|
2024-08-10 00:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString(`
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 07:08:28 +00:00
|
|
|
func (this *` + goClassName + `) cPointer() *C.` + goClassName + ` {
|
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-23 23:46:47 +00:00
|
|
|
localInit := "h: h"
|
2024-08-10 00:54:26 +00:00
|
|
|
for _, base := range c.Inherits {
|
2024-08-23 23:46:47 +00:00
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
2024-08-26 10:48:37 +00:00
|
|
|
localInit += ", " + base + ": new" + cabiClassName(base) + "_U(unsafe.Pointer(h))"
|
2024-08-10 00:54:26 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 04:37:06 +00:00
|
|
|
ret.WriteString(`
|
2024-08-25 07:08:28 +00:00
|
|
|
func new` + goClassName + `(h *C.` + goClassName + `) *` + goClassName + ` {
|
2024-09-01 02:23:31 +00:00
|
|
|
if h == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-25 07:08:28 +00:00
|
|
|
return &` + goClassName + `{` + localInit + `}
|
2024-08-11 04:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
2024-08-23 23:46:47 +00:00
|
|
|
// CGO types only exist within the same Go file, so other Go files can't
|
2024-08-25 00:50:04 +00:00
|
|
|
// call this same private ctor function, unless it goes through unsafe.Pointer{}.
|
|
|
|
// This is probably because C types can possibly violate the ODR whereas
|
|
|
|
// that never happens in Go's type system.
|
2024-08-23 23:46:47 +00:00
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
ret.WriteString(`
|
2024-08-25 07:08:28 +00:00
|
|
|
func new` + goClassName + `_U(h unsafe.Pointer) *` + goClassName + ` {
|
|
|
|
return new` + goClassName + `( (*C.` + goClassName + `)(h) )
|
2024-08-23 23:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for i, ctor := range c.Ctors {
|
2024-08-23 23:46:47 +00:00
|
|
|
preamble, forwarding := gfs.emitParametersGo2CABIForwarding(ctor)
|
2024-09-01 05:50:58 +00:00
|
|
|
|
|
|
|
if ctor.LinuxOnly {
|
|
|
|
gfs.imports["runtime"] = struct{}{}
|
|
|
|
ret.WriteString(`
|
|
|
|
// New` + goClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object.
|
|
|
|
func New` + goClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) *` + goClassName + ` {
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
` + preamble + ` ret := C.` + goClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `)
|
|
|
|
return new` + goClassName + `(ret)
|
|
|
|
} else {
|
|
|
|
panic("Unsupported OS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
|
|
|
} else {
|
|
|
|
ret.WriteString(`
|
2024-08-25 07:08:28 +00:00
|
|
|
// New` + goClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object.
|
|
|
|
func New` + goClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) *` + goClassName + ` {
|
|
|
|
` + preamble + ` ret := C.` + goClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `)
|
|
|
|
return new` + goClassName + `(ret)
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2024-09-01 05:50:58 +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-23 23:46:47 +00:00
|
|
|
preamble, forwarding := gfs.emitParametersGo2CABIForwarding(m)
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-09-14 08:32:55 +00:00
|
|
|
returnTypeDecl := m.ReturnType.RenderTypeGo()
|
|
|
|
if returnTypeDecl == "void" {
|
2024-08-07 06:51:51 +00:00
|
|
|
returnTypeDecl = ""
|
2024-09-14 08:32:55 +00:00
|
|
|
}
|
|
|
|
if m.ReturnType.QtClassType() && m.ReturnType.ParameterType != "QString" && !(m.ReturnType.Pointer || m.ReturnType.ByRef) {
|
|
|
|
returnTypeDecl = "*" + returnTypeDecl
|
|
|
|
}
|
|
|
|
if (m.ReturnType.ParameterType == "char" || m.ReturnType.ParameterType == "void") && m.ReturnType.Pointer {
|
2024-08-23 23:46:47 +00:00
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
2024-08-15 07:53:01 +00:00
|
|
|
returnTypeDecl = "unsafe.Pointer"
|
2024-09-14 08:32:55 +00:00
|
|
|
}
|
2024-09-14 04:21:05 +00:00
|
|
|
|
2024-09-14 08:32:55 +00:00
|
|
|
rvalue := `C.` + goClassName + `_` + m.SafeMethodName() + `(` + forwarding + `)`
|
2024-08-23 23:45:36 +00:00
|
|
|
|
2024-09-14 08:32:55 +00:00
|
|
|
returnFunc := gfs.emitCabiToGo("return ", m.ReturnType, rvalue)
|
2024-08-08 06:54:13 +00:00
|
|
|
|
2024-08-25 07:08:28 +00:00
|
|
|
receiverAndMethod := `(this *` + goClassName + `) ` + m.SafeMethodName()
|
2024-08-18 03:24:04 +00:00
|
|
|
if m.IsStatic {
|
2024-08-25 07:08:28 +00:00
|
|
|
receiverAndMethod = goClassName + `_` + m.SafeMethodName()
|
2024-08-18 03:24:04 +00:00
|
|
|
}
|
|
|
|
|
2024-09-14 08:32:55 +00:00
|
|
|
ret.WriteString(`
|
|
|
|
func ` + receiverAndMethod + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {`)
|
2024-09-01 05:50:58 +00:00
|
|
|
if m.LinuxOnly {
|
|
|
|
gfs.imports["runtime"] = struct{}{}
|
|
|
|
ret.WriteString(`
|
2024-09-14 08:32:55 +00:00
|
|
|
if runtime.GOOS != "linux" {
|
2024-09-01 05:50:58 +00:00
|
|
|
panic("Unsupported OS")
|
|
|
|
}
|
2024-09-14 08:32:55 +00:00
|
|
|
`)
|
2024-09-01 05:50:58 +00:00
|
|
|
}
|
2024-09-14 08:32:55 +00:00
|
|
|
ret.WriteString(`
|
2024-08-09 06:41:29 +00:00
|
|
|
` + preamble +
|
2024-09-14 08:32:55 +00:00
|
|
|
returnFunc + `}
|
2024-08-07 06:51:51 +00:00
|
|
|
`)
|
2024-08-18 05:48:17 +00:00
|
|
|
|
|
|
|
// Add Connect() wrappers for signal functions
|
2024-09-07 03:25:25 +00:00
|
|
|
if m.IsSignal {
|
2024-08-23 23:46:47 +00:00
|
|
|
gfs.imports["unsafe"] = struct{}{}
|
|
|
|
gfs.imports["runtime/cgo"] = struct{}{}
|
2024-09-14 07:31:39 +00:00
|
|
|
|
|
|
|
var cgoNamedParams []string
|
|
|
|
var paramNames []string
|
2024-09-14 08:32:55 +00:00
|
|
|
conversion := ""
|
|
|
|
|
|
|
|
if len(m.Parameters) > 0 {
|
|
|
|
conversion = "// Convert all CABI parameters to Go parameters\n"
|
|
|
|
}
|
|
|
|
for i, pp := range m.Parameters {
|
2024-09-14 07:31:39 +00:00
|
|
|
cgoNamedParams = append(cgoNamedParams, pp.ParameterName+" "+pp.parameterTypeCgo())
|
2024-09-14 08:32:55 +00:00
|
|
|
|
|
|
|
paramNames = append(paramNames, fmt.Sprintf("slotval%d", i+1))
|
|
|
|
conversion += gfs.emitCabiToGo(fmt.Sprintf("slotval%d := ", i+1), pp, pp.ParameterName) + "\n"
|
2024-09-14 07:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString(`func (this *` + goClassName + `) On` + m.SafeMethodName() + `(slot func(` + emitParametersGo(m.Parameters) + `)) {
|
2024-09-14 08:32:55 +00:00
|
|
|
C.` + goClassName + `_connect_` + m.SafeMethodName() + `(this.h, unsafe.Pointer(uintptr(cgo.NewHandle(slot))))
|
2024-08-18 05:48:17 +00:00
|
|
|
}
|
2024-09-14 07:31:39 +00:00
|
|
|
|
|
|
|
//export miqt_exec_callback_` + goClassName + `_` + m.SafeMethodName() + `
|
|
|
|
func miqt_exec_callback_` + goClassName + `_` + m.SafeMethodName() + `(cb *C.void` + ifv(len(m.Parameters) > 0, ", ", "") + strings.Join(cgoNamedParams, `, `) + `) {
|
2024-09-14 08:32:55 +00:00
|
|
|
gofunc, ok := (cgo.Handle(uintptr(unsafe.Pointer(cb))).Value()).(func(` + emitParametersGo(m.Parameters) + `))
|
2024-09-14 07:31:39 +00:00
|
|
|
if !ok {
|
|
|
|
panic("miqt: callback of non-callback type (heap corruption?)")
|
|
|
|
}
|
|
|
|
|
2024-09-14 08:32:55 +00:00
|
|
|
` + conversion + `
|
|
|
|
|
|
|
|
gofunc(` + strings.Join(paramNames, `, `) + ` )
|
2024-09-14 07:31:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 05:48:17 +00:00
|
|
|
`)
|
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
2024-08-06 02:29:12 +00:00
|
|
|
|
2024-08-20 08:10:57 +00:00
|
|
|
if c.CanDelete {
|
2024-09-14 22:16:28 +00:00
|
|
|
gfs.imports["runtime"] = struct{}{} // Finalizer
|
|
|
|
|
2024-08-18 05:47:19 +00:00
|
|
|
ret.WriteString(`
|
2024-09-14 22:16:28 +00:00
|
|
|
// Delete this object from C++ memory.
|
2024-08-25 07:08:28 +00:00
|
|
|
func (this *` + goClassName + `) Delete() {
|
|
|
|
C.` + goClassName + `_Delete(this.h)
|
2024-08-18 05:47:19 +00:00
|
|
|
}
|
2024-09-14 22:16:28 +00:00
|
|
|
|
|
|
|
// GoGC adds a Go Finalizer to this pointer, so that it will be deleted
|
|
|
|
// from C++ memory once it is unreachable from Go memory.
|
|
|
|
func (this *` + goClassName + `) GoGC() {
|
|
|
|
runtime.SetFinalizer(this, func(this *` + goClassName + `) {
|
|
|
|
this.Delete()
|
|
|
|
runtime.KeepAlive(this.h)
|
|
|
|
})
|
|
|
|
}
|
2024-08-18 05:47:19 +00:00
|
|
|
`)
|
2024-09-14 22:16:28 +00:00
|
|
|
|
2024-08-09 22:32:57 +00:00
|
|
|
}
|
|
|
|
|
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
|
2024-08-23 23:46:47 +00:00
|
|
|
if len(gfs.imports) > 0 {
|
|
|
|
allImports := make([]string, 0, len(gfs.imports))
|
|
|
|
for k, _ := range gfs.imports {
|
2024-08-09 22:28:02 +00:00
|
|
|
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)
|
2024-08-10 02:04:52 +00:00
|
|
|
formattedSrc = []byte(goSrc)
|
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
|
|
|
}
|