Source File
typeparam.go
Belonging Package
go/types
// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
// Source: ../../cmd/compile/internal/types2/typeparam.go
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import
// Note: This is a uint32 rather than a uint64 because the
// respective 64 bit atomic instructions are not available
// on all platforms.
var lastID atomic.Uint32
// nextID returns a value increasing monotonically by 1 with
// each call, starting with 1. It may be called concurrently.
func nextID() uint64 { return uint64(lastID.Add(1)) }
// A TypeParam represents a type parameter type.
type TypeParam struct {
check *Checker // for lazy type bound completion
id uint64 // unique id, for debugging only
obj *TypeName // corresponding type name
index int // type parameter index in source order, starting at 0
bound Type // any type, but underlying is eventually *Interface for correct programs (see TypeParam.iface)
}
// NewTypeParam returns a new TypeParam. Type parameters may be set on a Named
// type by calling SetTypeParams. Setting a type parameter on more than one type
// will result in a panic.
//
// The constraint argument can be nil, and set later via SetConstraint. If the
// constraint is non-nil, it must be fully defined.
func ( *TypeName, Type) *TypeParam {
return (*Checker)(nil).newTypeParam(, )
}
// check may be nil
func ( *Checker) ( *TypeName, Type) *TypeParam {
// Always increment lastID, even if it is not used.
:= nextID()
if != nil {
.nextID++
= .nextID
}
:= &TypeParam{check: , id: , obj: , index: -1, bound: }
if .typ == nil {
.typ =
}
// iface may mutate typ.bound, so we must ensure that iface() is called
// at least once before the resulting TypeParam escapes.
if != nil {
.needsCleanup()
} else if != nil {
.iface()
}
return
}
// Obj returns the type name for the type parameter t.
func ( *TypeParam) () *TypeName { return .obj }
// Index returns the index of the type param within its param list, or -1 if
// the type parameter has not yet been bound to a type.
func ( *TypeParam) () int {
return .index
}
// Constraint returns the type constraint specified for t.
func ( *TypeParam) () Type {
return .bound
}
// SetConstraint sets the type constraint for t.
//
// It must be called by users of NewTypeParam after the bound's underlying is
// fully defined, and before using the type parameter in any way other than to
// form other types. Once SetConstraint returns the receiver, t is safe for
// concurrent use.
func ( *TypeParam) ( Type) {
if == nil {
panic("nil constraint")
}
.bound =
// iface may mutate t.bound (if bound is not an interface), so ensure that
// this is done before returning.
.iface()
}
// Underlying returns the [underlying type] of the type parameter t, which is
// the underlying type of its constraint. This type is always an interface.
//
// [underlying type]: https://go.dev/ref/spec#Underlying_types.
func ( *TypeParam) () Type {
return .iface()
}
func ( *TypeParam) () string { return TypeString(, nil) }
// ----------------------------------------------------------------------------
// Implementation
func ( *TypeParam) () {
.iface()
.check = nil
}
// iface returns the constraint interface of t.
func ( *TypeParam) () *Interface {
:= .bound
// determine constraint interface
var *Interface
switch u := under().(type) {
case *Basic:
if !isValid() {
// error is reported elsewhere
return &emptyInterface
}
case *Interface:
if isTypeParam() {
// error is reported in Checker.collectTypeParams
return &emptyInterface
}
=
}
// If we don't have an interface, wrap constraint into an implicit interface.
if == nil {
= NewInterfaceType(nil, []Type{})
.implicit = true
.bound = // update t.bound for next time (optimization)
}
// compute type set if necessary
if .tset == nil {
// pos is used for tracing output; start with the type parameter position.
:= .obj.pos
// use the (original or possibly instantiated) type bound position if we have one
if := asNamed(); != nil {
= .obj.pos
}
computeInterfaceTypeSet(.check, , )
}
return
}
// is calls f with the specific type terms of t's constraint and reports whether
// all calls to f returned true. If there are no specific terms, is
// returns the result of f(nil).
func ( *TypeParam) ( func(*term) bool) bool {
return .iface().typeSet().is()
}
// underIs calls f with the underlying types of the specific type terms
// of t's constraint and reports whether all calls to f returned true.
// If there are no specific terms, underIs returns the result of f(nil).
func ( *TypeParam) ( func(Type) bool) bool {
return .iface().typeSet().underIs()
}
The pages are generated with Golds v0.7.0-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |