Source file src/cmd/compile/internal/types2/typelists.go

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

View as plain text