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-08 06:54:13 +00:00
|
|
|
func (p CppParameter) RenderTypeCpp() string {
|
|
|
|
ret := p.ParameterType
|
|
|
|
if p.Pointer || p.ByRef {
|
|
|
|
ret += "*"
|
|
|
|
}
|
|
|
|
return ret // ignore const
|
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
func emitParametersCpp(params []CppParameter, selfType string) string {
|
2024-08-07 06:51:51 +00:00
|
|
|
tmp := make([]string, 0, len(params)+1)
|
|
|
|
|
|
|
|
if selfType != "" {
|
2024-08-08 06:55:47 +00:00
|
|
|
tmp = append(tmp, selfType+" self")
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range params {
|
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-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
|
|
|
|
tmp = append(tmp, "P"+p.ParameterType+" "+p.ParameterName)
|
|
|
|
} else {
|
|
|
|
// RenderTypeCpp renders both pointer+reference as pointers
|
|
|
|
tmp = append(tmp, p.RenderTypeCpp()+" "+p.ParameterName)
|
|
|
|
}
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
return strings.Join(tmp, ", ")
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
func emitParametersCABI2CppForwarding(params []CppParameter, selfType string) (preamble string, forwarding string) {
|
2024-08-07 06:51:51 +00:00
|
|
|
tmp := make([]string, 0, len(params)+1)
|
|
|
|
|
|
|
|
if selfType != "" {
|
|
|
|
tmp = append(tmp, "self")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
preamble += "\tQString " + p.ParameterName + "_QString(" + p.ParameterName + ", " + p.ParameterName + "_Strlen);\n"
|
|
|
|
tmp = append(tmp, p.ParameterName+"_QString")
|
|
|
|
|
|
|
|
} else if p.ByRef {
|
|
|
|
// We changed RenderTypeCpp() to render this as a pointer
|
|
|
|
// Need to dereference so we can pass as reference to the actual Qt C++ function
|
|
|
|
tmp = append(tmp, "*"+p.ParameterName)
|
|
|
|
} 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-07 06:56:14 +00:00
|
|
|
func emitBindingHeader(src *CppParsedHeader, filename string) (string, error) {
|
2024-08-07 06:51:51 +00:00
|
|
|
ret := strings.Builder{}
|
|
|
|
|
2024-08-08 06:54:36 +00:00
|
|
|
includeGuard := "GEN_" + strings.ToUpper(strings.Replace(filename, `.`, `_`, -1))
|
2024-08-07 06:51:51 +00:00
|
|
|
|
|
|
|
ret.WriteString(`#ifndef ` + includeGuard + `
|
|
|
|
#define ` + includeGuard + `
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
2024-08-08 07:06:31 +00:00
|
|
|
// Find all referenced Qt types in this file, and generate PVoid typedefs
|
|
|
|
// for them
|
|
|
|
foundTypes := map[string]struct{}{}
|
|
|
|
for _, c := range src.Classes {
|
|
|
|
foundTypes[c.ClassName] = struct{}{}
|
|
|
|
for _, ctor := range c.Ctors {
|
|
|
|
for _, p := range ctor.Parameters {
|
|
|
|
if p.QtClassType() {
|
|
|
|
foundTypes[p.ParameterType] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, m := range c.Methods {
|
|
|
|
for _, p := range m.Parameters {
|
|
|
|
if p.QtClassType() {
|
|
|
|
foundTypes[p.ParameterType] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.ReturnType.QtClassType() {
|
|
|
|
foundTypes[m.ReturnType.ParameterType] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foundTypesList := make([]string, 0, len(foundTypes))
|
|
|
|
for ft := range foundTypes {
|
|
|
|
foundTypesList = append(foundTypesList, ft)
|
|
|
|
}
|
|
|
|
sort.Strings(foundTypesList)
|
|
|
|
for _, ft := range foundTypesList {
|
|
|
|
ret.WriteString(`typedef void* P` + ft + ";\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ret.WriteString(fmt.Sprintf("P%s %s_new%s(%s);\n", c.ClassName, maybeSuffix(i), emitParametersCpp(ctor.Parameters, "")))
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 06:56:14 +00:00
|
|
|
for _, m := range c.Methods {
|
2024-08-08 05:51:29 +00:00
|
|
|
ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", m.ReturnType.RenderTypeCpp(), c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName)))
|
2024-08-07 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.WriteString(`
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
} /* 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-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-08 06:54:13 +00:00
|
|
|
preamble, forwarding := emitParametersCABI2CppForwarding(ctor.Parameters, "")
|
2024-08-07 06:51:51 +00:00
|
|
|
ret.WriteString(fmt.Sprintf(
|
2024-08-08 06:54:13 +00:00
|
|
|
"P%s %s_new%s(%s) {\n"+
|
|
|
|
"%s"+
|
|
|
|
"\treturn new %s(%s);\n"+
|
|
|
|
"}\n"+
|
|
|
|
"\n",
|
|
|
|
c.ClassName, maybeSuffix(i), emitParametersCpp(ctor.Parameters, ""),
|
|
|
|
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-08 05:51:29 +00:00
|
|
|
if m.ReturnType.ParameterType == "void" {
|
2024-08-07 06:51:51 +00:00
|
|
|
shouldReturn = ""
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:54:13 +00:00
|
|
|
preamble, forwarding := emitParametersCABI2CppForwarding(m.Parameters, c.ClassName)
|
|
|
|
|
|
|
|
ret.WriteString(fmt.Sprintf(
|
|
|
|
"%s %s_%s(%s) {\n"+
|
|
|
|
"%s"+
|
|
|
|
"\t%sstatic_cast<%s*>(self)->%s(%s);\n"+
|
|
|
|
"}\n"+
|
|
|
|
"\n",
|
|
|
|
m.ReturnType.RenderTypeCpp(), c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName),
|
|
|
|
preamble,
|
|
|
|
shouldReturn, c.ClassName, m.MethodName, forwarding,
|
2024-08-07 06:51:51 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.String(), nil
|
|
|
|
}
|