From a58ac873a2a18e1b27061bbcb304cce025a80cc5 Mon Sep 17 00:00:00 2001 From: mappu Date: Fri, 11 Apr 2025 22:55:57 +1200 Subject: [PATCH] genbindings/transform: sort QVariant(QVariantList) ctor to end --- cmd/genbindings/transformctors.go | 40 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/cmd/genbindings/transformctors.go b/cmd/genbindings/transformctors.go index 3860024b..984a090d 100644 --- a/cmd/genbindings/transformctors.go +++ b/cmd/genbindings/transformctors.go @@ -4,30 +4,56 @@ import ( "sort" ) -// astTransformConstructorOrder creates a canonical ordering for constructors -// where the 0th entry is any entry taking solely a QWidget* parameter. -// This is so that UIC can safely assume this for types. -// @ref https://github.com/mappu/miqt/issues/46 +// astTransformConstructorOrder creates a canonical ordering for constructors. func astTransformConstructorOrder(parsed *CppParsedHeader) { // QFoo(QWidget* parent); - checkIsDefaultCtor := func(candidate CppMethod) bool { + checkIsDefaultCtor := func(candidate *CppMethod) bool { return len(candidate.Parameters) == 1 && candidate.Parameters[0].ParameterType == "QWidget" && candidate.Parameters[0].Pointer } + // QFoo(QVariantList); + checkIsQVariantListCtor := func(candidate *CppMethod) bool { + if len(candidate.Parameters) != 1 { + return false + } + + if t, _, ok := candidate.Parameters[0].QListOf(); ok && t.ParameterType == "QVariant" { + return true + } + return false + } + // Iterate all classes for i, c := range parsed.Classes { // Sort sort.SliceStable(c.Ctors, func(i, j int) bool { - ic := checkIsDefaultCtor(c.Ctors[i]) - jc := checkIsDefaultCtor(c.Ctors[j]) + // Case 1: Default ctors are moved to front, + // so the 0th entry is any entry taking solely a QWidget* parameter. + // This is so that UIC can safely assume this for types. + // @ref https://github.com/mappu/miqt/issues/46 + ic := checkIsDefaultCtor(&c.Ctors[i]) + jc := checkIsDefaultCtor(&c.Ctors[j]) if ic && !jc { return true } + + // Case 2: QVariantList overload is moved to end + // This softens a compatibility break churning all the QVariant + // constructor ordinals when QVariantList was unblocked. + // @ref https://github.com/mappu/miqt/issues/188 + ic = checkIsQVariantListCtor(&c.Ctors[i]) + jc = checkIsQVariantListCtor(&c.Ctors[j]) + + if !ic && jc { + return true + } + + // Case 3: Normal return false })