mirror of
https://github.com/mappu/miqt.git
synced 2025-04-04 20:50:22 +00:00
genbindings: merge enums with redundant definitions
This commit is contained in:
parent
7fdccff799
commit
ac2ec49248
@ -94,9 +94,12 @@ func parseHeader(topLevel []interface{}, addNamePrefix string) (*CppParsedHeader
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("processEnum: %w", err)) // A real problem
|
panic(fmt.Errorf("processEnum: %w", err)) // A real problem
|
||||||
}
|
}
|
||||||
if len(en.Entries) > 0 { // e.g. qmetatype's version of QCborSimpleType (the real one is in qcborcommon)
|
|
||||||
|
// n.b. In some cases we may produce multiple "copies" of an enum
|
||||||
|
// (e.g. qcborcommon and qmetatype both define QCborSimpleType)
|
||||||
|
// Allow, but use a transform pass to avoid multiple definitions of
|
||||||
|
// it
|
||||||
ret.Enums = append(ret.Enums, en)
|
ret.Enums = append(ret.Enums, en)
|
||||||
}
|
|
||||||
|
|
||||||
case "VarDecl":
|
case "VarDecl":
|
||||||
// TODO e.g. qmath.h
|
// TODO e.g. qmath.h
|
||||||
|
@ -400,20 +400,27 @@ import "C"
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, e := range src.Enums {
|
for _, e := range src.Enums {
|
||||||
|
if e.EnumName == "" {
|
||||||
|
continue // Removed by transformRedundant AST pass
|
||||||
|
}
|
||||||
|
|
||||||
goEnumName := cabiClassName(e.EnumName)
|
goEnumName := cabiClassName(e.EnumName)
|
||||||
|
|
||||||
ret.WriteString(`
|
ret.WriteString(`
|
||||||
type ` + goEnumName + ` ` + parseSingleTypeString(e.UnderlyingType).RenderTypeGo() + `
|
type ` + goEnumName + ` ` + parseSingleTypeString(e.UnderlyingType).RenderTypeGo() + `
|
||||||
|
|
||||||
const (
|
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
if len(e.Entries) > 0 {
|
||||||
|
|
||||||
|
ret.WriteString("const (\n")
|
||||||
|
|
||||||
for _, ee := range e.Entries {
|
for _, ee := range e.Entries {
|
||||||
ret.WriteString(cabiClassName(goEnumName+"::"+ee.EntryName) + " " + goEnumName + " = " + ee.EntryValue + "\n")
|
ret.WriteString(cabiClassName(goEnumName+"::"+ee.EntryName) + " " + goEnumName + " = " + ee.EntryValue + "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.WriteString("\n)\n\n")
|
ret.WriteString("\n)\n\n")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, c := range src.Classes {
|
for _, c := range src.Classes {
|
||||||
|
|
||||||
|
@ -195,10 +195,6 @@ func CheckComplexity(p CppParameter, isReturnType bool) error {
|
|||||||
"QXmlStreamAttributes", // e.g. qxmlstream.h
|
"QXmlStreamAttributes", // e.g. qxmlstream.h
|
||||||
"QVariantMap", // e.g. qcbormap.h
|
"QVariantMap", // e.g. qcbormap.h
|
||||||
"QVariantHash", // e.g. qcbormap.h
|
"QVariantHash", // e.g. qcbormap.h
|
||||||
"QCborTag", // e.g. qcborstreamreader.h.TODO Needs support for enums
|
|
||||||
"QCborSimpleType", // e.g. qcborstreamreader.h TODO Needs support for enums
|
|
||||||
"QCborKnownTags", // e.g. qcborstreamreader.h TODO Needs support for enums
|
|
||||||
"QCborNegativeInteger", // e.g. qcborstreamreader.h TODO Needs support for enums
|
|
||||||
"QtMsgType", // e.g. qdebug.h TODO Needs support for enums
|
"QtMsgType", // e.g. qdebug.h TODO Needs support for enums
|
||||||
"QTextStreamFunction", // e.g. qdebug.h
|
"QTextStreamFunction", // e.g. qdebug.h
|
||||||
"QFactoryInterface", // qfactoryinterface.h
|
"QFactoryInterface", // qfactoryinterface.h
|
||||||
|
@ -84,6 +84,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var processHeaders []*CppParsedHeader
|
var processHeaders []*CppParsedHeader
|
||||||
|
atr := astTransformRedundant{
|
||||||
|
preserve: make(map[string]*CppEnum),
|
||||||
|
}
|
||||||
|
|
||||||
for _, inputHeader := range includeFiles {
|
for _, inputHeader := range includeFiles {
|
||||||
|
|
||||||
@ -149,6 +152,7 @@ func main() {
|
|||||||
astTransformChildClasses(parsed) // must be first
|
astTransformChildClasses(parsed) // must be first
|
||||||
astTransformOptional(parsed)
|
astTransformOptional(parsed)
|
||||||
astTransformOverloads(parsed)
|
astTransformOverloads(parsed)
|
||||||
|
atr.Process(parsed)
|
||||||
|
|
||||||
// Update global state tracker (AFTER astTransformChildClasses)
|
// Update global state tracker (AFTER astTransformChildClasses)
|
||||||
// Currently, this is only used for inner classes
|
// Currently, this is only used for inner classes
|
||||||
|
31
cmd/genbindings/transformredundant.go
Normal file
31
cmd/genbindings/transformredundant.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// astTransformRedundant merges duplicate enum definitions.
|
||||||
|
type astTransformRedundant struct {
|
||||||
|
preserve map[string]*CppEnum
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *astTransformRedundant) Process(parsed *CppParsedHeader) {
|
||||||
|
|
||||||
|
for i, e := range parsed.Enums {
|
||||||
|
prev, ok := a.preserve[e.EnumName]
|
||||||
|
if !ok {
|
||||||
|
// It's new
|
||||||
|
a.preserve[e.EnumName] = &parsed.Enums[i]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// It's pre-existing
|
||||||
|
if prev.UnderlyingType != e.UnderlyingType {
|
||||||
|
panic("Enum " + e.EnumName + " is defined multiple times with different underlying types")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge entries
|
||||||
|
prev.Entries = append(prev.Entries, e.Entries...)
|
||||||
|
|
||||||
|
// Remove from second matched header
|
||||||
|
// This is difficult to manipulate while preserving pointers, so only
|
||||||
|
// wipe out the name and use that as a signal later on
|
||||||
|
parsed.Enums[i].EnumName = ""
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user