genbindings: move type renderering to CppParameter methods

This commit is contained in:
mappu 2024-08-08 17:51:29 +12:00
parent 4b803eb453
commit 179c37dfe0
3 changed files with 35 additions and 7 deletions

View File

@ -13,7 +13,7 @@ func emitParametersCpp(params []CppParameter, selfType string) string {
} }
for _, p := range params { for _, p := range params {
tmp = append(tmp, p.ParameterName+" "+p.ParameterType) tmp = append(tmp, p.RenderTypeCpp()+" "+p.ParameterName)
} }
return strings.Join(tmp, ", ") return strings.Join(tmp, ", ")
} }
@ -55,7 +55,7 @@ extern "C" {
} }
for _, m := range c.Methods { for _, m := range c.Methods {
ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", m.ReturnType, c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName))) ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", m.ReturnType.RenderTypeCpp(), c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName)))
} }
ret.WriteString("\n") ret.WriteString("\n")
@ -93,11 +93,11 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
// Need to take an extra 'self' parameter // Need to take an extra 'self' parameter
shouldReturn := "return " shouldReturn := "return "
if m.ReturnType == "void" { if m.ReturnType.ParameterType == "void" {
shouldReturn = "" shouldReturn = ""
} }
ret.WriteString(fmt.Sprintf("%s %s_%s(%s) {\n\t%sstatic_cast<%s*>(self)->%s(%s);\n}\n\n", m.ReturnType, c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName), ret.WriteString(fmt.Sprintf("%s %s_%s(%s) {\n\t%sstatic_cast<%s*>(self)->%s(%s);\n}\n\n", m.ReturnType.RenderTypeCpp(), c.ClassName, m.SafeMethodName(), emitParametersCpp(m.Parameters, "P"+c.ClassName),
shouldReturn, c.ClassName, m.MethodName, emitParametersNames(m.Parameters, c.ClassName), shouldReturn, c.ClassName, m.MethodName, emitParametersNames(m.Parameters, c.ClassName),
)) ))
} }

View File

@ -9,7 +9,7 @@ import (
func emitParametersGo(params []CppParameter) string { func emitParametersGo(params []CppParameter) string {
tmp := make([]string, 0, len(params)) tmp := make([]string, 0, len(params))
for _, p := range params { for _, p := range params {
tmp = append(tmp, p.ParameterType+" "+p.ParameterName) tmp = append(tmp, p.ParameterName+" "+p.RenderTypeGo())
} }
return strings.Join(tmp, ", ") return strings.Join(tmp, ", ")
} }
@ -61,14 +61,14 @@ import "C"
// TODO for any known pointer type, call its cPointer() method instead of passing it directly // TODO for any known pointer type, call its cPointer() method instead of passing it directly
shouldReturn := "return " shouldReturn := "return "
returnTypeDecl := m.ReturnType returnTypeDecl := m.ReturnType.ParameterType // FIXME handle byRef/const here too
if returnTypeDecl == "void" { if returnTypeDecl == "void" {
shouldReturn = "" shouldReturn = ""
returnTypeDecl = "" returnTypeDecl = ""
} }
ret.WriteString(` ret.WriteString(`
func (this *` + c.ClassName + `) ` + m.MethodName + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` { func (this *` + c.ClassName + `) ` + m.SafeMethodName() + `(` + emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {
` + shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + emitParametersNames(m.Parameters, c.ClassName) + `) ` + shouldReturn + ` C.` + c.ClassName + `_` + m.SafeMethodName() + `(` + emitParametersNames(m.Parameters, c.ClassName) + `)
} }

View File

@ -12,6 +12,34 @@ type CppParameter struct {
ByRef bool ByRef bool
} }
func (p CppParameter) RenderTypeCpp() string {
ret := ""
if p.ByRef {
ret += "&"
}
ret += p.ParameterType
if p.Pointer {
ret += "*"
}
return ret // ignore const
}
func (p CppParameter) RenderTypeGo() string {
if p.Pointer && p.ParameterType == "char" {
return "string"
}
ret := ""
if p.ByRef || p.Pointer {
/*
if p.ParameterType[0] == 'Q' {
ret += "C.P" // use our void typedef instead
} else {
*/
ret += "*"
}
ret += p.ParameterType
return ret // ignore const
} }
type CppProperty struct { type CppProperty struct {