From 7e78b73517919ca10e6f9a2a4850f034196388c8 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 20 Aug 2024 20:16:13 +1200 Subject: [PATCH] genbindings: accurate type alias tracking for static_cast<> overload resolution --- cmd/genbindings/clang2il.go | 3 +++ cmd/genbindings/emitcabi.go | 46 ++++++++++++++------------------- cmd/genbindings/intermediate.go | 1 + 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/cmd/genbindings/clang2il.go b/cmd/genbindings/clang2il.go index cea3db8a..faa3848a 100644 --- a/cmd/genbindings/clang2il.go +++ b/cmd/genbindings/clang2il.go @@ -555,6 +555,7 @@ func parseSingleTypeString(p string) CppParameter { } else if tok == "WId" { // Transform typedef + insert.TypeAlias = tok insert.ParameterType += " uintptr_t" } else if tok == "Q_PID" { @@ -562,6 +563,7 @@ func parseSingleTypeString(p string) CppParameter { // This is a uint64 PID on Linux/mac and a PROCESS_INFORMATION* on Windows // A uintptr should be tolerable for both cases until we do better // @ref https://doc.qt.io/qt-5/qprocess.html#Q_PID-typedef + insert.TypeAlias = tok insert.ParameterType += " uintptr_t" } else if tok == "QStringList" { @@ -571,6 +573,7 @@ func parseSingleTypeString(p string) CppParameter { // Classes ending in --List are usually better represented as a QList // type directly, so that the binding uses proper Go slices // Typedef e.g. QObjectList + insert.TypeAlias = tok switch tok { case "QModelIndexList": // These types are defined as a QList of values diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index a2e298d0..044e61c9 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -70,23 +70,29 @@ func emitReturnTypeCabi(p CppParameter) string { } } +func (p CppParameter) RenderTypeQtCpp() string { + cppType := p.ParameterType + if len(p.TypeAlias) > 0 { + cppType = p.TypeAlias // replace + } + if p.Const { + cppType = "const " + cppType + } + if p.Pointer { + cppType += "*" + } + if p.ByRef { + cppType += "&" + } + + return cppType +} + // emitParametersCpp emits the parameter definitions exactly how Qt C++ defines them. func emitParametersCpp(m CppMethod) string { tmp := make([]string, 0, len(m.Parameters)) for _, p := range m.Parameters { - - cppType := p.ParameterType - if p.Const { - cppType = "const " + cppType - } - if p.Pointer { - cppType += "*" - } - if p.ByRef { - cppType += "&" - } - - tmp = append(tmp, cppType+" "+p.ParameterName) + tmp = append(tmp, p.RenderTypeQtCpp()+" "+p.ParameterName) } return strings.Join(tmp, `, `) @@ -95,19 +101,7 @@ func emitParametersCpp(m CppMethod) string { func emitParameterTypesCpp(m CppMethod) string { tmp := make([]string, 0, len(m.Parameters)) for _, p := range m.Parameters { - - cppType := p.ParameterType - if p.Const { - cppType = "const " + cppType - } - if p.Pointer { - cppType += "*" - } - if p.ByRef { - cppType += "&" - } - - tmp = append(tmp, cppType) + tmp = append(tmp, p.RenderTypeQtCpp()) } return strings.Join(tmp, `, `) diff --git a/cmd/genbindings/intermediate.go b/cmd/genbindings/intermediate.go index 13a2f6a2..ceb8b6a0 100644 --- a/cmd/genbindings/intermediate.go +++ b/cmd/genbindings/intermediate.go @@ -8,6 +8,7 @@ import ( type CppParameter struct { ParameterName string ParameterType string + TypeAlias string // If we rewrote QStringList->QList, this field contains the original QStringList Const bool Pointer bool ByRef bool