Commit 9892ccff authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/compile: add types.SoleComponent, use in convFuncName

The specialized conversion functions care only
about a type's layout in memory, so e.g.
[1]string is equivalent to string.

Add types.SoleComponent to assist with such use cases,
and use it for the specialized conversion functions.

Increases the number of convTstring calls by ~1%.

Change-Id: I09a392909f2037387b30642781e65f707a048af5
Reviewed-on: https://go-review.googlesource.com/c/148577
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 694cd005
......@@ -398,10 +398,14 @@ func convFuncName(from, to *types.Type) (fnname string, needsaddr bool) {
return "convT32", false
case from.Size() == 8 && from.Align == types.Types[TUINT64].Align && !types.Haspointers(from):
return "convT64", false
case from.IsString():
return "convTstring", false
case from.IsSlice():
return "convTslice", false
}
if sc := from.SoleComponent(); sc != nil {
switch {
case sc.IsString():
return "convTstring", false
case sc.IsSlice():
return "convTslice", false
}
}
switch tkind {
......
......@@ -1395,6 +1395,28 @@ func (t *Type) NumComponents(countBlank componentsIncludeBlankFields) int64 {
return 1
}
// SoleComponent returns the only primitive component in t,
// if there is exactly one. Otherwise, it returns nil.
// Components are counted as in NumComponents, including blank fields.
func (t *Type) SoleComponent() *Type {
switch t.Etype {
case TSTRUCT:
if t.IsFuncArgStruct() {
Fatalf("SoleComponent func arg struct")
}
if t.NumFields() != 1 {
return nil
}
return t.Field(0).Type.SoleComponent()
case TARRAY:
if t.NumElem() != 1 {
return nil
}
return t.Elem().SoleComponent()
}
return t
}
// ChanDir returns the direction of a channel type t.
// The direction will be one of Crecv, Csend, or Cboth.
func (t *Type) ChanDir() ChanDir {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment