Skip to content

Commit

Permalink
codegen: do not add refEnumNames to enumNames
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 14, 2024
1 parent 086586f commit 51c118b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
7 changes: 5 additions & 2 deletions cmd/codegen/arguments_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,18 @@ func getArgWrapper(
pureType = TrimSuffix(pureType, "*")
isPointer = true
}

_, isRefTypedef := context.refTypedefs[pureType]
_, isEnum := context.enumNames[pureType]
_, isRefEnum := context.refEnumNames[pureType]

if goEnumName := pureType; IsEnum(goEnumName, context.enumNames) {
if isEnum || isRefEnum {
srcPkg := context.flags.packageName
if isRefTypedef {
srcPkg = context.flags.refPackageName
}

goType := prefixGoPackage(goEnumName.renameGoIdentifier(context), GoIdentifier(srcPkg), context)
goType := prefixGoPackage(pureType.renameGoIdentifier(context), GoIdentifier(srcPkg), context)

if isPointer {
argDeclaration = fmt.Sprintf("%s *%s", a.Name, goType)
Expand Down
14 changes: 10 additions & 4 deletions cmd/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,26 @@ func loadData(f *flags) (*jsonData, error) {

// this will store json data processed by appropiate pre-rocessors
type Context struct {
// prefix for generated files (prefix_fileType.go)
prefix string

// plain idata loaded from json
funcs []FuncDef
structs []StructDef
enums []EnumDef
typedefs *Typedefs

prefix string

funcNames map[CIdentifier]bool
// ghese fields are filled by parser while it generates code.
funcNames map[CIdentifier]bool // funcs are filled by gencpp
enumNames map[CIdentifier]bool
structNames map[CIdentifier]bool
typedefsNames map[CIdentifier]bool

// contains helper C functions to get/set struct fields
// of array types
arrayIndexGetters map[CIdentifier]CIdentifier

// contains identifiers from other package (usually imgui).
refStructNames map[CIdentifier]bool
refEnumNames map[CIdentifier]bool
refTypedefs map[CIdentifier]bool
Expand Down Expand Up @@ -232,7 +238,7 @@ func main() {
glg.Fatalf("Generating enum names: %v", err)
}

context.enumNames = MergeMaps(SliceToMap(enumNames), context.refEnumNames)
context.enumNames = MergeMaps(SliceToMap(enumNames))

// 1.2. Generate Go typedefs
callbacks, err := GenerateTypedefs(context.typedefs, context.structs, context)
Expand Down
33 changes: 18 additions & 15 deletions cmd/codegen/return_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ func getReturnWrapper(
"void*": simpleR("unsafe.Pointer", "unsafe.Pointer"),
}

isPointer := HasSuffix(t, "*")
pureType := TrimPrefix(TrimSuffix(t, "*"), "const ")
// check if pureType is a declared type (struct or something else from typedefs)
_, isRefStruct := context.refStructNames[pureType]
_, isRefTypedef := context.refTypedefs[pureType]
_, isEnum := context.enumNames[pureType]
_, isRefEnum := context.refEnumNames[pureType]
_, shouldSkipRefTypedef := context.preset.SkipTypedefs[pureType]
_, isStruct := context.structNames[pureType]
isStruct = isStruct || ((isRefStruct || (isRefTypedef && !IsEnum(pureType, context.refEnumNames))) && !shouldSkipRefTypedef)
Expand All @@ -96,25 +99,32 @@ func getReturnWrapper(

_, shouldSkipStruct := context.preset.SkipStructs[pureType]

pureType = TrimPrefix(t, "const ")
switch {
case known:
return w, nil
// case (context.structNames[t] || context.refStructNames[t]) && !shouldSkipStruct(t):
case !HasSuffix(t, "*") && isStruct && !shouldSkipStruct:
case !isPointer && isStruct && !shouldSkipStruct:
return returnWrapper{
returnType: prefixGoPackage(pureType.renameGoIdentifier(context), srcPackage, context),
// this is a small trick as using prefixGoPackage isn't in fact intended to be used in such a way, but it should treat the whole string as a "type" and prefix it correctly
returnStmt: string(prefixGoPackage(GoIdentifier(fmt.Sprintf("*New%sFromC(func() *C.%s {result := %%s; return &result}())", pureType.renameGoIdentifier(context), pureType)), srcPackage, context)),
CType: GoIdentifier(fmt.Sprintf("C.%s", pureType)),
}, nil
case IsEnum(t, context.enumNames):
case isEnum || isRefEnum:
goType := prefixGoPackage(pureType.renameGoIdentifier(context), srcPackage, context)
return returnWrapper{
returnType: goType,
returnStmt: fmt.Sprintf("%s(%%s)", goType),
CType: GoIdentifier(fmt.Sprintf("C.%s", pureType)),
}, nil
if isPointer {
return returnWrapper{
returnType: "*" + goType,
returnStmt: fmt.Sprintf("(*%s)(%%s)", goType),
CType: GoIdentifier(fmt.Sprintf("*C.%s", TrimSuffix(pureType, "*"))),
}, nil
} else {
return returnWrapper{
returnType: goType,
returnStmt: fmt.Sprintf("%s(%%s)", goType),
CType: GoIdentifier(fmt.Sprintf("C.%s", pureType)),
}, nil
}
case HasPrefix(t, "ImVector_") &&
!(HasSuffix(t, "*") || HasSuffix(t, "]")):
pureType := CIdentifier(TrimPrefix(t, "ImVector_") + "*")
Expand All @@ -133,13 +143,6 @@ func getReturnWrapper(
returnStmt: fmt.Sprintf("vectors.NewVectorFromC(%%[1]s.Size, %%[1]s.Capacity, %s)", fmt.Sprintf(rw.returnStmt, "%[1]s.Data")),
CType: GoIdentifier(fmt.Sprintf("*C.%s", pureType)),
}, nil
case HasSuffix(t, "*") && IsEnum(TrimSuffix(t, "*"), context.enumNames):
goType := prefixGoPackage("*"+TrimSuffix(pureType, "*").renameGoIdentifier(context), srcPackage, context)
return returnWrapper{
returnType: goType,
returnStmt: fmt.Sprintf("(%s)(%%s)", goType),
CType: GoIdentifier(fmt.Sprintf("*C.%s", TrimSuffix(pureType, "*"))),
}, nil
case HasSuffix(t, "*") && isStruct && !shouldSkipStruct:
goType := prefixGoPackage("*"+TrimSuffix(pureType, "*").renameGoIdentifier(context), srcPackage, context)
return returnWrapper{
Expand Down

0 comments on commit 51c118b

Please sign in to comment.