genbindings: try to invent better names for function overloads

This commit is contained in:
mappu 2024-08-15 19:50:50 +12:00
parent 2ccc11f57d
commit b89aeb2aa7
3 changed files with 26 additions and 5 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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:]
}