mirror of
https://github.com/golang/go
synced 2024-09-05 07:54:46 +00:00
[dev.typeparams] go/types: move Signature type decl into signature.go (cleanup)
This is a port of CL 332091 to go/types. Notably types.Signature is missing APIs for RParams. This is left for a later CL, when the API is finalized. Change-Id: I70b664d656e8c1e32958a75e1d13eab5f3281bf9 Reviewed-on: https://go-review.googlesource.com/c/go/+/335038 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
b3e7f23a48
commit
e12d43866d
|
@ -11,6 +11,71 @@ import (
|
|||
"go/token"
|
||||
)
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// API
|
||||
|
||||
// A Signature represents a (non-builtin) function or method type.
|
||||
// The receiver is ignored when comparing signatures for identity.
|
||||
type Signature struct {
|
||||
// We need to keep the scope in Signature (rather than passing it around
|
||||
// and store it in the Func Object) because when type-checking a function
|
||||
// literal we call the general type checker which returns a general Type.
|
||||
// We then unpack the *Signature and use the scope for the literal body.
|
||||
rparams []*TypeName // receiver type parameters from left to right, or nil
|
||||
tparams []*TypeName // type parameters from left to right, or nil
|
||||
scope *Scope // function scope, present for package-local signatures
|
||||
recv *Var // nil if not a method
|
||||
params *Tuple // (incoming) parameters from left to right; or nil
|
||||
results *Tuple // (outgoing) results from left to right; or nil
|
||||
variadic bool // true if the last parameter's type is of the form ...T (or string, for append built-in only)
|
||||
}
|
||||
|
||||
// NewSignature returns a new function type for the given receiver, parameters,
|
||||
// and results, either of which may be nil. If variadic is set, the function
|
||||
// is variadic, it must have at least one parameter, and the last parameter
|
||||
// must be of unnamed slice type.
|
||||
func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
|
||||
if variadic {
|
||||
n := params.Len()
|
||||
if n == 0 {
|
||||
panic("types.NewSignature: variadic function must have at least one parameter")
|
||||
}
|
||||
if _, ok := params.At(n - 1).typ.(*Slice); !ok {
|
||||
panic("types.NewSignature: variadic parameter must be of unnamed slice type")
|
||||
}
|
||||
}
|
||||
return &Signature{recv: recv, params: params, results: results, variadic: variadic}
|
||||
}
|
||||
|
||||
// Recv returns the receiver of signature s (if a method), or nil if a
|
||||
// function. It is ignored when comparing signatures for identity.
|
||||
//
|
||||
// For an abstract method, Recv returns the enclosing interface either
|
||||
// as a *Named or an *Interface. Due to embedding, an interface may
|
||||
// contain methods whose receiver type is a different interface.
|
||||
func (s *Signature) Recv() *Var { return s.recv }
|
||||
|
||||
// _TParams returns the type parameters of signature s, or nil.
|
||||
func (s *Signature) TParams() []*TypeName { return s.tparams }
|
||||
|
||||
// _SetTParams sets the type parameters of signature s.
|
||||
func (s *Signature) SetTParams(tparams []*TypeName) { s.tparams = tparams }
|
||||
|
||||
// Params returns the parameters of signature s, or nil.
|
||||
func (s *Signature) Params() *Tuple { return s.params }
|
||||
|
||||
// Results returns the results of signature s, or nil.
|
||||
func (s *Signature) Results() *Tuple { return s.results }
|
||||
|
||||
// Variadic reports whether the signature s is variadic.
|
||||
func (s *Signature) Variadic() bool { return s.variadic }
|
||||
|
||||
func (t *Signature) Underlying() Type { return t }
|
||||
func (t *Signature) String() string { return TypeString(t, nil) }
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Implementation
|
||||
|
||||
// funcType type-checks a function or method type.
|
||||
func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast.FuncType) {
|
||||
check.openScope(ftyp, "function")
|
||||
|
|
|
@ -163,62 +163,6 @@ func (t *Tuple) Len() int {
|
|||
// At returns the i'th variable of tuple t.
|
||||
func (t *Tuple) At(i int) *Var { return t.vars[i] }
|
||||
|
||||
// A Signature represents a (non-builtin) function or method type.
|
||||
// The receiver is ignored when comparing signatures for identity.
|
||||
type Signature struct {
|
||||
// We need to keep the scope in Signature (rather than passing it around
|
||||
// and store it in the Func Object) because when type-checking a function
|
||||
// literal we call the general type checker which returns a general Type.
|
||||
// We then unpack the *Signature and use the scope for the literal body.
|
||||
rparams []*TypeName // receiver type parameters from left to right, or nil
|
||||
tparams []*TypeName // type parameters from left to right, or nil
|
||||
scope *Scope // function scope, present for package-local signatures
|
||||
recv *Var // nil if not a method
|
||||
params *Tuple // (incoming) parameters from left to right; or nil
|
||||
results *Tuple // (outgoing) results from left to right; or nil
|
||||
variadic bool // true if the last parameter's type is of the form ...T (or string, for append built-in only)
|
||||
}
|
||||
|
||||
// NewSignature returns a new function type for the given receiver, parameters,
|
||||
// and results, either of which may be nil. If variadic is set, the function
|
||||
// is variadic, it must have at least one parameter, and the last parameter
|
||||
// must be of unnamed slice type.
|
||||
func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
|
||||
if variadic {
|
||||
n := params.Len()
|
||||
if n == 0 {
|
||||
panic("types.NewSignature: variadic function must have at least one parameter")
|
||||
}
|
||||
if _, ok := params.At(n - 1).typ.(*Slice); !ok {
|
||||
panic("types.NewSignature: variadic parameter must be of unnamed slice type")
|
||||
}
|
||||
}
|
||||
return &Signature{recv: recv, params: params, results: results, variadic: variadic}
|
||||
}
|
||||
|
||||
// Recv returns the receiver of signature s (if a method), or nil if a
|
||||
// function. It is ignored when comparing signatures for identity.
|
||||
//
|
||||
// For an abstract method, Recv returns the enclosing interface either
|
||||
// as a *Named or an *Interface. Due to embedding, an interface may
|
||||
// contain methods whose receiver type is a different interface.
|
||||
func (s *Signature) Recv() *Var { return s.recv }
|
||||
|
||||
// _TParams returns the type parameters of signature s, or nil.
|
||||
func (s *Signature) TParams() []*TypeName { return s.tparams }
|
||||
|
||||
// _SetTParams sets the type parameters of signature s.
|
||||
func (s *Signature) SetTParams(tparams []*TypeName) { s.tparams = tparams }
|
||||
|
||||
// Params returns the parameters of signature s, or nil.
|
||||
func (s *Signature) Params() *Tuple { return s.params }
|
||||
|
||||
// Results returns the results of signature s, or nil.
|
||||
func (s *Signature) Results() *Tuple { return s.results }
|
||||
|
||||
// Variadic reports whether the signature s is variadic.
|
||||
func (s *Signature) Variadic() bool { return s.variadic }
|
||||
|
||||
// An Interface represents an interface type.
|
||||
type Interface struct {
|
||||
obj *TypeName // type name object defining this interface; or nil (for better error messages)
|
||||
|
@ -694,7 +638,6 @@ func (t *Array) Underlying() Type { return t }
|
|||
func (t *Slice) Underlying() Type { return t }
|
||||
func (t *Pointer) Underlying() Type { return t }
|
||||
func (t *Tuple) Underlying() Type { return t }
|
||||
func (t *Signature) Underlying() Type { return t }
|
||||
func (t *Interface) Underlying() Type { return t }
|
||||
func (t *Map) Underlying() Type { return t }
|
||||
func (t *Chan) Underlying() Type { return t }
|
||||
|
@ -709,7 +652,6 @@ func (t *Array) String() string { return TypeString(t, nil) }
|
|||
func (t *Slice) String() string { return TypeString(t, nil) }
|
||||
func (t *Pointer) String() string { return TypeString(t, nil) }
|
||||
func (t *Tuple) String() string { return TypeString(t, nil) }
|
||||
func (t *Signature) String() string { return TypeString(t, nil) }
|
||||
func (t *Interface) String() string { return TypeString(t, nil) }
|
||||
func (t *Map) String() string { return TypeString(t, nil) }
|
||||
func (t *Chan) String() string { return TypeString(t, nil) }
|
||||
|
|
Loading…
Reference in a new issue