aboutsummaryrefslogtreecommitdiffstats
path: root/sys/decl.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2016-09-29 13:28:03 +0200
committerDmitry Vyukov <dvyukov@google.com>2016-09-29 13:30:08 +0200
commit3ca39dfc4dd8a2dda5b3a9b8c29e295839490af3 (patch)
treed3f4161d07c22e4a08eb0d3edfd61b485ef10579 /sys/decl.go
parentbf21057e7c36c72c1b46aa71bea8dc48509d4c40 (diff)
sys: add padding to structs again
Struct padding was accidentially lost after: 852e3d2eae98a913b7ec91822ba4dc61059a6955 Restore it. Now with tests. Fixes #78
Diffstat (limited to 'sys/decl.go')
-rw-r--r--sys/decl.go47
1 files changed, 41 insertions, 6 deletions
diff --git a/sys/decl.go b/sys/decl.go
index 2cc8f22e5..f792f541c 100644
--- a/sys/decl.go
+++ b/sys/decl.go
@@ -252,11 +252,14 @@ type ArrayType struct {
}
func (t ArrayType) Size() uintptr {
- panic("should not be called")
+ if t.RangeBegin == t.RangeEnd {
+ return t.RangeBegin * t.Type.Size()
+ }
+ return 0 // for trailing embed arrays
}
func (t ArrayType) Align() uintptr {
- panic("should not be called")
+ return t.Type.Align()
}
type PtrType struct {
@@ -282,11 +285,27 @@ type StructType struct {
}
func (t *StructType) Size() uintptr {
- panic("not called")
+ if !t.padded {
+ panic("struct is not padded yet")
+ }
+ var size uintptr
+ for _, f := range t.Fields {
+ size += f.Size()
+ }
+ return size
}
func (t *StructType) Align() uintptr {
- panic("not called")
+ if t.align != 0 {
+ return t.align // overrided by user attribute
+ }
+ var align uintptr
+ for _, f := range t.Fields {
+ if a1 := f.Align(); align < a1 {
+ align = a1
+ }
+ }
+ return align
}
type UnionType struct {
@@ -296,11 +315,26 @@ type UnionType struct {
}
func (t *UnionType) Size() uintptr {
- panic("not called")
+ if t.varlen {
+ panic("union size is not statically known")
+ }
+ size := t.Options[0].Size()
+ for _, opt := range t.Options {
+ if size < opt.Size() {
+ size = opt.Size()
+ }
+ }
+ return size
}
func (t *UnionType) Align() uintptr {
- panic("not called")
+ var align uintptr
+ for _, opt := range t.Options {
+ if a1 := opt.Align(); align < a1 {
+ align = a1
+ }
+ }
+ return align
}
type Dir int
@@ -503,6 +537,7 @@ var (
func init() {
initCalls()
+ initStructFields()
initResources()
initAlign()