OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Annotate Ref in Prog with C types by parsing gcc debug output. | 5 // Annotate Ref in Prog with C types by parsing gcc debug output. |
6 // Conversion of debug output to Go types. | 6 // Conversion of debug output to Go types. |
7 | 7 |
8 package main | 8 package main |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 1530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1541 } | 1541 } |
1542 } | 1542 } |
1543 } | 1543 } |
1544 | 1544 |
1545 anon := 0 | 1545 anon := 0 |
1546 for _, f := range dt.Field { | 1546 for _, f := range dt.Field { |
1547 if f.ByteOffset > off { | 1547 if f.ByteOffset > off { |
1548 fld = c.pad(fld, f.ByteOffset-off) | 1548 fld = c.pad(fld, f.ByteOffset-off) |
1549 off = f.ByteOffset | 1549 off = f.ByteOffset |
1550 } | 1550 } |
1551 » » t := c.Type(f.Type, pos) | 1551 |
| 1552 » » name := f.Name |
| 1553 » » ft := f.Type |
| 1554 |
| 1555 » » // In godefs or cdefs mode, if this field is a C11 |
| 1556 » » // anonymous union then treat the first field in the |
| 1557 » » // union as the field in the struct. This handles |
| 1558 » » // cases like the glibc <sys/resource.h> file; see |
| 1559 » » // issue 6677. |
| 1560 » » if *godefs || *cdefs { |
| 1561 » » » if st, ok := f.Type.(*dwarf.StructType); ok && name == "
" && st.Kind == "union" && len(st.Field) > 0 && !used[st.Field[0].Name] { |
| 1562 » » » » name = st.Field[0].Name |
| 1563 » » » » ident[name] = name |
| 1564 » » » » ft = st.Field[0].Type |
| 1565 » » » } |
| 1566 » » } |
| 1567 |
| 1568 » » // TODO: Handle fields that are anonymous structs by |
| 1569 » » // promoting the fields of the inner struct. |
| 1570 |
| 1571 » » t := c.Type(ft, pos) |
1552 tgo := t.Go | 1572 tgo := t.Go |
1553 size := t.Size | 1573 size := t.Size |
1554 talign := t.Align | 1574 talign := t.Align |
1555 if f.BitSize > 0 { | 1575 if f.BitSize > 0 { |
1556 if f.BitSize%8 != 0 { | 1576 if f.BitSize%8 != 0 { |
1557 continue | 1577 continue |
1558 } | 1578 } |
1559 size = f.BitSize / 8 | 1579 size = f.BitSize / 8 |
1560 name := tgo.(*ast.Ident).String() | 1580 name := tgo.(*ast.Ident).String() |
1561 if strings.HasPrefix(name, "int") { | 1581 if strings.HasPrefix(name, "int") { |
1562 name = "int" | 1582 name = "int" |
1563 } else { | 1583 } else { |
1564 name = "uint" | 1584 name = "uint" |
1565 } | 1585 } |
1566 tgo = ast.NewIdent(name + fmt.Sprint(f.BitSize)) | 1586 tgo = ast.NewIdent(name + fmt.Sprint(f.BitSize)) |
1567 talign = size | 1587 talign = size |
1568 } | 1588 } |
1569 | 1589 |
1570 if talign > 0 && f.ByteOffset%talign != 0 { | 1590 if talign > 0 && f.ByteOffset%talign != 0 { |
1571 // Drop misaligned fields, the same way we drop integer
bit fields. | 1591 // Drop misaligned fields, the same way we drop integer
bit fields. |
1572 // The goal is to make available what can be made availa
ble. | 1592 // The goal is to make available what can be made availa
ble. |
1573 // Otherwise one bad and unneeded field in an otherwise
okay struct | 1593 // Otherwise one bad and unneeded field in an otherwise
okay struct |
1574 // makes the whole program not compile. Much of the time
these | 1594 // makes the whole program not compile. Much of the time
these |
1575 // structs are in system headers that cannot be correcte
d. | 1595 // structs are in system headers that cannot be correcte
d. |
1576 continue | 1596 continue |
1577 } | 1597 } |
1578 n := len(fld) | 1598 n := len(fld) |
1579 fld = fld[0 : n+1] | 1599 fld = fld[0 : n+1] |
1580 name := f.Name | |
1581 if name == "" { | 1600 if name == "" { |
1582 name = fmt.Sprintf("anon%d", anon) | 1601 name = fmt.Sprintf("anon%d", anon) |
1583 anon++ | 1602 anon++ |
1584 ident[name] = name | 1603 ident[name] = name |
1585 } | 1604 } |
1586 fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[name])}, T
ype: tgo} | 1605 fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[name])}, T
ype: tgo} |
1587 off += size | 1606 off += size |
1588 buf.WriteString(t.C.String()) | 1607 buf.WriteString(t.C.String()) |
1589 buf.WriteString(" ") | 1608 buf.WriteString(" ") |
1590 buf.WriteString(name) | 1609 buf.WriteString(name) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1688 } | 1707 } |
1689 if prefix == "" { | 1708 if prefix == "" { |
1690 prefix = n.Name[:i+1] | 1709 prefix = n.Name[:i+1] |
1691 } else if prefix != n.Name[:i+1] { | 1710 } else if prefix != n.Name[:i+1] { |
1692 return "" | 1711 return "" |
1693 } | 1712 } |
1694 } | 1713 } |
1695 } | 1714 } |
1696 return prefix | 1715 return prefix |
1697 } | 1716 } |
OLD | NEW |