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  // TypeParamList holds a list of type parameters.
     8  type TypeParamList struct{ tparams []*TypeParam }
     9  
    10  // Len returns the number of type parameters in the list.
    11  // It is safe to call on a nil receiver.
    12  func (l *TypeParamList) Len() int { return len(l.list()) }
    13  
    14  // At returns the i'th type parameter in the list.
    15  func (l *TypeParamList) At(i int) *TypeParam { return l.tparams[i] }
    16  
    17  // list is for internal use where we expect a []*TypeParam.
    18  // TODO(rfindley): list should probably be eliminated: we can pass around a
    19  // TypeParamList instead.
    20  func (l *TypeParamList) list() []*TypeParam {
    21  	if l == nil {
    22  		return nil
    23  	}
    24  	return l.tparams
    25  }
    26  
    27  // TypeList holds a list of types.
    28  type TypeList struct{ types []Type }
    29  
    30  // newTypeList returns a new TypeList with the types in list.
    31  func newTypeList(list []Type) *TypeList {
    32  	if len(list) == 0 {
    33  		return nil
    34  	}
    35  	return &TypeList{list}
    36  }
    37  
    38  // Len returns the number of types in the list.
    39  // It is safe to call on a nil receiver.
    40  func (l *TypeList) Len() int { return len(l.list()) }
    41  
    42  // At returns the i'th type in the list.
    43  func (l *TypeList) At(i int) Type { return l.types[i] }
    44  
    45  // list is for internal use where we expect a []Type.
    46  // TODO(rfindley): list should probably be eliminated: we can pass around a
    47  // TypeList instead.
    48  func (l *TypeList) list() []Type {
    49  	if l == nil {
    50  		return nil
    51  	}
    52  	return l.types
    53  }
    54  
    55  // ----------------------------------------------------------------------------
    56  // Implementation
    57  
    58  func bindTParams(list []*TypeParam) *TypeParamList {
    59  	if len(list) == 0 {
    60  		return nil
    61  	}
    62  	for i, typ := range list {
    63  		if typ.index >= 0 {
    64  			panic("type parameter bound more than once")
    65  		}
    66  		typ.index = i
    67  	}
    68  	return &TypeParamList{tparams: list}
    69  }
    70  

View as plain text