| 1 |
// Copyright 2024 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package types |
| 6 |
|
| 7 |
import "iter" |
| 8 |
|
| 9 |
// This file defines go1.23 iterator methods for a variety of data |
| 10 |
// types. They are not mirrored to cmd/compile/internal/types2, as |
| 11 |
// there is no point doing so until the bootstrap compiler it at least |
| 12 |
// go1.23; therefore go1.23-style range statements should not be used |
| 13 |
// in code common to types and types2, though clients of go/types are |
| 14 |
// free to use them. |
| 15 |
|
| 16 |
// Methods returns a go1.23 iterator over all the methods of an |
| 17 |
// interface, ordered by Id. |
| 18 |
// |
| 19 |
// Example: for m := range t.Methods() { ... } |
| 20 |
func (t *Interface) Methods() iter.Seq[*Func] { |
| 21 |
return func(yield func(m *Func) bool) { |
| 22 |
for i := range t.NumMethods() { |
| 23 |
if !yield(t.Method(i)) { |
| 24 |
break |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// ExplicitMethods returns a go1.23 iterator over the explicit methods of |
| 31 |
// an interface, ordered by Id. |
| 32 |
// |
| 33 |
// Example: for m := range t.ExplicitMethods() { ... } |
| 34 |
func (t *Interface) ExplicitMethods() iter.Seq[*Func] { |
| 35 |
return func(yield func(m *Func) bool) { |
| 36 |
for i := range t.NumExplicitMethods() { |
| 37 |
if !yield(t.ExplicitMethod(i)) { |
| 38 |
break |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// EmbeddedTypes returns a go1.23 iterator over the types embedded within an interface. |
| 45 |
// |
| 46 |
// Example: for e := range t.EmbeddedTypes() { ... } |
| 47 |
func (t *Interface) EmbeddedTypes() iter.Seq[Type] { |
| 48 |
return func(yield func(e Type) bool) { |
| 49 |
for i := range t.NumEmbeddeds() { |
| 50 |
if !yield(t.EmbeddedType(i)) { |
| 51 |
break |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Methods returns a go1.23 iterator over the declared methods of a named type. |
| 58 |
// |
| 59 |
// Example: for m := range t.Methods() { ... } |
| 60 |
func (t *Named) Methods() iter.Seq[*Func] { |
| 61 |
return func(yield func(m *Func) bool) { |
| 62 |
for i := range t.NumMethods() { |
| 63 |
if !yield(t.Method(i)) { |
| 64 |
break |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Children returns a go1.23 iterator over the child scopes nested within scope s. |
| 71 |
// |
| 72 |
// Example: for child := range scope.Children() { ... } |
| 73 |
func (s *Scope) Children() iter.Seq[*Scope] { |
| 74 |
return func(yield func(child *Scope) bool) { |
| 75 |
for i := range s.NumChildren() { |
| 76 |
if !yield(s.Child(i)) { |
| 77 |
break |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// Fields returns a go1.23 iterator over the fields of a struct type. |
| 84 |
// |
| 85 |
// Example: for field := range s.Fields() { ... } |
| 86 |
func (s *Struct) Fields() iter.Seq[*Var] { |
| 87 |
return func(yield func(field *Var) bool) { |
| 88 |
for i := range s.NumFields() { |
| 89 |
if !yield(s.Field(i)) { |
| 90 |
break |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Variables returns a go1.23 iterator over the variables of a tuple type. |
| 97 |
// |
| 98 |
// Example: for v := range tuple.Variables() { ... } |
| 99 |
func (t *Tuple) Variables() iter.Seq[*Var] { |
| 100 |
return func(yield func(v *Var) bool) { |
| 101 |
for i := range t.Len() { |
| 102 |
if !yield(t.At(i)) { |
| 103 |
break |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Methods returns a go1.23 iterator over the methods of a method set. |
| 110 |
// |
| 111 |
// Example: for method := range s.Methods() { ... } |
| 112 |
func (s *MethodSet) Methods() iter.Seq[*Selection] { |
| 113 |
return func(yield func(method *Selection) bool) { |
| 114 |
for i := range s.Len() { |
| 115 |
if !yield(s.At(i)) { |
| 116 |
break |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Terms returns a go1.23 iterator over the terms of a union. |
| 123 |
// |
| 124 |
// Example: for term := range union.Terms() { ... } |
| 125 |
func (u *Union) Terms() iter.Seq[*Term] { |
| 126 |
return func(yield func(term *Term) bool) { |
| 127 |
for i := range u.Len() { |
| 128 |
if !yield(u.Term(i)) { |
| 129 |
break |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// TypeParams returns a go1.23 iterator over a list of type parameters. |
| 136 |
// |
| 137 |
// Example: for tparam := range l.TypeParams() { ... } |
| 138 |
func (l *TypeParamList) TypeParams() iter.Seq[*TypeParam] { |
| 139 |
return func(yield func(tparam *TypeParam) bool) { |
| 140 |
for i := range l.Len() { |
| 141 |
if !yield(l.At(i)) { |
| 142 |
break |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Types returns a go1.23 iterator over the elements of a list of types. |
| 149 |
// |
| 150 |
// Example: for t := range l.Types() { ... } |
| 151 |
func (l *TypeList) Types() iter.Seq[Type] { |
| 152 |
return func(yield func(t Type) bool) { |
| 153 |
for i := range l.Len() { |
| 154 |
if !yield(l.At(i)) { |
| 155 |
break |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |