2024-08-07 06:51:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-08 07:06:31 +00:00
|
|
|
"sort"
|
2024-08-07 06:51:51 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-08-20 08:19:38 +00:00
|
|
|
func (p CppParameter) RenderTypeCabi() string {
|
2024-08-08 06:54:13 +00:00
|
|
|
ret := p.ParameterType
|
2024-08-14 06:34:05 +00:00
|
|
|
switch p.ParameterType {
|
2024-08-16 23:28:49 +00:00
|
|
|
case "uchar":
|
|
|
|
ret = "unsigned char"
|
2024-08-14 06:34:05 +00:00
|
|
|
case "uint":
|
|
|
|
ret = "unsigned int"
|
|
|
|
case "ulong":
|
|
|
|
ret = "unsigned long"
|
2024-08-15 07:49:05 +00:00
|
|
|
case "qint8":
|
|
|
|
ret = "int8_t"
|
|
|
|
case "quint8":
|
|
|
|
ret = "uint8_t"
|
|
|
|
case "qint16", "short":
|
|
|
|
ret = "int16_t"
|
|
|
|
case "quint16", "ushort", "unsigned short":
|
|
|
|
ret = "uint16_t"
|
|
|
|
case "qint32":
|
|
|
|
ret = "int32_t"
|
|
|
|
case "quint32":
|
|
|
|
ret = "uint32_t"
|
|
|
|
case "qlonglong", "qint64":
|
2024-08-14 06:34:05 +00:00
|
|
|
ret = "int64_t"
|
2024-08-15 07:49:05 +00:00
|
|
|
case "qulonglong", "quint64":
|
2024-08-14 06:34:05 +00:00
|
|
|
ret = "uint64_t"
|
2024-08-15 07:49:05 +00:00
|
|
|
case "qfloat16":
|
|
|
|
ret = "_Float16" // No idea where this typedef comes from, but it exists
|
2024-08-16 23:28:49 +00:00
|
|
|
case "qsizetype":
|
|
|
|
ret = "size_t"
|
2024-08-18 06:56:09 +00:00
|
|
|
case "qreal":
|
|
|
|
ret = "double"
|
2024-08-23 23:45:36 +00:00
|
|
|
case "qintptr":
|
|
|
|
ret = "intptr_t"
|
|
|
|
case "quintptr":
|
2024-08-18 05:47:31 +00:00
|
|
|
ret = "uintptr_t"
|
2024-08-16 23:27:02 +00:00
|
|
|
case "QRgb":
|
|
|
|
ret = "unsigned int"
|
2024-08-14 06:34:05 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
if p.Pointer || p.ByRef {
|
|
|
|
ret += "*"
|
|
|
|
}
|
2024-08-14 06:34:05 +00:00
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
return ret // ignore const
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
func emitReturnTypeCabi(p CppParameter) string {
|
|
|
|
if p.ParameterType == "QString" {
|
|
|
|
return "void" // Will be handled separately
|
|
|
|
|
2024-08-11 04:37:18 +00:00
|
|
|
} else if _, ok := p.QListOf(); ok {
|
|
|
|
return "void" // Will be handled separately
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
} else if (p.Pointer || p.ByRef) && p.QtClassType() {
|
2024-08-14 05:31:10 +00:00
|
|
|
return p.ParameterType + "*" // CABI type
|
2024-08-09 06:41:29 +00:00
|
|
|
|
2024-08-09 22:34:54 +00:00
|
|
|
} else if p.QtClassType() && !p.Pointer {
|
|
|
|
// Even if C++ returns by value, CABI is returning a heap copy (new'd, not malloc'd)
|
2024-08-14 05:31:10 +00:00
|
|
|
return p.ParameterType + "*" // CABI type
|
2024-08-09 22:34:54 +00:00
|
|
|
// return "void" // Handled separately with an _out pointer
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
} else {
|
2024-08-20 08:19:38 +00:00
|
|
|
return p.RenderTypeCabi()
|
2024-08-09 06:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 08:16:13 +00:00
|
|
|
func (p CppParameter) RenderTypeQtCpp() string {
|
|
|
|
cppType := p.ParameterType
|
|
|
|
if len(p.TypeAlias) > 0 {
|
|
|
|
cppType = p.TypeAlias // replace
|
|
|
|
}
|
|
|
|
if p.Const {
|
|
|
|
cppType = "const " + cppType
|
|
|
|
}
|
|
|
|
if p.Pointer {
|
|
|
|
cppType += "*"
|
|
|
|
}
|
|
|
|
if p.ByRef {
|
|
|
|
cppType += "&"
|
|
|
|
}
|
|
|
|
|
|
|
|
return cppType
|
|
|
|
}
|
|
|
|
|
2024-08-18 05:48:17 +00:00
|
|
|
// emitParametersCpp emits the parameter definitions exactly how Qt C++ defines them.
|
|
|
|
func emitParametersCpp(m CppMethod) string {
|
|
|
|
tmp := make([]string, 0, len(m.Parameters))
|
|
|
|
for _, p := range m.Parameters {
|
2024-08-20 08:16:13 +00:00
|
|
|
tmp = append(tmp, p.RenderTypeQtCpp()+" "+p.ParameterName)
|
2024-08-18 05:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(tmp, `, `)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitParameterTypesCpp(m CppMethod) string {
|
|
|
|
tmp := make([]string, 0, len(m.Parameters))
|
|
|
|
for _, p := range m.Parameters {
|
2024-08-20 08:16:13 +00:00
|
|
|
tmp = append(tmp, p.RenderTypeQtCpp())
|
2024-08-18 05:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(tmp, `, `)
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
func emitParametersCabi(m CppMethod, selfType string) string {
|
|
|
|
tmp := make([]string, 0, len(m.Parameters)+1)
|
2024-08-07 06:51:51 +00:00
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
if !m.IsStatic && selfType != "" {
|
2024-08-08 06:55:47 +00:00
|
|
|
tmp = append(tmp, selfType+" self")
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
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" {
|
|
|
|
// The Go code has called this with two arguments: char* and len
|
|
|
|
// Declare that we take two parameters
|
|
|
|
tmp = append(tmp, "const char* "+p.ParameterName+", size_t "+p.ParameterName+"_Strlen")
|
|
|
|
|
2024-08-11 04:37:18 +00:00
|
|
|
} else if t, ok := p.QListOf(); ok {
|
2024-08-15 07:50:30 +00:00
|
|
|
|
|
|
|
if t.ParameterType == "QString" {
|
2024-08-25 03:31:21 +00:00
|
|
|
// Combo
|
|
|
|
tmp = append(tmp, "char** "+p.ParameterName+", uint64_t* "+p.ParameterName+"_Lengths, size_t "+p.ParameterName+"_len")
|
2024-08-15 07:50:30 +00:00
|
|
|
|
2024-08-25 03:31:51 +00:00
|
|
|
} else if t.QtClassType() && !t.Pointer {
|
|
|
|
// The Go code can only work with Qt types as pointers, so the CABI needs to take an array of
|
|
|
|
// pointers, not an array of values
|
|
|
|
// Needs one more level of indirection
|
|
|
|
tmp = append(tmp, t.RenderTypeCabi()+"** "+p.ParameterName+", size_t "+p.ParameterName+"_len")
|
|
|
|
|
2024-08-15 07:50:30 +00:00
|
|
|
} else {
|
|
|
|
// The Go code has called this with two arguments: T* and len
|
|
|
|
// Declare that we take two parameters
|
2024-08-20 08:19:38 +00:00
|
|
|
tmp = append(tmp, t.RenderTypeCabi()+"* "+p.ParameterName+", size_t "+p.ParameterName+"_len")
|
2024-08-15 07:50:30 +00:00
|
|
|
}
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-08 07:06:14 +00:00
|
|
|
} else if (p.ByRef || p.Pointer) && p.QtClassType() {
|
2024-08-08 06:54:13 +00:00
|
|
|
// Pointer to Qt type
|
|
|
|
// Replace with taking our PQ typedef by value
|
2024-08-14 05:31:10 +00:00
|
|
|
tmp = append(tmp, p.ParameterType+"* "+p.ParameterName)
|
2024-08-09 22:36:36 +00:00
|
|
|
|
|
|
|
} else if p.QtClassType() {
|
|
|
|
// Qt type passed by value
|
|
|
|
// The CABI will unconditionally take these by pointer and dereference them
|
|
|
|
// when passing to C++
|
2024-08-14 05:31:10 +00:00
|
|
|
tmp = append(tmp, p.ParameterType+"* "+p.ParameterName)
|
2024-08-09 22:36:36 +00:00
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
} else {
|
2024-08-20 08:19:38 +00:00
|
|
|
// RenderTypeCabi renders both pointer+reference as pointers
|
|
|
|
tmp = append(tmp, p.RenderTypeCabi()+" "+p.ParameterName)
|
2024-08-08 06:54:13 +00:00
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
2024-08-09 06:41:29 +00:00
|
|
|
|
|
|
|
// 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" {
|
2024-08-23 23:45:36 +00:00
|
|
|
// Normally we would use size_t for a strlen, but Go calls C.GoStringN which takes a C.int
|
|
|
|
tmp = append(tmp, "char** _out, int* _out_Strlen")
|
2024-08-11 04:37:18 +00:00
|
|
|
|
|
|
|
} else if t, ok := m.ReturnType.QListOf(); ok {
|
2024-08-17 00:39:45 +00:00
|
|
|
// +1 pointer indirection since it's a heap array
|
|
|
|
// +1 pointer indirection for mutating remote parameter
|
|
|
|
// = 3 for char*, 2 for most types
|
|
|
|
|
|
|
|
// Maybe: +1 pointer indirection if we have to lift stack types to the heap
|
|
|
|
|
|
|
|
if t.ParameterType == "QString" {
|
|
|
|
// Combo
|
2024-08-23 23:45:36 +00:00
|
|
|
tmp = append(tmp, "char*** _out, int** _out_Lengths, size_t* _out_len") // Each length is a C.int for C.GoStringN use
|
2024-08-17 00:39:45 +00:00
|
|
|
} else if t.QtClassType() && !t.Pointer {
|
|
|
|
// QList<QByteArray> QByteArray::Split()
|
|
|
|
// We need to pointer-ify each of the interior elements too
|
2024-08-20 08:19:38 +00:00
|
|
|
tmp = append(tmp, t.RenderTypeCabi()+"*** _out, size_t* _out_len")
|
2024-08-17 00:39:45 +00:00
|
|
|
} else {
|
2024-08-20 08:19:38 +00:00
|
|
|
tmp = append(tmp, t.RenderTypeCabi()+"** _out, size_t* _out_len")
|
2024-08-17 00:39:45 +00:00
|
|
|
}
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
return strings.Join(tmp, ", ")
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:35:58 +00:00
|
|
|
func emitParametersCABI2CppForwarding(params []CppParameter) (preamble string, forwarding string) {
|
2024-08-07 06:51:51 +00:00
|
|
|
tmp := make([]string, 0, len(params)+1)
|
|
|
|
|
|
|
|
for _, p := range params {
|
2024-08-08 06:54:13 +00:00
|
|
|
if p.ParameterType == "QString" {
|
|
|
|
// The CABI has accepted two parameters - need to convert to one real QString
|
|
|
|
// Create it on the stack
|
2024-08-09 22:34:54 +00:00
|
|
|
preamble += "\tQString " + p.ParameterName + "_QString = QString::fromUtf8(" + p.ParameterName + ", " + p.ParameterName + "_Strlen);\n"
|
2024-08-08 06:54:13 +00:00
|
|
|
tmp = append(tmp, p.ParameterName+"_QString")
|
|
|
|
|
2024-08-15 07:50:30 +00:00
|
|
|
} else if listType, ok := p.QListOf(); ok {
|
|
|
|
|
|
|
|
if listType.ParameterType == "QString" {
|
|
|
|
|
|
|
|
// Combo (3 parameters)
|
|
|
|
preamble += "\t" + p.ParameterType + " " + p.ParameterName + "_QList;\n"
|
|
|
|
preamble += "\t" + p.ParameterName + "_QList.reserve(" + p.ParameterName + "_len);\n"
|
|
|
|
preamble += "\tfor(size_t i = 0; i < " + p.ParameterName + "_len; ++i) {\n"
|
2024-08-17 00:39:45 +00:00
|
|
|
preamble += "\t\t" + p.ParameterName + "_QList.push_back(QString::fromUtf8(" + p.ParameterName + "[i], " + p.ParameterName + "_Lengths[i]));\n"
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "\t}\n"
|
|
|
|
tmp = append(tmp, p.ParameterName+"_QList")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// The CABI has accepted two parameters - need to convert to one real QList<>
|
|
|
|
// Create it on the stack
|
|
|
|
preamble += "\t" + p.ParameterType + " " + p.ParameterName + "_QList;\n"
|
|
|
|
preamble += "\t" + p.ParameterName + "_QList.reserve(" + p.ParameterName + "_len);\n"
|
|
|
|
preamble += "\tfor(size_t i = 0; i < " + p.ParameterName + "_len; ++i) {\n"
|
2024-08-25 03:31:51 +00:00
|
|
|
if listType.QtClassType() && !listType.Pointer {
|
|
|
|
preamble += "\t\t" + p.ParameterName + "_QList.push_back(*(" + p.ParameterName + "[i]));\n"
|
|
|
|
} else {
|
|
|
|
preamble += "\t\t" + p.ParameterName + "_QList.push_back(" + p.ParameterName + "[i]);\n"
|
|
|
|
}
|
2024-08-15 07:50:30 +00:00
|
|
|
preamble += "\t}\n"
|
|
|
|
tmp = append(tmp, p.ParameterName+"_QList")
|
|
|
|
}
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-14 06:33:47 +00:00
|
|
|
} else if p.IntType() {
|
|
|
|
// Use the raw ParameterType to select an explicit integer overload
|
2024-08-20 08:19:38 +00:00
|
|
|
// Don't use RenderTypeCabi() since it canonicalizes some int types for CABI
|
2024-08-15 07:49:05 +00:00
|
|
|
castSrc := p.ParameterName
|
2024-08-14 06:33:47 +00:00
|
|
|
castType := p.ParameterType
|
|
|
|
if p.Pointer {
|
|
|
|
castType += "*"
|
|
|
|
}
|
2024-08-15 07:49:05 +00:00
|
|
|
if p.ByRef { // e.g. QDataStream::operator>>() overloads
|
|
|
|
castSrc = "*" + castSrc
|
|
|
|
castType += "&" // believe it or not, this is legal
|
2024-08-22 07:20:29 +00:00
|
|
|
}
|
2024-08-15 07:49:05 +00:00
|
|
|
|
2024-08-22 09:56:19 +00:00
|
|
|
if p.ParameterType == "qint64" || p.ParameterType == "quint64" || p.ParameterType == "qlonglong" || p.ParameterType == "qulonglong" {
|
|
|
|
// QDataStream::operator>>() by reference (qint64)
|
2024-08-22 07:20:29 +00:00
|
|
|
// QLockFile::getLockInfo() by pointer
|
2024-08-22 09:56:19 +00:00
|
|
|
// QTextStream::operator>>() by reference (qlonglong + qulonglong)
|
2024-08-22 07:20:29 +00:00
|
|
|
// CABI has these as int64_t* (long int) which fails a static_cast to qint64& (long long int&)
|
|
|
|
// Hack a hard C-style cast
|
|
|
|
tmp = append(tmp, "("+castType+")("+castSrc+")")
|
|
|
|
} else {
|
|
|
|
// Use static_cast<> safely
|
|
|
|
tmp = append(tmp, "static_cast<"+castType+">("+castSrc+")")
|
2024-08-15 07:49:05 +00:00
|
|
|
}
|
2024-08-14 06:33:47 +00:00
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
} else if p.ByRef {
|
2024-08-20 08:19:38 +00:00
|
|
|
// We changed RenderTypeCabi() to render this as a pointer
|
2024-08-08 06:54:13 +00:00
|
|
|
// Need to dereference so we can pass as reference to the actual Qt C++ function
|
2024-08-09 22:34:54 +00:00
|
|
|
//tmp = append(tmp, "*"+p.ParameterName)
|
2024-08-14 05:31:10 +00:00
|
|
|
tmp = append(tmp, "*"+p.ParameterName)
|
2024-08-09 22:34:54 +00:00
|
|
|
|
|
|
|
} else if p.QtClassType() && !p.Pointer {
|
|
|
|
// CABI takes all Qt types by pointer, even if C++ wants them by value
|
|
|
|
// Dereference the passed-in pointer
|
2024-08-14 05:31:10 +00:00
|
|
|
tmp = append(tmp, "*"+p.ParameterName)
|
2024-08-09 22:34:54 +00:00
|
|
|
|
2024-08-14 05:31:10 +00:00
|
|
|
// } else if p.QtClassType() && p.Pointer {
|
2024-08-09 23:46:48 +00:00
|
|
|
// We need this static_cast<> anyway to convert from PQt (void*) to
|
|
|
|
// the real Qt type
|
2024-08-14 05:31:10 +00:00
|
|
|
// tmp = append(tmp, "static_cast<"+p.ParameterType+"*>("+p.ParameterName+")")
|
2024-08-09 23:46:48 +00:00
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
} else {
|
|
|
|
tmp = append(tmp, p.ParameterName)
|
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
2024-08-08 06:54:13 +00:00
|
|
|
|
|
|
|
return preamble, strings.Join(tmp, ", ")
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 06:34:27 +00:00
|
|
|
// getReferencedTypes finds all referenced Qt types in this file.
|
2024-08-14 05:31:30 +00:00
|
|
|
func getReferencedTypes(src *CppParsedHeader) []string {
|
2024-08-14 06:34:27 +00:00
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
foundTypes := map[string]struct{}{}
|
|
|
|
for _, c := range src.Classes {
|
2024-08-14 06:34:27 +00:00
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
foundTypes[c.ClassName] = struct{}{}
|
2024-08-14 06:34:27 +00:00
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
for _, ctor := range c.Ctors {
|
|
|
|
for _, p := range ctor.Parameters {
|
|
|
|
if p.QtClassType() {
|
|
|
|
foundTypes[p.ParameterType] = struct{}{}
|
|
|
|
}
|
2024-08-17 02:10:33 +00:00
|
|
|
if t, ok := p.QListOf(); ok {
|
|
|
|
foundTypes["QList"] = struct{}{} // FIXME or QVector?
|
|
|
|
if t.QtClassType() {
|
|
|
|
foundTypes[t.ParameterType] = struct{}{}
|
|
|
|
}
|
2024-08-14 06:34:27 +00:00
|
|
|
}
|
2024-08-08 07:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, m := range c.Methods {
|
|
|
|
for _, p := range m.Parameters {
|
|
|
|
if p.QtClassType() {
|
|
|
|
foundTypes[p.ParameterType] = struct{}{}
|
|
|
|
}
|
2024-08-17 02:10:33 +00:00
|
|
|
if t, ok := p.QListOf(); ok {
|
|
|
|
foundTypes["QList"] = struct{}{} // FIXME or QVector?
|
|
|
|
if t.QtClassType() {
|
|
|
|
foundTypes[t.ParameterType] = struct{}{}
|
|
|
|
}
|
2024-08-14 06:34:27 +00:00
|
|
|
}
|
2024-08-08 07:06:31 +00:00
|
|
|
}
|
|
|
|
if m.ReturnType.QtClassType() {
|
|
|
|
foundTypes[m.ReturnType.ParameterType] = struct{}{}
|
|
|
|
}
|
2024-08-17 02:10:33 +00:00
|
|
|
if t, ok := m.ReturnType.QListOf(); ok {
|
|
|
|
foundTypes["QList"] = struct{}{} // FIXME or QVector?
|
|
|
|
if t.QtClassType() {
|
|
|
|
foundTypes[t.ParameterType] = struct{}{}
|
|
|
|
}
|
2024-08-14 06:34:27 +00:00
|
|
|
}
|
2024-08-08 07:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-14 06:34:27 +00:00
|
|
|
|
2024-08-16 23:25:54 +00:00
|
|
|
// Some types (e.g. QRgb) are found but are typedefs, not classes
|
|
|
|
for _, td := range src.Typedefs {
|
|
|
|
delete(foundTypes, td.Alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to sorted list
|
2024-08-08 07:06:31 +00:00
|
|
|
foundTypesList := make([]string, 0, len(foundTypes))
|
|
|
|
for ft := range foundTypes {
|
2024-08-16 23:29:31 +00:00
|
|
|
if strings.HasPrefix(ft, "QList<") || strings.HasPrefix(ft, "QVector<") {
|
2024-08-14 05:31:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-08-17 02:09:51 +00:00
|
|
|
if strings.HasSuffix(ft, "Private") { // qbrush.h finds QGradientPrivate
|
|
|
|
continue
|
|
|
|
}
|
2024-08-16 23:27:02 +00:00
|
|
|
if ft == "QRgb" {
|
|
|
|
continue
|
|
|
|
}
|
2024-08-14 05:31:30 +00:00
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
foundTypesList = append(foundTypesList, ft)
|
|
|
|
}
|
|
|
|
sort.Strings(foundTypesList)
|
2024-08-14 05:31:30 +00:00
|
|
|
|
|
|
|
return foundTypesList
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitBindingHeader(src *CppParsedHeader, filename string) (string, error) {
|
|
|
|
ret := strings.Builder{}
|
|
|
|
|
|
|
|
includeGuard := "GEN_" + strings.ToUpper(strings.Replace(filename, `.`, `_`, -1))
|
|
|
|
|
|
|
|
ret.WriteString(`#ifndef ` + includeGuard + `
|
|
|
|
#define ` + includeGuard + `
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
foundTypesList := getReferencedTypes(src)
|
|
|
|
|
|
|
|
ret.WriteString("#ifdef __cplusplus\n")
|
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
for _, ft := range foundTypesList {
|
2024-08-17 00:39:45 +00:00
|
|
|
if ft == "QList" || ft == "QString" { // These types are reprojected
|
2024-08-14 06:34:27 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-08-14 05:31:30 +00:00
|
|
|
ret.WriteString(`class ` + ft + ";\n")
|
|
|
|
}
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-14 05:31:30 +00:00
|
|
|
ret.WriteString("#else\n")
|
|
|
|
|
|
|
|
for _, ft := range foundTypesList {
|
2024-08-17 00:39:45 +00:00
|
|
|
if ft == "QList" || ft == "QString" { // These types are reprojected
|
2024-08-14 06:34:27 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-08-14 05:31:30 +00:00
|
|
|
ret.WriteString(`typedef struct ` + ft + " " + ft + ";\n")
|
2024-08-08 07:06:31 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 05:31:30 +00:00
|
|
|
ret.WriteString("#endif\n")
|
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
ret.WriteString("\n")
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, c := range src.Classes {
|
2024-08-07 06:51:51 +00:00
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for i, ctor := range c.Ctors {
|
2024-08-14 05:31:10 +00:00
|
|
|
ret.WriteString(fmt.Sprintf("%s %s_new%s(%s);\n", c.ClassName+"*", c.ClassName, maybeSuffix(i), emitParametersCabi(ctor, "")))
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, m := range c.Methods {
|
2024-08-14 05:31:10 +00:00
|
|
|
ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, c.ClassName+"*")))
|
2024-08-18 05:48:17 +00:00
|
|
|
|
2024-08-18 06:55:28 +00:00
|
|
|
if m.IsSignal && !m.HasHiddenParams {
|
2024-08-23 23:46:26 +00:00
|
|
|
ret.WriteString(fmt.Sprintf("%s %s_connect_%s(%s* self, void* slot);\n", emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), c.ClassName))
|
2024-08-18 05:48:17 +00:00
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:32:57 +00:00
|
|
|
// delete
|
2024-08-20 08:10:57 +00:00
|
|
|
if c.CanDelete {
|
2024-08-18 05:47:19 +00:00
|
|
|
ret.WriteString(fmt.Sprintf("void %s_Delete(%s* self);\n", c.ClassName, c.ClassName))
|
|
|
|
}
|
2024-08-09 22:32:57 +00:00
|
|
|
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
2024-08-09 06:41:29 +00:00
|
|
|
ret.WriteString(
|
|
|
|
`#ifdef __cplusplus
|
2024-08-07 06:51:51 +00:00
|
|
|
} /* extern C */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
`)
|
|
|
|
return ret.String(), nil
|
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
|
2024-08-07 06:51:51 +00:00
|
|
|
ret := strings.Builder{}
|
|
|
|
|
|
|
|
ret.WriteString(`#include "gen_` + filename + `"
|
|
|
|
#include "` + filename + `"
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
2024-08-14 05:31:30 +00:00
|
|
|
for _, ref := range getReferencedTypes(src) {
|
2024-08-18 07:02:14 +00:00
|
|
|
if !ImportHeaderForClass(ref) {
|
2024-08-18 04:08:25 +00:00
|
|
|
continue
|
2024-08-15 07:53:01 +00:00
|
|
|
}
|
2024-08-18 04:08:25 +00:00
|
|
|
|
2024-08-14 05:31:30 +00:00
|
|
|
ret.WriteString(`#include <` + ref + ">\n")
|
|
|
|
}
|
|
|
|
|
2024-08-18 05:48:17 +00:00
|
|
|
ret.WriteString(`
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern void miqt_exec_callback(void* cb, int argc, void* argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2024-08-14 05:31:30 +00:00
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, c := range src.Classes {
|
2024-08-07 06:51:51 +00:00
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for i, ctor := range c.Ctors {
|
2024-08-09 22:35:58 +00:00
|
|
|
preamble, forwarding := emitParametersCABI2CppForwarding(ctor.Parameters)
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(fmt.Sprintf(
|
2024-08-14 05:31:10 +00:00
|
|
|
"%s* %s_new%s(%s) {\n"+
|
2024-08-08 06:54:13 +00:00
|
|
|
"%s"+
|
|
|
|
"\treturn new %s(%s);\n"+
|
|
|
|
"}\n"+
|
|
|
|
"\n",
|
2024-08-10 00:52:45 +00:00
|
|
|
c.ClassName, c.ClassName, maybeSuffix(i), emitParametersCabi(ctor, ""),
|
2024-08-08 06:54:13 +00:00
|
|
|
preamble,
|
|
|
|
c.ClassName, forwarding,
|
2024-08-07 06:51:51 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, m := range c.Methods {
|
2024-08-07 06:51:51 +00:00
|
|
|
// Need to take an extra 'self' parameter
|
|
|
|
|
|
|
|
shouldReturn := "return "
|
2024-08-09 06:41:29 +00:00
|
|
|
afterCall := ""
|
|
|
|
|
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 = ""
|
2024-08-09 06:41:29 +00:00
|
|
|
|
|
|
|
} else if m.ReturnType.ParameterType == "QString" {
|
2024-08-22 09:56:19 +00:00
|
|
|
|
|
|
|
if m.ReturnType.Pointer {
|
|
|
|
// e.g. QTextStream::String()
|
|
|
|
// These are rare, and probably expected to be lightweight references
|
|
|
|
// But, a copy is the best we can project it as
|
|
|
|
// Un-pointer-ify
|
|
|
|
shouldReturn = "QString* ret = "
|
|
|
|
afterCall = "\t// Convert QString pointer from UTF-16 in C++ RAII memory to UTF-8 in manually-managed C memory\n"
|
|
|
|
afterCall += "\tQByteArray b = ret->toUtf8();\n"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
if m.ReturnType.Const {
|
|
|
|
shouldReturn = "const " + shouldReturn
|
|
|
|
}
|
2024-08-09 06:41:29 +00:00
|
|
|
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"
|
2024-08-09 22:34:54 +00:00
|
|
|
|
2024-08-11 04:37:18 +00:00
|
|
|
} else if t, ok := m.ReturnType.QListOf(); ok {
|
2024-08-15 07:50:30 +00:00
|
|
|
|
2024-08-17 00:39:45 +00:00
|
|
|
if t.ParameterType == "QString" {
|
|
|
|
// Combo
|
|
|
|
// "char** _out, int64_t* _out_Lengths, size_t* _out_len")
|
|
|
|
|
|
|
|
shouldReturn = m.ReturnType.ParameterType + " ret = "
|
|
|
|
afterCall += "\t// Convert QStringList from C++ memory to manually-managed C memory\n"
|
|
|
|
afterCall += "\tchar** __out = static_cast<char**>(malloc(sizeof(char*) * ret.length()));\n"
|
2024-08-23 23:45:36 +00:00
|
|
|
afterCall += "\tint* __out_Lengths = static_cast<int*>(malloc(sizeof(int) * ret.length()));\n"
|
2024-08-17 00:39:45 +00:00
|
|
|
afterCall += "\tfor (size_t i = 0, e = ret.length(); i < e; ++i) {\n"
|
|
|
|
afterCall += "\t\t// Convert QString from UTF-16 in C++ RAII memory to UTF-8 in manually-managed C memory\n"
|
|
|
|
afterCall += "\t\tQByteArray b = ret[i].toUtf8();\n"
|
|
|
|
afterCall += "\t\t__out[i] = static_cast<char*>(malloc(b.length()));\n"
|
|
|
|
afterCall += "\t\tmemcpy(__out[i], b.data(), b.length());\n"
|
|
|
|
afterCall += "\t\t__out_Lengths[i] = b.length();\n"
|
|
|
|
afterCall += "\t}\n"
|
|
|
|
afterCall += "\t*_out = __out;\n"
|
|
|
|
afterCall += "\t*_out_Lengths = __out_Lengths;\n"
|
|
|
|
afterCall += "\t*_out_len = ret.length();\n"
|
|
|
|
|
|
|
|
} else if !t.QtClassType() || (t.QtClassType() && t.Pointer) { // QList<int>, QList<QFoo*>
|
2024-08-15 07:50:30 +00:00
|
|
|
|
|
|
|
shouldReturn = m.ReturnType.ParameterType + " ret = "
|
|
|
|
afterCall += "\t// Convert QList<> from C++ memory to manually-managed C memory\n"
|
2024-08-20 08:19:38 +00:00
|
|
|
afterCall += "\t" + t.RenderTypeCabi() + "* __out = static_cast<" + t.RenderTypeCabi() + "*>(malloc(sizeof(" + t.RenderTypeCabi() + ") * ret.length()));\n"
|
2024-08-17 00:39:45 +00:00
|
|
|
afterCall += "\tfor (size_t i = 0, e = ret.length(); i < e; ++i) {\n"
|
2024-08-22 09:56:19 +00:00
|
|
|
if t.Const {
|
|
|
|
nonConst := t // copy
|
|
|
|
nonConst.Const = false
|
|
|
|
afterCall += "\t\t__out[i] = const_cast<" + t.RenderTypeCabi() + ">(ret[i]);\n"
|
|
|
|
} else {
|
|
|
|
afterCall += "\t\t__out[i] = ret[i];\n"
|
|
|
|
}
|
2024-08-15 07:50:30 +00:00
|
|
|
afterCall += "\t}\n"
|
2024-08-17 00:39:45 +00:00
|
|
|
afterCall += "\t*_out = __out;\n"
|
2024-08-15 07:50:30 +00:00
|
|
|
afterCall += "\t*_out_len = ret.length();\n"
|
|
|
|
|
|
|
|
} else { // QList<QFoo>
|
|
|
|
|
|
|
|
shouldReturn = m.ReturnType.ParameterType + " ret = "
|
|
|
|
afterCall += "\t// Convert QList<> from C++ memory to manually-managed C memory of copy-constructed pointers\n"
|
2024-08-20 08:19:38 +00:00
|
|
|
afterCall += "\t" + t.RenderTypeCabi() + "** __out = static_cast<" + t.RenderTypeCabi() + "**>(malloc(sizeof(" + t.RenderTypeCabi() + "**) * ret.length()));\n"
|
2024-08-17 00:39:45 +00:00
|
|
|
afterCall += "\tfor (size_t i = 0, e = ret.length(); i < e; ++i) {\n"
|
|
|
|
afterCall += "\t\t__out[i] = new " + t.ParameterType + "(ret[i]);\n"
|
2024-08-15 07:50:30 +00:00
|
|
|
afterCall += "\t}\n"
|
2024-08-17 00:39:45 +00:00
|
|
|
afterCall += "\t*_out = __out;\n"
|
2024-08-15 07:50:30 +00:00
|
|
|
afterCall += "\t*_out_len = ret.length();\n"
|
|
|
|
|
|
|
|
}
|
2024-08-11 04:37:18 +00:00
|
|
|
|
2024-08-20 08:20:26 +00:00
|
|
|
} else if m.ReturnType.QtClassType() && m.ReturnType.ByRef {
|
|
|
|
// It's a pointer in disguise, just needs one cast
|
|
|
|
shouldReturn = m.ReturnType.RenderTypeQtCpp() + " ret = "
|
|
|
|
afterCall += "\t// Cast returned reference into pointer\n"
|
|
|
|
if m.ReturnType.Const {
|
|
|
|
nonConst := m.ReturnType // copy
|
|
|
|
nonConst.Const = false
|
|
|
|
nonConst.ByRef = false
|
|
|
|
nonConst.Pointer = true
|
|
|
|
afterCall += "\treturn const_cast<" + nonConst.RenderTypeQtCpp() + ">(&ret);\n"
|
|
|
|
} else {
|
|
|
|
afterCall += "\treturn &ret;\n"
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:34:54 +00:00
|
|
|
} else if m.ReturnType.QtClassType() && !m.ReturnType.Pointer {
|
|
|
|
shouldReturn = m.ReturnType.ParameterType + " ret = "
|
|
|
|
afterCall = "\t// Copy-construct value returned type into heap-allocated copy\n"
|
2024-08-14 05:31:10 +00:00
|
|
|
afterCall += "\treturn static_cast<" + m.ReturnType.ParameterType + "*>(new " + m.ReturnType.ParameterType + "(ret));\n"
|
2024-08-09 22:34:54 +00:00
|
|
|
|
2024-08-09 23:46:27 +00:00
|
|
|
} else if m.ReturnType.Const {
|
|
|
|
shouldReturn += "(" + emitReturnTypeCabi(m.ReturnType) + ") "
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:35:58 +00:00
|
|
|
preamble, forwarding := emitParametersCABI2CppForwarding(m.Parameters)
|
2024-08-08 06:54:13 +00:00
|
|
|
|
2024-08-09 23:45:19 +00:00
|
|
|
nativeMethodName := m.MethodName
|
|
|
|
if m.OverrideMethodName != "" {
|
|
|
|
nativeMethodName = m.OverrideMethodName
|
|
|
|
}
|
|
|
|
|
2024-08-18 03:24:04 +00:00
|
|
|
callTarget := "self->"
|
|
|
|
if m.IsStatic {
|
|
|
|
callTarget = c.ClassName + "::"
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
ret.WriteString(fmt.Sprintf(
|
|
|
|
"%s %s_%s(%s) {\n"+
|
|
|
|
"%s"+
|
2024-08-18 03:24:04 +00:00
|
|
|
"\t%s%s%s(%s);\n"+
|
2024-08-09 06:41:29 +00:00
|
|
|
"%s"+
|
2024-08-08 06:54:13 +00:00
|
|
|
"}\n"+
|
|
|
|
"\n",
|
2024-08-14 05:31:10 +00:00
|
|
|
emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, c.ClassName+"*"),
|
2024-08-08 06:54:13 +00:00
|
|
|
preamble,
|
2024-08-18 03:24:04 +00:00
|
|
|
shouldReturn, callTarget, nativeMethodName, forwarding,
|
2024-08-09 06:41:29 +00:00
|
|
|
afterCall,
|
2024-08-07 06:51:51 +00:00
|
|
|
))
|
2024-08-18 05:48:17 +00:00
|
|
|
|
2024-08-18 06:55:28 +00:00
|
|
|
if m.IsSignal && !m.HasHiddenParams {
|
2024-08-18 05:48:17 +00:00
|
|
|
exactSignal := `static_cast<void (` + c.ClassName + `::*)(` + emitParameterTypesCpp(m) + `)>(&` + c.ClassName + `::` + nativeMethodName + `)`
|
|
|
|
|
|
|
|
ret.WriteString(
|
|
|
|
`void ` + c.ClassName + `_connect_` + m.SafeMethodName() + `(` + c.ClassName + `* self, void* slot) {` + "\n" +
|
|
|
|
"\t" + c.ClassName + `::connect(self, ` + exactSignal + `, self, [=](` + emitParametersCpp(m) + `) {` + "\n" +
|
|
|
|
"\t\t" + `miqt_exec_callback(slot, 0, nullptr);` + "\n" +
|
|
|
|
"\t});\n" +
|
|
|
|
"}\n" +
|
|
|
|
"\n",
|
|
|
|
)
|
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
2024-08-09 22:32:57 +00:00
|
|
|
|
|
|
|
// Delete
|
2024-08-20 08:10:57 +00:00
|
|
|
if c.CanDelete {
|
2024-08-18 05:47:19 +00:00
|
|
|
ret.WriteString(fmt.Sprintf(
|
|
|
|
"void %s_Delete(%s* self) {\n"+
|
|
|
|
"\tdelete self;\n"+
|
|
|
|
"}\n"+
|
|
|
|
"\n",
|
|
|
|
c.ClassName, c.ClassName,
|
|
|
|
))
|
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret.String(), nil
|
|
|
|
}
|