github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/internal/govendor/subst/export.go (about)

     1  // Package subst is an excerpt from x/tools/go/ssa responsible for performing
     2  // type substitution in types defined in terms of type parameters with provided
     3  // type arguments.
     4  package subst
     5  
     6  import (
     7  	"go/types"
     8  )
     9  
    10  // To simplify future updates of the borrowed code, we minimize modifications
    11  // to it as much as possible. This file implements an exported interface to the
    12  // original code for us to use.
    13  
    14  // Subster performs type parameter substitution.
    15  type Subster struct {
    16  	impl *subster
    17  }
    18  
    19  // New creates a new Subster with a given list of type parameters and matching args.
    20  func New(tc *types.Context, tParams []*types.TypeParam, tArgs []types.Type) *Subster {
    21  	assert(len(tParams) == len(tArgs), "New() argument count must match")
    22  
    23  	if len(tParams) == 0 {
    24  		return nil
    25  	}
    26  
    27  	subst := &subster{
    28  		replacements: make(map[*types.TypeParam]types.Type, len(tParams)),
    29  		cache:        make(map[types.Type]types.Type),
    30  		ctxt:         tc,
    31  		scope:        nil,
    32  		debug:        false,
    33  	}
    34  	for i := 0; i < len(tParams); i++ {
    35  		subst.replacements[tParams[i]] = tArgs[i]
    36  	}
    37  	return &Subster{
    38  		impl: subst,
    39  	}
    40  }
    41  
    42  // Type returns a version of typ with all references to type parameters replaced
    43  // with the corresponding type arguments.
    44  func (s *Subster) Type(typ types.Type) types.Type {
    45  	if s == nil {
    46  		return typ
    47  	}
    48  	return s.impl.typ(typ)
    49  }