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

     1  // Copyright 2011 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 "sync/atomic"
     8  
     9  // Note: This is a uint32 rather than a uint64 because the
    10  // respective 64 bit atomic instructions are not available
    11  // on all platforms.
    12  var lastID atomic.Uint32
    13  
    14  // nextID returns a value increasing monotonically by 1 with
    15  // each call, starting with 1. It may be called concurrently.
    16  func nextID() uint64 { return uint64(lastID.Add(1)) }
    17  
    18  // A TypeParam represents a type parameter type.
    19  type TypeParam struct {
    20  	check *Checker  // for lazy type bound completion
    21  	id    uint64    // unique id, for debugging only
    22  	obj   *TypeName // corresponding type name
    23  	index int       // type parameter index in source order, starting at 0
    24  	bound Type      // any type, but underlying is eventually *Interface for correct programs (see TypeParam.iface)
    25  }
    26  
    27  // NewTypeParam returns a new TypeParam. Type parameters may be set on a Named
    28  // or Signature type by calling SetTypeParams. Setting a type parameter on more
    29  // than one type will result in a panic.
    30  //
    31  // The constraint argument can be nil, and set later via SetConstraint. If the
    32  // constraint is non-nil, it must be fully defined.
    33  func NewTypeParam(obj *TypeName, constraint Type) *TypeParam {
    34  	return (*Checker)(nil).newTypeParam(obj, constraint)
    35  }
    36  
    37  // check may be nil
    38  func (check *Checker) newTypeParam(obj *TypeName, constraint Type) *TypeParam {
    39  	// Always increment lastID, even if it is not used.
    40  	id := nextID()
    41  	if check != nil {
    42  		check.nextID++
    43  		id = check.nextID
    44  	}
    45  	typ := &TypeParam{check: check, id: id, obj: obj, index: -1, bound: constraint}
    46  	if obj.typ == nil {
    47  		obj.typ = typ
    48  	}
    49  	// iface may mutate typ.bound, so we must ensure that iface() is called
    50  	// at least once before the resulting TypeParam escapes.
    51  	if check != nil {
    52  		check.needsCleanup(typ)
    53  	} else if constraint != nil {
    54  		typ.iface()
    55  	}
    56  	return typ
    57  }
    58  
    59  // Obj returns the type name for the type parameter t.
    60  func (t *TypeParam) Obj() *TypeName { return t.obj }
    61  
    62  // Index returns the index of the type param within its param list, or -1 if
    63  // the type parameter has not yet been bound to a type.
    64  func (t *TypeParam) Index() int {
    65  	return t.index
    66  }
    67  
    68  // Constraint returns the type constraint specified for t.
    69  func (t *TypeParam) Constraint() Type {
    70  	return t.bound
    71  }
    72  
    73  // SetConstraint sets the type constraint for t.
    74  //
    75  // It must be called by users of NewTypeParam after the bound's underlying is
    76  // fully defined, and before using the type parameter in any way other than to
    77  // form other types. Once SetConstraint returns the receiver, t is safe for
    78  // concurrent use.
    79  func (t *TypeParam) SetConstraint(bound Type) {
    80  	if bound == nil {
    81  		panic("nil constraint")
    82  	}
    83  	t.bound = bound
    84  	// iface may mutate t.bound (if bound is not an interface), so ensure that
    85  	// this is done before returning.
    86  	t.iface()
    87  }
    88  
    89  func (t *TypeParam) Underlying() Type {
    90  	return t.iface()
    91  }
    92  
    93  func (t *TypeParam) String() string { return TypeString(t, nil) }
    94  
    95  // ----------------------------------------------------------------------------
    96  // Implementation
    97  
    98  func (t *TypeParam) cleanup() {
    99  	t.iface()
   100  	t.check = nil
   101  }
   102  
   103  // iface returns the constraint interface of t.
   104  func (t *TypeParam) iface() *Interface {
   105  	bound := t.bound
   106  
   107  	// determine constraint interface
   108  	var ityp *Interface
   109  	switch u := under(bound).(type) {
   110  	case *Basic:
   111  		if !isValid(u) {
   112  			// error is reported elsewhere
   113  			return &emptyInterface
   114  		}
   115  	case *Interface:
   116  		if isTypeParam(bound) {
   117  			// error is reported in Checker.collectTypeParams
   118  			return &emptyInterface
   119  		}
   120  		ityp = u
   121  	}
   122  
   123  	// If we don't have an interface, wrap constraint into an implicit interface.
   124  	if ityp == nil {
   125  		ityp = NewInterfaceType(nil, []Type{bound})
   126  		ityp.implicit = true
   127  		t.bound = ityp // update t.bound for next time (optimization)
   128  	}
   129  
   130  	// compute type set if necessary
   131  	if ityp.tset == nil {
   132  		// pos is used for tracing output; start with the type parameter position.
   133  		pos := t.obj.pos
   134  		// use the (original or possibly instantiated) type bound position if we have one
   135  		if n := asNamed(bound); n != nil {
   136  			pos = n.obj.pos
   137  		}
   138  		computeInterfaceTypeSet(t.check, pos, ityp)
   139  	}
   140  
   141  	return ityp
   142  }
   143  
   144  // is calls f with the specific type terms of t's constraint and reports whether
   145  // all calls to f returned true. If there are no specific terms, is
   146  // returns the result of f(nil).
   147  func (t *TypeParam) is(f func(*term) bool) bool {
   148  	return t.iface().typeSet().is(f)
   149  }
   150  
   151  // underIs calls f with the underlying types of the specific type terms
   152  // of t's constraint and reports whether all calls to f returned true.
   153  // If there are no specific terms, underIs returns the result of f(nil).
   154  func (t *TypeParam) underIs(f func(Type) bool) bool {
   155  	return t.iface().typeSet().underIs(f)
   156  }
   157  

View as plain text