Skip to content

Commit

Permalink
CheckSigFuncEx, OverloadType
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 30, 2024
1 parent a2480c4 commit 3692f48
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func TestTypeEx(t *testing.T) {
if fex, ok := typ.(iSubstType); ok {
fex.Obj()
}
if fex, ok := typ.(iOverloadType); ok {
if fex, ok := typ.(OverloadType); ok {
fex.Len()
func() {
defer func() {
Expand Down
14 changes: 11 additions & 3 deletions func_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func IsFunc(t types.Type) bool {

// CheckFuncEx returns if specified function is a FuncEx or not.
func CheckFuncEx(sig *types.Signature) (ext TyFuncEx, ok bool) {
if typ, is := checkSigFuncEx(sig); is {
if typ, is := CheckSigFuncEx(sig); is {
ext, ok = typ.(TyFuncEx)
}
return
Expand All @@ -48,8 +48,10 @@ const (
overloadMethod = "_"
)

// checkSigFuncEx retrun hide recv type from func($overloadArgs ...interface{$overloadMethod()})
func checkSigFuncEx(sig *types.Signature) (types.Type, bool) {
// CheckSigFuncEx retrun hide recv type from func($overloadArgs ...interface{$overloadMethod()})
// The return type can be OverloadType (*TyOverloadFunc, *TyOverloadMethod, *TyOverloadNamed) or
// *TyTemplateRecvMethod.
func CheckSigFuncEx(sig *types.Signature) (types.Type, bool) {
if sig.Params().Len() == 1 {
if param := sig.Params().At(0); param.Name() == overloadArgs {
if typ, ok := param.Type().(*types.Interface); ok && typ.NumMethods() == 1 {
Expand Down Expand Up @@ -100,10 +102,13 @@ func (p *TyOverloadFunc) Underlying() types.Type { return p }
func (p *TyOverloadFunc) String() string { return "TyOverloadFunc" }
func (p *TyOverloadFunc) funcEx() {}

// NewOverloadFunc creates an overload func.
func NewOverloadFunc(pos token.Pos, pkg *types.Package, name string, funcs ...types.Object) *types.Func {
return newFuncEx(pos, pkg, nil, name, &TyOverloadFunc{funcs})
}

// CheckOverloadFunc checks a func is overload func or not.
// Deprecated: please use CheckFuncEx.
func CheckOverloadFunc(sig *types.Signature) (funcs []types.Object, ok bool) {
if t, ok := CheckFuncEx(sig); ok {
if oft, ok := t.(*TyOverloadFunc); ok {
Expand All @@ -127,10 +132,13 @@ func (p *TyOverloadMethod) Underlying() types.Type { return p }
func (p *TyOverloadMethod) String() string { return "TyOverloadMethod" }
func (p *TyOverloadMethod) funcEx() {}

// NewOverloadMethod creates an overload method.
func NewOverloadMethod(typ *types.Named, pos token.Pos, pkg *types.Package, name string, methods ...types.Object) *types.Func {
return newMethodEx(typ, pos, pkg, name, &TyOverloadMethod{methods})
}

// CheckOverloadMethod checks a func is overload method or not.
// Deprecated: please use CheckFuncEx.
func CheckOverloadMethod(sig *types.Signature) (methods []types.Object, ok bool) {
if t, ok := CheckFuncEx(sig); ok {
if oft, ok := t.(*TyOverloadMethod); ok {
Expand Down
10 changes: 5 additions & 5 deletions type_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
// ----------------------------------------------------------------------------

// Go+ overload extended types
type iOverloadType interface {
type OverloadType interface {
At(i int) types.Object
Len() int
}

var (
_ iOverloadType = (*TyOverloadNamed)(nil)
_ iOverloadType = (*TyOverloadFunc)(nil)
_ iOverloadType = (*TyOverloadMethod)(nil)
_ OverloadType = (*TyOverloadNamed)(nil)
_ OverloadType = (*TyOverloadFunc)(nil)
_ OverloadType = (*TyOverloadMethod)(nil)
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -114,7 +114,7 @@ func NewOverloadNamed(pos token.Pos, pkg *types.Package, name string, typs ...*t
// CheckOverloadNamed returns if specified type is a TyOverloadNamed or not.
func CheckOverloadNamed(typ types.Type) (on *TyOverloadNamed, ok bool) {
if sig, is := typ.(*types.Signature); is {
if typ, is := checkSigFuncEx(sig); is {
if typ, is := CheckSigFuncEx(sig); is {
on, ok = typ.(*TyOverloadNamed)
}
}
Expand Down

0 comments on commit 3692f48

Please sign in to comment.