genbindings: static methods move to top-level

This commit is contained in:
mappu 2024-08-18 15:24:04 +12:00
parent 123f946d24
commit d8145be2da
4 changed files with 29 additions and 11 deletions

View File

@ -298,6 +298,10 @@ func parseMethod(node map[string]interface{}, mm *CppMethod) error {
} }
} }
if storageClass, ok := node["storageClass"].(string); ok && storageClass == "static" {
mm.IsStatic = true
}
if methodInner, ok := node["inner"].([]interface{}); ok { if methodInner, ok := node["inner"].([]interface{}); ok {
paramCounter := 0 paramCounter := 0
for _, methodObj := range methodInner { for _, methodObj := range methodInner {

View File

@ -69,7 +69,7 @@ func emitReturnTypeCabi(p CppParameter) string {
func emitParametersCabi(m CppMethod, selfType string) string { func emitParametersCabi(m CppMethod, selfType string) string {
tmp := make([]string, 0, len(m.Parameters)+1) tmp := make([]string, 0, len(m.Parameters)+1)
if selfType != "" { if !m.IsStatic && selfType != "" {
tmp = append(tmp, selfType+" self") tmp = append(tmp, selfType+" self")
} }
@ -472,16 +472,21 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
nativeMethodName = m.OverrideMethodName nativeMethodName = m.OverrideMethodName
} }
callTarget := "self->"
if m.IsStatic {
callTarget = c.ClassName + "::"
}
ret.WriteString(fmt.Sprintf( ret.WriteString(fmt.Sprintf(
"%s %s_%s(%s) {\n"+ "%s %s_%s(%s) {\n"+
"%s"+ "%s"+
"\t%sself->%s(%s);\n"+ "\t%s%s%s(%s);\n"+
"%s"+ "%s"+
"}\n"+ "}\n"+
"\n", "\n",
emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, c.ClassName+"*"), emitReturnTypeCabi(m.ReturnType), c.ClassName, m.SafeMethodName(), emitParametersCabi(m, c.ClassName+"*"),
preamble, preamble,
shouldReturn, nativeMethodName, forwarding, shouldReturn, callTarget, nativeMethodName, forwarding,
afterCall, afterCall,
)) ))
} }

View File

@ -109,15 +109,15 @@ func (p CppParameter) parameterTypeCgo() string {
func emitParametersGo(params []CppParameter) string { func emitParametersGo(params []CppParameter) string {
tmp := make([]string, 0, len(params)) tmp := make([]string, 0, len(params))
isArgcArgv := false skipNext := false
for i, p := range params { for i, p := range params {
if i == 0 && IsArgcArgv(params) { if i == 0 && IsArgcArgv(params) {
isArgcArgv = true skipNext = true
tmp = append(tmp, "args []string") tmp = append(tmp, "args []string")
} else if i == 1 && isArgcArgv { } else if skipNext {
// Skip this parameter, already handled // Skip this parameter, already handled
} else { } else {
@ -132,14 +132,16 @@ func emitParametersGo(params []CppParameter) string {
func emitParametersGo2CABIForwarding(m CppMethod) (preamble string, fowarding string) { func emitParametersGo2CABIForwarding(m CppMethod) (preamble string, fowarding string) {
tmp := make([]string, 0, len(m.Parameters)+2) tmp := make([]string, 0, len(m.Parameters)+2)
tmp = append(tmp, "this.h") if !m.IsStatic {
tmp = append(tmp, "this.h")
}
isArgcArgv := false skipNext := false
for i, p := range m.Parameters { for i, p := range m.Parameters {
if i == 0 && IsArgcArgv(m.Parameters) { if i == 0 && IsArgcArgv(m.Parameters) {
isArgcArgv = true skipNext = true
// QApplication constructor. Convert 'args' into Qt's wanted types // QApplication constructor. Convert 'args' into Qt's wanted types
// Qt has a warning in the docs saying these pointers must be valid // Qt has a warning in the docs saying these pointers must be valid
// for the entire lifetype of QApplication, so, malloc + never free // for the entire lifetype of QApplication, so, malloc + never free
@ -154,8 +156,9 @@ func emitParametersGo2CABIForwarding(m CppMethod) (preamble string, fowarding st
tmp = append(tmp, "argc, argv") tmp = append(tmp, "argc, argv")
} else if i == 1 && isArgcArgv { } else if skipNext {
// Skip this parameter, already handled // Skip this parameter, already handled
skipNext = false
} else if p.ParameterType == "QString" { } else if p.ParameterType == "QString" {
// Go: convert string -> char* and len // Go: convert string -> char* and len
@ -359,8 +362,13 @@ import "C"
} }
receiverAndMethod := `(this *` + c.ClassName + `) ` + m.SafeMethodName()
if m.IsStatic {
receiverAndMethod = c.ClassName + `_` + m.SafeMethodName()
}
ret.WriteString(` ret.WriteString(`
func (this *` + c.ClassName + `) ` + m.SafeMethodName() + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` { func ` + receiverAndMethod + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {
` + preamble + ` + preamble +
shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + forwarding + `) shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + forwarding + `)
` + afterword + `} ` + afterword + `}

View File

@ -65,6 +65,7 @@ type CppMethod struct {
OverrideMethodName string // Present only if we changed the target OverrideMethodName string // Present only if we changed the target
ReturnType CppParameter // Name not used ReturnType CppParameter // Name not used
Parameters []CppParameter Parameters []CppParameter
IsStatic bool
} }
func IsArgcArgv(params []CppParameter) bool { func IsArgcArgv(params []CppParameter) bool {