From 3547aa31037c0bb5eedfff6d01209e99daea8297 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 19 Oct 2024 09:23:26 +1300 Subject: [PATCH] genbindings/enums: prevent enum-class clashes, allow some more short enums --- cmd/genbindings/emitgo.go | 32 +++++++++++++++++--------------- cmd/genbindings/intermediate.go | 12 ++++++++++++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/cmd/genbindings/emitgo.go b/cmd/genbindings/emitgo.go index b4a8643c..469d7131 100644 --- a/cmd/genbindings/emitgo.go +++ b/cmd/genbindings/emitgo.go @@ -481,13 +481,23 @@ import "C" nameTest := map[string]string{} nextEnum: for _, e := range src.Enums { + + shortEnumName := e.ShortEnumName() + + // Disallow entry<-->entry collisions for _, ee := range e.Entries { - if other, ok := nameTest[ee.EntryName]; ok { - preventShortNames[e.EnumName] = struct{}{} - preventShortNames[other] = struct{}{} + if other, ok := nameTest[shortEnumName+"::"+ee.EntryName]; ok { + preventShortNames[e.EnumName] = struct{}{} // Our full enum name + preventShortNames[other] = struct{}{} // Their full enum name 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 } - // Fully qualified name of the enum itself - goEnumName := cabiClassName(e.EnumName) + goEnumName := cabiClassName(e.EnumName) // Fully qualified name of the enum itself - // Shorter name, so that enum elements are reachable from the surrounding - // namespace - goEnumShortName := goEnumName + goEnumShortName := goEnumName // Shorter name, so that enum elements are reachable from the surrounding namespace if _, ok := preventShortNames[e.EnumName]; !ok { - // Strip back one single :: pair from the generated variable name - nameParts := strings.Split(e.EnumName, `::`) - if len(nameParts) > 1 { - nameParts = nameParts[0 : len(nameParts)-1] - } - goEnumShortName = cabiClassName(strings.Join(nameParts, `::`)) + goEnumShortName = cabiClassName(e.ShortEnumName()) } ret.WriteString(` diff --git a/cmd/genbindings/intermediate.go b/cmd/genbindings/intermediate.go index 7f384963..d2ee417c 100644 --- a/cmd/genbindings/intermediate.go +++ b/cmd/genbindings/intermediate.go @@ -324,6 +324,18 @@ type CppEnum struct { 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 { ClassName string Abstract bool