cgo: improve typedef/struct/enum support

Typedefs are now Go type aliases. And C.struct_ and C.union_ prefixed
records work correctly now, even when they're not in a typedef.
This commit is contained in:
Ayke van Laethem
2019-04-27 23:06:45 +02:00
committed by Ron Evans
parent 4bd1b9e53d
commit 35af33ead7
5 changed files with 88 additions and 64 deletions
+6 -1
View File
@@ -358,8 +358,10 @@ func (info *fileInfo) addTypedefs() {
for _, name := range names {
typedef := info.typedefs[name]
typeName := "C." + name
isAlias := true
if strings.HasPrefix(name, "_Cgo_") {
typeName = "C." + name[len("_Cgo_"):]
isAlias = false // C.short etc. should not be aliased to the equivalent Go type (not portable)
}
if _, ok := cgoAliases[typeName]; ok {
// This is a type that also exists in Go (defined in stdint.h).
@@ -377,6 +379,9 @@ func (info *fileInfo) addTypedefs() {
},
Type: typedef.typeExpr,
}
if isAlias {
typeSpec.Assign = info.importCPos
}
obj.Decl = typeSpec
gen.Specs = append(gen.Specs, typeSpec)
}
@@ -400,7 +405,7 @@ func (info *fileInfo) addElaboratedTypes() {
sort.Strings(names)
for _, name := range names {
typ := info.elaboratedTypes[name]
typeName := "C.struct_" + name
typeName := "C." + name
obj := &ast.Object{
Kind: ast.Typ,
Name: typeName,