From aa3650f01976524ad80b2aad987e2d4f655cbe65 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Tue, 8 Mar 2016 17:45:55 -0800 Subject: [PATCH] cmd/compile: eliminate IterParams It's only used once, so just make the caller responsible for iterating both the receiver and input params. Change-Id: Icb34f3f0cf96e80fbe27f3f49d12eddc26599b92 Reviewed-on: https://go-review.googlesource.com/20454 Reviewed-by: Dave Cheney --- src/cmd/compile/internal/gc/align.go | 14 ++++------ src/cmd/compile/internal/gc/type.go | 39 ++++++++++++---------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/cmd/compile/internal/gc/align.go b/src/cmd/compile/internal/gc/align.go index e11da5022a..35c824b959 100644 --- a/src/cmd/compile/internal/gc/align.go +++ b/src/cmd/compile/internal/gc/align.go @@ -618,15 +618,11 @@ func typeinit() { func Argsize(t *Type) int { var w int64 - for fp, ip := IterFields(t.Results()); fp != nil; fp = ip.Next() { - if x := fp.Width + fp.Type.Width; x > w { - w = x - } - } - - for fp, ip := IterParams(t); fp != nil; fp = ip.Next() { - if x := fp.Width + fp.Type.Width; x > w { - w = x + for _, p := range recvParamsResults { + for f, it := IterFields(p(t)); f != nil; f = it.Next() { + if x := f.Width + f.Type.Width; x > w { + w = x + } } } diff --git a/src/cmd/compile/internal/gc/type.go b/src/cmd/compile/internal/gc/type.go index b18da81445..3fefe3d066 100644 --- a/src/cmd/compile/internal/gc/type.go +++ b/src/cmd/compile/internal/gc/type.go @@ -164,10 +164,10 @@ type Type struct { Lastfn *Node // for usefield } -// Iter provides an abstraction for iterating across struct fields, -// interface methods, and function parameters. +// Iter provides an abstraction for iterating across struct fields and +// interface methods. type Iter struct { - a, b *Type + x *Type } // IterFields returns the first field or method in struct or interface type t @@ -176,35 +176,21 @@ func IterFields(t *Type) (*Type, Iter) { if t.Etype != TSTRUCT && t.Etype != TINTER { Fatalf("IterFields: type %v does not have fields", t) } - i := Iter{a: t.Type} + i := Iter{x: t.Type} f := i.Next() return f, i } -// IterParams returns the first reeiver or input parameter in function type t -// and an Iter value to continue iterating across the rest. -func IterParams(t *Type) (*Type, Iter) { - if t.Etype != TFUNC { - Fatalf("IterParams: type %v does not have params", t) - } - i := Iter{a: t.Recv().Type, b: t.Params().Type} - f := i.Next() - return f, i -} - -// Next returns the next field, method, or parameter, if any. +// Next returns the next field or method, if any. func (i *Iter) Next() *Type { - if i.a == nil { - if i.b == nil { - return nil - } - i.a, i.b = i.b, nil + if i.x == nil { + return nil } - t := i.a + t := i.x if t.Etype != TFIELD { Fatalf("Iter.Next: type %v is not a field", t) } - i.a = t.Down + i.x = t.Down return t } @@ -233,6 +219,13 @@ func (t *Type) Recv() *Type { return *t.RecvP() } func (t *Type) Params() *Type { return *t.ParamsP() } func (t *Type) Results() *Type { return *t.ResultsP() } +// recvParamsResults stores the accessor functions for a function Type's +// receiver, parameters, and result parameters, in that order. +// It can be used to iterate over all of a function's parameter lists. +var recvParamsResults = [3]func(*Type) *Type{ + (*Type).Recv, (*Type).Params, (*Type).Results, +} + func (t *Type) Size() int64 { dowidth(t) return t.Width