diff options
author | Josh Bleecher Snyder <josharian@gmail.com> | 2018-03-30 12:25:02 -0700 |
---|---|---|
committer | Josh Bleecher Snyder <josharian@gmail.com> | 2018-04-07 21:01:07 +0000 |
commit | bbfae469a1d207a415dd813d799df1ed2dd2d80a (patch) | |
tree | 9b0e0a639daccb7f5d8644022f733b5c178dfbd2 /src/cmd/compile/internal/types/type.go | |
parent | d8b417e00b3f19f7fb47aaa64fe107ed59f6603f (diff) | |
download | go-bbfae469a1d207a415dd813d799df1ed2dd2d80a.tar.gz go-bbfae469a1d207a415dd813d799df1ed2dd2d80a.zip |
cmd/compile: handle blank struct fields in NumComponents
NumComponents is used by racewalk to decide whether reads and writes
might occur to subobjects of an address. For that purpose,
blank fields matter.
It is also used to decide whether to inline == and != for a type.
For that purpose, blank fields may be ignored.
Add a parameter to NumComponents to support this distinction.
While we're here, document NumComponents, as requested in CL 59334.
Change-Id: I8c2021b172edadd6184848a32a74774dde1805c8
Reviewed-on: https://go-review.googlesource.com/103755
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types/type.go')
-rw-r--r-- | src/cmd/compile/internal/types/type.go | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index 87623a2cc3..f5e9237b81 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -1325,7 +1325,20 @@ func (t *Type) SetNumElem(n int64) { at.Bound = n } -func (t *Type) NumComponents() int64 { +type componentsIncludeBlankFields bool + +const ( + IgnoreBlankFields componentsIncludeBlankFields = false + CountBlankFields componentsIncludeBlankFields = true +) + +// NumComponents returns the number of primitive elements that compose t. +// Struct and array types are flattened for the purpose of counting. +// All other types (including string, slice, and interface types) count as one element. +// If countBlank is IgnoreBlankFields, then blank struct fields +// (and their comprised elements) are excluded from the count. +// struct { x, y [3]int } has six components; [10]struct{ x, y string } has twenty. +func (t *Type) NumComponents(countBlank componentsIncludeBlankFields) int64 { switch t.Etype { case TSTRUCT: if t.IsFuncArgStruct() { @@ -1333,11 +1346,14 @@ func (t *Type) NumComponents() int64 { } var n int64 for _, f := range t.FieldSlice() { - n += f.Type.NumComponents() + if countBlank == IgnoreBlankFields && f.Sym.IsBlank() { + continue + } + n += f.Type.NumComponents(countBlank) } return n case TARRAY: - return t.NumElem() * t.Elem().NumComponents() + return t.NumElem() * t.Elem().NumComponents(countBlank) } return 1 } |