]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: more use of IterXXX functions
authorMatthew Dempsky <mdempsky@google.com>
Thu, 10 Mar 2016 09:50:58 +0000 (01:50 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 10 Mar 2016 19:54:03 +0000 (19:54 +0000)
This CL was mostly produced by a one-off automated rewrite tool
looking for statements like "for X := T.Type; X != nil; X = X.Down"
and a few minor variations.

Passes toolstash -cmp.

Change-Id: Ib22705e37d078ef97841ee2e08f60bdbcabb94ad
Reviewed-on: https://go-review.googlesource.com/20520
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
14 files changed:
src/cmd/compile/internal/gc/alg.go
src/cmd/compile/internal/gc/align.go
src/cmd/compile/internal/gc/bexport.go
src/cmd/compile/internal/gc/dcl.go
src/cmd/compile/internal/gc/export.go
src/cmd/compile/internal/gc/fmt.go
src/cmd/compile/internal/gc/gen.go
src/cmd/compile/internal/gc/plive.go
src/cmd/compile/internal/gc/reflect.go
src/cmd/compile/internal/gc/ssa.go
src/cmd/compile/internal/gc/subr.go
src/cmd/compile/internal/gc/type.go
src/cmd/compile/internal/gc/typecheck.go
src/cmd/compile/internal/gc/walk.go

index f61d029946b6c58baca3fa67e8fd9edeb45b39b5..4a080247d383a0ee29f5247ad72c6d7889efc1cb 100644 (file)
@@ -135,7 +135,7 @@ func algtype1(t *Type, bad **Type) int {
                }
 
                ret := AMEM
-               for f := t.Type; f != nil; f = f.Down {
+               for f, it := IterFields(t); f != nil; f = it.Next() {
                        // All fields must be comparable.
                        a := algtype1(f.Type, bad)
                        if a == ANOEQ {
index 3be095076fdded9e4fb4e58e22682323415e0073..2dc3f41212e9540330f686b413319f7854800a99 100644 (file)
@@ -19,7 +19,7 @@ func Rnd(o int64, r int64) int64 {
 
 func offmod(t *Type) {
        o := int32(0)
-       for f := t.Type; f != nil; f = f.Down {
+       for f, it := IterFields(t); f != nil; f = it.Next() {
                if f.Etype != TFIELD {
                        Fatalf("offmod: not TFIELD: %v", Tconv(f, obj.FmtLong))
                }
@@ -40,7 +40,7 @@ func widstruct(errtype *Type, t *Type, o int64, flag int) int64 {
        }
        lastzero := int64(0)
        var w int64
-       for f := t.Type; f != nil; f = f.Down {
+       for f, it := IterFields(t); f != nil; f = it.Next() {
                if f.Etype != TFIELD {
                        Fatalf("widstruct: not TFIELD: %v", Tconv(f, obj.FmtLong))
                }
index b8670004053f170ad580204a6ce88df86cb387f6..6bb14dea2faf7cf96510d7922682fd4e81eeeab5 100644 (file)
@@ -465,7 +465,7 @@ func (p *exporter) typ(t *Type) {
                // TODO(gri) Determine if they are already sorted
                // in which case we can drop this step.
                var methods []*Type
-               for m := t.Method; m != nil; m = m.Down {
+               for m, it := IterMethods(t); m != nil; m = it.Next() {
                        methods = append(methods, m)
                }
                sort.Sort(methodbyname(methods))
@@ -559,7 +559,7 @@ func (p *exporter) fieldList(t *Type) {
        }
 
        p.int(countfield(t))
-       for f := t.Type; f != nil; f = f.Down {
+       for f, it := IterFields(t); f != nil; f = it.Next() {
                p.field(f)
                if p.trace && f.Down != nil {
                        p.tracef("\n")
@@ -592,7 +592,7 @@ func (p *exporter) methodList(t *Type) {
        }
 
        p.int(countfield(t))
-       for m := t.Type; m != nil; m = m.Down {
+       for m, it := IterFields(t); m != nil; m = it.Next() {
                p.method(m)
                if p.trace && m.Down != nil {
                        p.tracef("\n")
@@ -657,7 +657,7 @@ func (p *exporter) paramList(params *Type) {
                n = -n
        }
        p.int(n)
-       for q := params.Type; q != nil; q = q.Down {
+       for q, it := IterFields(params); q != nil; q = it.Next() {
                p.param(q, n)
        }
 }
index 4b6333a38b3a0bc640f24a299bd65e034387b4dc..6f029146437d9272b8d5fdbc77e0ec62ef06badb 100644 (file)
@@ -834,7 +834,7 @@ func tostruct0(t *Type, l []*Node) {
                tp = &f.Down
        }
 
-       for f := t.Type; f != nil && !t.Broke; f = f.Down {
+       for f, it := IterFields(t); f != nil && !t.Broke; f = it.Next() {
                if f.Broke {
                        t.Broke = true
                }
@@ -868,7 +868,7 @@ func tofunargs(l []*Node) *Type {
                tp = &f.Down
        }
 
-       for f := t.Type; f != nil && !t.Broke; f = f.Down {
+       for f, it := IterFields(t); f != nil && !t.Broke; f = it.Next() {
                if f.Broke {
                        t.Broke = true
                }
@@ -961,7 +961,7 @@ func tointerface0(t *Type, l []*Node) *Type {
 
                if n.Left == nil && f.Type.Etype == TINTER {
                        // embedded interface, inline methods
-                       for t1 := f.Type.Type; t1 != nil; t1 = t1.Down {
+                       for t1, it := IterFields(f.Type); t1 != nil; t1 = it.Next() {
                                f = typ(TFIELD)
                                f.Type = t1.Type
                                f.Broke = t1.Broke
@@ -978,7 +978,7 @@ func tointerface0(t *Type, l []*Node) *Type {
                }
        }
 
-       for f := t.Type; f != nil && !t.Broke; f = f.Down {
+       for f, it := IterFields(t); f != nil && !t.Broke; f = it.Next() {
                if f.Broke {
                        t.Broke = true
                }
@@ -1357,7 +1357,7 @@ func addmethod(sf *Sym, t *Type, local bool, nointerface bool) {
        }
 
        if pa.Etype == TSTRUCT {
-               for f := pa.Type; f != nil; f = f.Down {
+               for f, it := IterFields(pa); f != nil; f = it.Next() {
                        if f.Sym == sf {
                                Yyerror("type %v has both field and method named %v", pa, sf)
                                return
@@ -1369,7 +1369,7 @@ func addmethod(sf *Sym, t *Type, local bool, nointerface bool) {
        n.Type = t
 
        var d *Type // last found
-       for f := pa.Method; f != nil; f = f.Down {
+       for f, it := IterMethods(pa); f != nil; f = it.Next() {
                d = f
                if f.Etype != TFIELD {
                        Fatalf("addmethod: not TFIELD: %v", Tconv(f, obj.FmtLong))
index 7c96817d4380b4d0dcc074bbc84ef09e2a0f57ed..9a1f1a6aaf3a41cea3dbe558efefdf8adaa96a88 100644 (file)
@@ -296,7 +296,7 @@ func dumpexporttype(t *Type) {
        }
 
        var m []*Type
-       for f := t.Method; f != nil; f = f.Down {
+       for f, it := IterMethods(t); f != nil; f = it.Next() {
                dumpexporttype(f)
                m = append(m, f)
        }
@@ -584,7 +584,7 @@ func dumpasmhdr() {
                                break
                        }
                        fmt.Fprintf(b, "#define %s__size %d\n", t.Sym.Name, int(t.Width))
-                       for t = t.Type; t != nil; t = t.Down {
+                       for t, it := IterFields(t); t != nil; t = it.Next() {
                                if !isblanksym(t.Sym) {
                                        fmt.Fprintf(b, "#define %s_%s %d\n", n.Sym.Name, t.Sym.Name, int(t.Width))
                                }
index 91c7ec863c85119e686792b9c15dda1d905da1f0..6d7a50a98b4883878798a64a18966c45a5de7c95 100644 (file)
@@ -585,7 +585,7 @@ func typefmt(t *Type, flag int) string {
        case TINTER:
                var buf bytes.Buffer
                buf.WriteString("interface {")
-               for t1 := t.Type; t1 != nil; t1 = t1.Down {
+               for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                        buf.WriteString(" ")
                        switch {
                        case t1.Sym == nil:
@@ -663,14 +663,14 @@ func typefmt(t *Type, flag int) string {
                if t.Funarg {
                        buf.WriteString("(")
                        if fmtmode == FTypeId || fmtmode == FErr { // no argument names on function signature, and no "noescape"/"nosplit" tags
-                               for t1 := t.Type; t1 != nil; t1 = t1.Down {
+                               for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                                        buf.WriteString(Tconv(t1, obj.FmtShort))
                                        if t1.Down != nil {
                                                buf.WriteString(", ")
                                        }
                                }
                        } else {
-                               for t1 := t.Type; t1 != nil; t1 = t1.Down {
+                               for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                                        buf.WriteString(Tconv(t1, 0))
                                        if t1.Down != nil {
                                                buf.WriteString(", ")
@@ -680,7 +680,7 @@ func typefmt(t *Type, flag int) string {
                        buf.WriteString(")")
                } else {
                        buf.WriteString("struct {")
-                       for t1 := t.Type; t1 != nil; t1 = t1.Down {
+                       for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                                buf.WriteString(" ")
                                buf.WriteString(Tconv(t1, obj.FmtLong))
                                if t1.Down != nil {
index 3c991cf150fa7c7a869171ab66c7ae1129aec771..34d2f31d7ca8643ece1a2a6b5cadd0ce9d9d83e8 100644 (file)
@@ -1242,7 +1242,7 @@ func visitComponents(t *Type, startOffset int64, f func(elem *Type, elemOffset i
                        Fatalf("struct not at offset 0")
                }
 
-               for field := t.Type; field != nil; field = field.Down {
+               for field, it := IterFields(t); field != nil; field = it.Next() {
                        if field.Etype != TFIELD {
                                Fatalf("bad struct")
                        }
index 52729613fc1e761736952e3a008eeeceec82f4b9..683989a720950e0349642871dd0b38b2290e54cd 100644 (file)
@@ -951,7 +951,7 @@ func onebitwalktype1(t *Type, xoffset *int64, bv Bvec) {
        case TSTRUCT:
                o := int64(0)
                var fieldoffset int64
-               for t1 := t.Type; t1 != nil; t1 = t1.Down {
+               for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                        fieldoffset = t1.Width
                        *xoffset += fieldoffset - o
                        onebitwalktype1(t1.Type, xoffset, bv)
index 0679853cec6d626fcd170324b2feecc3d7431e28..306f7c7d36d509f5d26a29998dd0ad9629ac0e9d 100644 (file)
@@ -299,7 +299,7 @@ func methods(t *Type) []*Sig {
        // make list of methods for t,
        // generating code if necessary.
        var ms []*Sig
-       for f := mt.Xmethod; f != nil; f = f.Down {
+       for f, it2 := IterAllMethods(mt); f != nil; f = it2.Next() {
                if f.Etype != TFIELD {
                        Fatalf("methods: not field %v", f)
                }
index b86391826aaca51ef40ce3c116dfd97ff33f139b..ff6a3f2a419f9165829461791a430d9bda5b461c 100644 (file)
@@ -2634,7 +2634,7 @@ func canSSAType(t *Type) bool {
                if countfield(t) > ssa.MaxStruct {
                        return false
                }
-               for t1 := t.Type; t1 != nil; t1 = t1.Down {
+               for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                        if !canSSAType(t1.Type) {
                                return false
                        }
@@ -5138,7 +5138,7 @@ func fieldIdx(n *Node) int64 {
        }
 
        var i int64
-       for t1 := t.Type; t1 != nil; t1 = t1.Down {
+       for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                if t1.Etype != TFIELD {
                        panic("non-TFIELD in TSTRUCT")
                }
index 84836d34ec63f8302828c522211044f5c0c14015..6c6816570d617927c0c8aa9f23544b130cb829c5 100644 (file)
@@ -1136,7 +1136,7 @@ func substAny(tp **Type, types *[]*Type) {
                        substAny(t.ResultsP(), types)
 
                case TSTRUCT:
-                       for t = t.Type; t != nil; t = t.Down {
+                       for t, it := IterFields(t); t != nil; t = it.Next() {
                                substAny(&t.Type, types)
                        }
                }
@@ -1219,7 +1219,7 @@ func deep(t *Type) *Type {
                nt.Type = t.Type.Copy()
                xt := nt.Type
 
-               for t = t.Type; t != nil; t = t.Down {
+               for t, it := IterFields(t); t != nil; t = it.Next() {
                        xt.Type = deep(t.Type)
                        xt.Down = t.Down.Copy()
                        xt = xt.Down
@@ -1588,7 +1588,7 @@ func lookdot0(s *Sym, t *Type, save **Type, ignorecase bool) int {
 
        c := 0
        if u.Etype == TSTRUCT || u.Etype == TINTER {
-               for f := u.Type; f != nil; f = f.Down {
+               for f, it := IterFields(u); f != nil; f = it.Next() {
                        if f.Sym == s || (ignorecase && f.Type.Etype == TFUNC && f.Type.Thistuple > 0 && strings.EqualFold(f.Sym.Name, s.Name)) {
                                if save != nil {
                                        *save = f
@@ -1600,7 +1600,7 @@ func lookdot0(s *Sym, t *Type, save **Type, ignorecase bool) int {
 
        u = methtype(t, 0)
        if u != nil {
-               for f := u.Method; f != nil; f = f.Down {
+               for f, it := IterMethods(u); f != nil; f = it.Next() {
                        if f.Embedded == 0 && (f.Sym == s || (ignorecase && strings.EqualFold(f.Sym.Name, s.Name))) {
                                if save != nil {
                                        *save = f
@@ -1645,7 +1645,7 @@ func adddot1(s *Sym, t *Type, d int, save **Type, ignorecase bool) (c int, more
                goto out
        }
 
-       for f := u.Type; f != nil; f = f.Down {
+       for f, it := IterFields(u); f != nil; f = it.Next() {
                if f.Embedded == 0 || f.Sym == nil {
                        continue
                }
@@ -1759,7 +1759,7 @@ func expand0(t *Type, followptr bool) {
 
        if u.Etype == TINTER {
                var sl *Symlink
-               for f := u.Type; f != nil; f = f.Down {
+               for f, it := IterFields(u); f != nil; f = it.Next() {
                        if f.Sym.Flags&SymUniq != 0 {
                                continue
                        }
@@ -1777,7 +1777,7 @@ func expand0(t *Type, followptr bool) {
        u = methtype(t, 0)
        if u != nil {
                var sl *Symlink
-               for f := u.Method; f != nil; f = f.Down {
+               for f, it := IterMethods(u); f != nil; f = it.Next() {
                        if f.Sym.Flags&SymUniq != 0 {
                                continue
                        }
@@ -1811,7 +1811,7 @@ func expand1(t *Type, top, followptr bool) {
                goto out
        }
 
-       for f := u.Type; f != nil; f = f.Down {
+       for f, it := IterFields(u); f != nil; f = it.Next() {
                if f.Embedded == 0 {
                        continue
                }
@@ -1833,7 +1833,7 @@ func expandmeth(t *Type) {
        // mark top-level method symbols
        // so that expand1 doesn't consider them.
        var f *Type
-       for f = t.Method; f != nil; f = f.Down {
+       for f, it := IterMethods(t); f != nil; f = it.Next() {
                f.Sym.Flags |= SymUniq
        }
 
@@ -1855,7 +1855,7 @@ func expandmeth(t *Type) {
                }
        }
 
-       for f = t.Method; f != nil; f = f.Down {
+       for f, it := IterMethods(t); f != nil; f = it.Next() {
                f.Sym.Flags &^= SymUniq
        }
 
@@ -2114,9 +2114,8 @@ func implements(t *Type, iface *Type, m **Type, samename **Type, ptr *int) bool
        // and then do one loop.
 
        if t.Etype == TINTER {
-               var tm *Type
-               for im := iface.Type; im != nil; im = im.Down {
-                       for tm = t.Type; tm != nil; tm = tm.Down {
+               for im, it := IterFields(iface); im != nil; im = it.Next() {
+                       for tm, it2 := IterFields(t); tm != nil; tm = it2.Next() {
                                if tm.Sym == im.Sym {
                                        if Eqtype(tm.Type, im.Type) {
                                                goto found
@@ -2146,7 +2145,7 @@ func implements(t *Type, iface *Type, m **Type, samename **Type, ptr *int) bool
        var imtype *Type
        var followptr bool
        var rcvr *Type
-       for im := iface.Type; im != nil; im = im.Down {
+       for im, it := IterFields(iface); im != nil; im = it.Next() {
                if im.Broke {
                        continue
                }
index 54745453e3e80fe1a37898ae4c8bbdd9ce5565ff..8ba625dc860f5415f905ad8a88d56daa3067665e 100644 (file)
@@ -200,7 +200,29 @@ func IterFields(t *Type) (*Type, Iter) {
        if t.Etype != TSTRUCT && t.Etype != TINTER {
                Fatalf("IterFields: type %v does not have fields", t)
        }
-       i := Iter{x: t.Type}
+       return RawIter(t.Type)
+}
+
+// IterMethods returns the first method in type t's method set
+// and an Iter value to continue iterating across the rest.
+// IterMethods does not include promoted methods.
+func IterMethods(t *Type) (*Type, Iter) {
+       // TODO(mdempsky): Validate t?
+       return RawIter(t.Method)
+}
+
+// IterAllMethods returns the first (possibly promoted) method in type t's
+// method set and an Iter value to continue iterating across the rest.
+func IterAllMethods(t *Type) (*Type, Iter) {
+       // TODO(mdempsky): Validate t?
+       return RawIter(t.Xmethod)
+}
+
+// RawIter returns field t and an Iter value to continue iterating across
+// its successor fields. Most code should instead use one of the IterXXX
+// functions above.
+func RawIter(t *Type) (*Type, Iter) {
+       i := Iter{x: t}
        f := i.Next()
        return f, i
 }
index 45bb1607a143be444b8559b44725b84bbf15a230..fe2560e9f84f8f66d41ee3090a09d3bf33d751d9 100644 (file)
@@ -2372,7 +2372,7 @@ func twoarg(n *Node) bool {
 
 func lookdot1(errnode *Node, s *Sym, t *Type, f *Type, dostrcmp int) *Type {
        var r *Type
-       for ; f != nil; f = f.Down {
+       for f, it := RawIter(f); f != nil; f = it.Next() {
                if dostrcmp != 0 && f.Sym.Name == s.Name {
                        return f
                }
@@ -2577,7 +2577,7 @@ func nokeys(l Nodes) bool {
 }
 
 func hasddd(t *Type) bool {
-       for tl := t.Type; tl != nil; tl = tl.Down {
+       for tl, it := IterFields(t); tl != nil; tl = it.Next() {
                if tl.Isddd {
                        return true
                }
@@ -2590,7 +2590,7 @@ func hasddd(t *Type) bool {
 // TODO decide if we want both (for semantic reasons)
 func downcount(t *Type) int {
        n := 0
-       for tl := t.Type; tl != nil; tl = tl.Down {
+       for tl, it := IterFields(t); tl != nil; tl = it.Next() {
                n++
        }
 
@@ -2629,7 +2629,7 @@ func typecheckaste(op Op, call *Node, isddd bool, tstruct *Type, nl Nodes, desc
 
                                tn := n.Type.Type
                                var why string
-                               for tl := tstruct.Type; tl != nil; tl = tl.Down {
+                               for tl, it2 := IterFields(tstruct); tl != nil; tl = it2.Next() {
                                        if tl.Isddd {
                                                for ; tn != nil; tn = tn.Down {
                                                        if assignop(tn.Type, tl.Type.Type, &why) == 0 {
@@ -2691,7 +2691,7 @@ func typecheckaste(op Op, call *Node, isddd bool, tstruct *Type, nl Nodes, desc
        }
 
        i = 0
-       for tl := tstruct.Type; tl != nil; tl = tl.Down {
+       for tl, it := IterFields(tstruct); tl != nil; tl = it.Next() {
                t = tl.Type
                if tl.Isddd {
                        if isddd {
index 0825442a34dc3dd887f1fd27aa3c9ee149a76f9c..3c397dfc1b792687525870d63abb6c10eb484cdf 100644 (file)
@@ -3083,7 +3083,7 @@ func eqfor(t *Type, needsize *int) *Node {
 
 func countfield(t *Type) int {
        n := 0
-       for t1 := t.Type; t1 != nil; t1 = t1.Down {
+       for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                n++
        }
        return n
@@ -3234,7 +3234,7 @@ func walkcompare(np **Node, init *Nodes) {
                // Inline comparisons.
                var li *Node
                var ri *Node
-               for t1 := t.Type; t1 != nil; t1 = t1.Down {
+               for t1, it := IterFields(t); t1 != nil; t1 = it.Next() {
                        if isblanksym(t1.Sym) {
                                continue
                        }