genbindings/enums: prevent enum-class clashes, allow some more short enums

This commit is contained in:
mappu 2024-10-19 09:23:26 +13:00
parent f87002914d
commit 3547aa3103
2 changed files with 29 additions and 15 deletions

View File

@ -481,13 +481,23 @@ import "C"
nameTest := map[string]string{} nameTest := map[string]string{}
nextEnum: nextEnum:
for _, e := range src.Enums { for _, e := range src.Enums {
shortEnumName := e.ShortEnumName()
// Disallow entry<-->entry collisions
for _, ee := range e.Entries { for _, ee := range e.Entries {
if other, ok := nameTest[ee.EntryName]; ok { if other, ok := nameTest[shortEnumName+"::"+ee.EntryName]; ok {
preventShortNames[e.EnumName] = struct{}{} preventShortNames[e.EnumName] = struct{}{} // Our full enum name
preventShortNames[other] = struct{}{} preventShortNames[other] = struct{}{} // Their full enum name
continue nextEnum continue nextEnum
} }
nameTest[ee.EntryName] = e.EnumName nameTest[shortEnumName+"::"+ee.EntryName] = e.EnumName
if _, ok := KnownClassnames[shortEnumName+"::"+ee.EntryName]; ok {
preventShortNames[e.EnumName] = struct{}{}
continue nextEnum
}
} }
} }
} }
@ -497,19 +507,11 @@ import "C"
continue // Removed by transformRedundant AST pass continue // Removed by transformRedundant AST pass
} }
// Fully qualified name of the enum itself goEnumName := cabiClassName(e.EnumName) // Fully qualified name of the enum itself
goEnumName := cabiClassName(e.EnumName)
// Shorter name, so that enum elements are reachable from the surrounding goEnumShortName := goEnumName // Shorter name, so that enum elements are reachable from the surrounding namespace
// namespace
goEnumShortName := goEnumName
if _, ok := preventShortNames[e.EnumName]; !ok { if _, ok := preventShortNames[e.EnumName]; !ok {
// Strip back one single :: pair from the generated variable name goEnumShortName = cabiClassName(e.ShortEnumName())
nameParts := strings.Split(e.EnumName, `::`)
if len(nameParts) > 1 {
nameParts = nameParts[0 : len(nameParts)-1]
}
goEnumShortName = cabiClassName(strings.Join(nameParts, `::`))
} }
ret.WriteString(` ret.WriteString(`

View File

@ -324,6 +324,18 @@ type CppEnum struct {
Entries []CppEnumEntry Entries []CppEnumEntry
} }
func (e CppEnum) ShortEnumName() string {
// Strip back one single :: pair from the generated variable name
if nameParts := strings.Split(e.EnumName, `::`); len(nameParts) > 1 {
nameParts = nameParts[0 : len(nameParts)-1]
return strings.Join(nameParts, `::`)
}
// No change
return e.EnumName
}
type CppClass struct { type CppClass struct {
ClassName string ClassName string
Abstract bool Abstract bool