genbindings: implement "BecomesNonConstInVersion" quirk support

This commit is contained in:
mappu 2024-10-26 10:41:42 +13:00
parent f7184c2a3f
commit ba423f153a
5 changed files with 36 additions and 4 deletions

View File

@ -458,7 +458,7 @@ nextMethod:
// Once all processing is complete, pass to exceptions for final decision
if err := AllowMethod(mm); err != nil {
if err := AllowMethod(ret.ClassName, mm); err != nil {
if errors.Is(err, ErrTooComplex) {
log.Printf("Skipping method %q with complex type", mm.MethodName)
continue nextMethod
@ -468,6 +468,8 @@ nextMethod:
return CppClass{}, err
}
ApplyQuirks(ret.ClassName, &mm)
ret.Methods = append(ret.Methods, mm)
default:

View File

@ -179,7 +179,7 @@ func AllowSignal(mm CppMethod) bool {
}
}
func AllowMethod(mm CppMethod) error {
func AllowMethod(className string, mm CppMethod) error {
for _, p := range mm.Parameters {
if strings.HasSuffix(p.ParameterType, "Private") {
@ -395,3 +395,9 @@ func LinuxWindowsCompatCheck(p CppParameter) bool {
}
return false
}
func ApplyQuirks(className string, mm *CppMethod) {
if className == "QArrayData" && mm.MethodName == "needsDetach" && mm.IsConst {
mm.BecomesNonConstInVersion = addr("6.7")
}
}

View File

@ -703,6 +703,23 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
m.ReturnType.RenderTypeCabi(),
))
} else if m.BecomesNonConstInVersion != nil {
nonConstCallTarget := "const_cast<" + cClassName + "*>(self)->" + m.CppCallTarget() + "(" + forwarding + ")"
ret.WriteString("" +
m.ReturnType.RenderTypeCabi() + " " + cClassName + "_" + m.SafeMethodName() + "(" + emitParametersCabi(m, ifv(m.IsConst, "const ", "")+cClassName+"*") + ") {\n" +
preamble + "\n" +
"// This method was changed from const to non-const in Qt " + *m.BecomesNonConstInVersion + "\n" +
"#if QT_VERSION < QT_VERSION_CHECK(" + strings.Replace(*m.BecomesNonConstInVersion, `.`, `,`, -1) + ",0)\n" +
emitAssignCppToCabi("\treturn ", m.ReturnType, callTarget) +
"#else\n" +
emitAssignCppToCabi("\treturn ", m.ReturnType, nonConstCallTarget) +
"#endif\n" +
"}\n" +
"\n",
)
} else {
ret.WriteString(fmt.Sprintf(

View File

@ -209,7 +209,10 @@ type CppMethod struct {
IsSignal bool
IsConst bool
HiddenParams []CppParameter // Populated if there is an overload with more parameters
LinuxOnly bool
// Special quirks
LinuxOnly bool
BecomesNonConstInVersion *string // "6,7"
}
func (m CppMethod) CppCallTarget() string {

View File

@ -34,3 +34,7 @@ func ifv[T any](condition bool, trueval T, falseval T) T {
}
return falseval
}
func addr[T any](s T) *T {
return &s
}