mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/cgo: given typedef struct S T, make C.T and C.struct_S interchangeable
For incomplete struct S, C.T and C.struct_S were interchangeable in Go 1.2 and earlier, because all incomplete types were interchangeable (even C.struct_S1 and C.struct_S2). CL 76450043, which fixed issue 7409, made different incomplete types different from Go's point of view, so that they were no longer completely interchangeable. However, imprecision about C.T and C.struct_S - really the same underlying C type - is the one behavior enabled by the bug that is most likely to be depended on by existing cgo code. Explicitly allow it, to keep that code working. Fixes #7786. LGTM=iant, r R=golang-codereviews, iant, r CC=golang-codereviews https://golang.org/cl/98580046
This commit is contained in:
parent
94849d5a78
commit
0782ee3ad5
3 changed files with 102 additions and 8 deletions
|
|
@ -1197,12 +1197,12 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
|
|||
return t
|
||||
|
||||
case *dwarf.StructType:
|
||||
if dt.ByteSize < 0 { // opaque struct
|
||||
break
|
||||
}
|
||||
// Convert to Go struct, being careful about alignment.
|
||||
// Have to give it a name to simulate C "struct foo" references.
|
||||
tag := dt.StructName
|
||||
if dt.ByteSize < 0 && tag == "" { // opaque unnamed struct - should not be possible
|
||||
break
|
||||
}
|
||||
if tag == "" {
|
||||
tag = "__" + strconv.Itoa(tagGen)
|
||||
tagGen++
|
||||
|
|
@ -1212,6 +1212,16 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
|
|||
name := c.Ident("_Ctype_" + dt.Kind + "_" + tag)
|
||||
t.Go = name // publish before recursive calls
|
||||
goIdent[name.Name] = name
|
||||
if dt.ByteSize < 0 {
|
||||
// Size calculation in c.Struct/c.Opaque will die with size=-1 (unknown),
|
||||
// so execute the basic things that the struct case would do
|
||||
// other than try to determine a Go representation.
|
||||
tt := *t
|
||||
tt.C = &TypeRepr{"%s %s", []interface{}{dt.Kind, tag}}
|
||||
tt.Go = c.Ident("struct{}")
|
||||
typedef[name.Name] = &tt
|
||||
break
|
||||
}
|
||||
switch dt.Kind {
|
||||
case "class", "union":
|
||||
t.Go = c.Opaque(t.Size)
|
||||
|
|
@ -1264,7 +1274,12 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
|
|||
tt.Go = sub.Go
|
||||
typedef[name.Name] = &tt
|
||||
}
|
||||
if *godefs || *cdefs {
|
||||
|
||||
// If sub.Go.Name is "_Ctype_struct_foo" or "_Ctype_union_foo" or "_Ctype_class_foo",
|
||||
// use that as the Go form for this typedef too, so that the typedef will be interchangeable
|
||||
// with the base type.
|
||||
// In -godefs and -cdefs mode, do this for all typedefs.
|
||||
if isStructUnionClass(sub.Go) || *godefs || *cdefs {
|
||||
t.Go = sub.Go
|
||||
}
|
||||
|
||||
|
|
@ -1327,10 +1342,19 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
|
|||
// be correct, so calling dtype.Size again will produce the correct value.
|
||||
t.Size = dtype.Size()
|
||||
if t.Size < 0 {
|
||||
// Unsized types are [0]byte, unless they're typedefs of other types.
|
||||
// if so, use the name of the typedef for the go name.
|
||||
// Unsized types are [0]byte, unless they're typedefs of other types
|
||||
// or structs with tags.
|
||||
// if so, use the name we've already defined.
|
||||
t.Size = 0
|
||||
if _, ok := dtype.(*dwarf.TypedefType); !ok {
|
||||
switch dt := dtype.(type) {
|
||||
case *dwarf.TypedefType:
|
||||
// ok
|
||||
case *dwarf.StructType:
|
||||
if dt.StructName != "" {
|
||||
break
|
||||
}
|
||||
t.Go = c.Opaque(0)
|
||||
default:
|
||||
t.Go = c.Opaque(0)
|
||||
}
|
||||
if t.C.Empty() {
|
||||
|
|
@ -1347,6 +1371,19 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
|
|||
return t
|
||||
}
|
||||
|
||||
// isStructUnionClass reports whether the type described by the Go syntax x
|
||||
// is a struct, union, or class with a tag.
|
||||
func isStructUnionClass(x ast.Expr) bool {
|
||||
id, ok := x.(*ast.Ident)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
name := id.Name
|
||||
return strings.HasPrefix(name, "_Ctype_struct_") ||
|
||||
strings.HasPrefix(name, "_Ctype_union_") ||
|
||||
strings.HasPrefix(name, "_Ctype_class_")
|
||||
}
|
||||
|
||||
// FuncArg returns a Go type with the same memory layout as
|
||||
// dtype when used as the type of a C function argument.
|
||||
func (c *typeConv) FuncArg(dtype dwarf.Type, pos token.Pos) *Type {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue