Source file src/go/types/typelists.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/typelists.go
     3  
     4  // Copyright 2021 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package types
     9  
    10  import "bytes"
    11  
    12  // TypeParamList holds a list of type parameters.
    13  type TypeParamList struct{ tparams []*TypeParam }
    14  
    15  // Len returns the number of type parameters in the list.
    16  // It is safe to call on a nil receiver.
    17  func (l *TypeParamList) Len() int { return len(l.list()) }
    18  
    19  // At returns the i'th type parameter in the list.
    20  func (l *TypeParamList) At(i int) *TypeParam { return l.tparams[i] }
    21  
    22  // list is for internal use where we expect a []*TypeParam.
    23  // TODO(rfindley): list should probably be eliminated: we can pass around a
    24  // TypeParamList instead.
    25  func (l *TypeParamList) list() []*TypeParam {
    26  	if l == nil {
    27  		return nil
    28  	}
    29  	return l.tparams
    30  }
    31  
    32  func (l *TypeParamList) String() string {
    33  	var buf bytes.Buffer
    34  	buf.WriteByte('[')
    35  	for i, tparam := range l.tparams {
    36  		if i > 0 {
    37  			buf.WriteString(", ")
    38  		}
    39  		WriteType(&buf, tparam, nil)
    40  	}
    41  	buf.WriteByte(']')
    42  	return buf.String()
    43  }
    44  
    45  // TypeList holds a list of types.
    46  type TypeList struct{ types []Type }
    47  
    48  // newTypeList returns a new TypeList with the types in list.
    49  func newTypeList(list []Type) *TypeList {
    50  	if len(list) == 0 {
    51  		return nil
    52  	}
    53  	return &TypeList{list}
    54  }
    55  
    56  // Len returns the number of types in the list.
    57  // It is safe to call on a nil receiver.
    58  func (l *TypeList) Len() int { return len(l.list()) }
    59  
    60  // At returns the i'th type in the list.
    61  func (l *TypeList) At(i int) Type { return l.types[i] }
    62  
    63  // list is for internal use where we expect a []Type.
    64  // TODO(rfindley): list should probably be eliminated: we can pass around a
    65  // TypeList instead.
    66  func (l *TypeList) list() []Type {
    67  	if l == nil {
    68  		return nil
    69  	}
    70  	return l.types
    71  }
    72  
    73  func (l *TypeList) String() string {
    74  	var buf bytes.Buffer
    75  	buf.WriteByte('[')
    76  	for i, t := range l.types {
    77  		if i > 0 {
    78  			buf.WriteString(", ")
    79  		}
    80  		WriteType(&buf, t, nil)
    81  	}
    82  	buf.WriteByte(']')
    83  	return buf.String()
    84  }
    85  
    86  // ----------------------------------------------------------------------------
    87  // Implementation
    88  
    89  func bindTParams(list []*TypeParam) *TypeParamList {
    90  	if len(list) == 0 {
    91  		return nil
    92  	}
    93  	for i, typ := range list {
    94  		if typ.index >= 0 {
    95  			panic("type parameter bound more than once")
    96  		}
    97  		typ.index = i
    98  	}
    99  	return &TypeParamList{tparams: list}
   100  }
   101  

View as plain text