2024-08-11 04:53:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-16 23:25:20 +00:00
|
|
|
"strings"
|
2024-08-11 04:53:58 +00:00
|
|
|
)
|
|
|
|
|
2024-08-25 05:50:14 +00:00
|
|
|
func (p CppParameter) renderTypeForMethod() string {
|
|
|
|
return strings.NewReplacer(" ", "", "::", "").Replace(p.ParameterType)
|
|
|
|
}
|
|
|
|
|
2024-08-11 04:53:58 +00:00
|
|
|
// astTransformOverloads renames methods if another method exists with the same
|
|
|
|
// name.
|
|
|
|
func astTransformOverloads(parsed *CppParsedHeader) {
|
|
|
|
for i, c := range parsed.Classes {
|
|
|
|
|
|
|
|
anyChange := false
|
|
|
|
existing := map[string]struct{}{}
|
|
|
|
for j, m := range c.Methods {
|
|
|
|
|
2024-08-22 07:20:12 +00:00
|
|
|
originalProposal := m.SafeMethodName()
|
|
|
|
proposedName := originalProposal
|
|
|
|
|
|
|
|
if _, ok := existing[proposedName]; !ok {
|
|
|
|
existing[proposedName] = struct{}{}
|
2024-08-11 04:53:58 +00:00
|
|
|
continue // No collision
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collision - rename
|
|
|
|
anyChange = true
|
|
|
|
|
2024-08-15 07:50:50 +00:00
|
|
|
ctr := 1
|
2024-08-11 04:53:58 +00:00
|
|
|
for {
|
2024-08-15 07:50:50 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
|
2024-08-16 23:25:20 +00:00
|
|
|
// If the parameter has a proper name (i.e. not 'l' or 'param1')
|
|
|
|
// then go with that
|
|
|
|
if len(m.Parameters[0].ParameterName) > 1 && !strings.HasPrefix(m.Parameters[0].ParameterName, "param") {
|
2024-08-22 07:20:12 +00:00
|
|
|
proposedName = originalProposal + "With" + titleCase(m.Parameters[0].ParameterName)
|
2024-08-16 23:25:20 +00:00
|
|
|
} else {
|
|
|
|
// Try the type instead
|
2024-08-25 05:50:14 +00:00
|
|
|
proposedName = originalProposal + "With" + titleCase(m.Parameters[0].renderTypeForMethod())
|
2024-08-16 23:25:20 +00:00
|
|
|
}
|
2024-08-15 07:50:50 +00:00
|
|
|
if _, ok := existing[proposedName]; !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2024-08-22 07:20:12 +00:00
|
|
|
proposedName = fmt.Sprintf("%s%d", originalProposal, ctr)
|
2024-08-15 07:50:50 +00:00
|
|
|
if _, ok := existing[proposedName]; !ok {
|
|
|
|
break
|
|
|
|
}
|
2024-08-11 04:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctr++ // Loop until we have a non-colliding name available
|
|
|
|
}
|
|
|
|
|
|
|
|
existing[proposedName] = struct{}{}
|
2024-09-11 06:06:17 +00:00
|
|
|
m.Rename(proposedName)
|
2024-08-11 04:53:58 +00:00
|
|
|
c.Methods[j] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
if anyChange {
|
|
|
|
parsed.Classes[i] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|