cgo: implement C unions

Unions are somewhat hard to implement in Go because they are not a
native type. But it is actually possible with some compiler magic.

This commit inserts a special "C union" field at the start of a struct
to indicate that it is a union. As such a field cannot be written
directly in Go, this is a useful to distinguish structs and unions.
This commit is contained in:
Ayke van Laethem
2019-04-11 23:14:10 +02:00
committed by Ron Evans
parent 536086988c
commit d2b3a5486c
8 changed files with 206 additions and 22 deletions
+4
View File
@@ -170,6 +170,10 @@ func getTypeCodeName(t types.Type) string {
return "slice:" + name + getTypeCodeName(t.Elem())
case *types.Struct:
elems := make([]string, t.NumFields())
if t.NumFields() > 2 && t.Field(0).Name() == "C union" {
// TODO: report this as a normal error instead of panicking.
panic("cgo unions are not allowed in interfaces")
}
for i := 0; i < t.NumFields(); i++ {
elems[i] = getTypeCodeName(t.Field(i).Type())
}