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 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(atomic.AddUint32(&lastID, 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  // Obj returns the type name for the type parameter t.
    28  func (t *TypeParam) Obj() *TypeName { return t.obj }
    29  
    30  // NewTypeParam returns a new TypeParam. Type parameters may be set on a Named
    31  // or Signature type by calling SetTypeParams. Setting a type parameter on more
    32  // than one type will result in a panic.
    33  //
    34  // The constraint argument can be nil, and set later via SetConstraint. If the
    35  // constraint is non-nil, it must be fully defined.
    36  func NewTypeParam(obj *TypeName, constraint Type) *TypeParam {
    37  	return (*Checker)(nil).newTypeParam(obj, constraint)
    38  }
    39  
    40  // check may be nil
    41  func (check *Checker) newTypeParam(obj *TypeName, constraint Type) *TypeParam {
    42  	// Always increment lastID, even if it is not used.
    43  	id := nextID()
    44  	if check != nil {
    45  		check.nextID++
    46  		id = check.nextID
    47  	}
    48  	typ := &TypeParam{check: check, id: id, obj: obj, index: -1, bound: constraint}
    49  	if obj.typ == nil {
    50  		obj.typ = typ
    51  	}
    52  	// iface may mutate typ.bound, so we must ensure that iface() is called
    53  	// at least once before the resulting TypeParam escapes.
    54  	if check != nil {
    55  		check.needsCleanup(typ)
    56  	} else if constraint != nil {
    57  		typ.iface()
    58  	}
    59  	return typ
    60  }
    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 u == Typ[Invalid] {
   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, _ := bound.(*Named); 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