From b89aeb2aa7e2af5c54c124da83d7906205dcc94f Mon Sep 17 00:00:00 2001 From: mappu Date: Thu, 15 Aug 2024 19:50:50 +1200 Subject: [PATCH] genbindings: try to invent better names for function overloads --- cmd/genbindings/intermediate.go | 2 +- cmd/genbindings/transformoverload.go | 24 ++++++++++++++++++++---- cmd/genbindings/util.go | 5 +++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/cmd/genbindings/intermediate.go b/cmd/genbindings/intermediate.go index 5cb7e413..66763552 100644 --- a/cmd/genbindings/intermediate.go +++ b/cmd/genbindings/intermediate.go @@ -88,7 +88,7 @@ func (nm CppMethod) SafeMethodName() string { tmp := replacer.Replace(nm.MethodName) // Also make the first letter uppercase so it becomes public in Go - return strings.ToUpper(tmp[0:1]) + tmp[1:] + return titleCase(tmp) } type CppClass struct { diff --git a/cmd/genbindings/transformoverload.go b/cmd/genbindings/transformoverload.go index a1e59972..6f0333b5 100644 --- a/cmd/genbindings/transformoverload.go +++ b/cmd/genbindings/transformoverload.go @@ -26,12 +26,28 @@ func astTransformOverloads(parsed *CppParsedHeader) { rootMethodName = m.MethodName } - ctr := 2 + ctr := 1 var proposedName string for { - proposedName = fmt.Sprintf("%s%d", rootMethodName, ctr) - if _, ok := existing[proposedName]; !ok { - break + + if ctr == 1 { + // Fake special-case check + // If this is a 1-argument function, try and name it FooFrom{Type} + // e.g. NewVariantFromFloat + if len(m.Parameters) == 1 { + + proposedName = rootMethodName + "With" + titleCase(m.Parameters[0].ParameterName) + if _, ok := existing[proposedName]; !ok { + break + } + + } + + } else { + proposedName = fmt.Sprintf("%s%d", rootMethodName, ctr) + if _, ok := existing[proposedName]; !ok { + break + } } ctr++ // Loop until we have a non-colliding name available diff --git a/cmd/genbindings/util.go b/cmd/genbindings/util.go index 0760ef69..0b72c46b 100644 --- a/cmd/genbindings/util.go +++ b/cmd/genbindings/util.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" ) func maybeSuffix(counter int) string { @@ -11,3 +12,7 @@ func maybeSuffix(counter int) string { return fmt.Sprintf("%d", counter+1) } + +func titleCase(s string) string { + return strings.ToUpper(s[0:1]) + s[1:] +}