Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline #266

Merged
merged 3 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 57 additions & 54 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
return &internal.Elem{
Val: toType(pkg, typ), Type: NewTypeType(typ), Src: src,
}
} else {
} else { // builtin
return toObject(pkg, v, src)
}
case *types.Builtin:
Expand Down Expand Up @@ -596,23 +596,65 @@
}
break
}
if funcs, ok := CheckOverloadMethod(t); ok {
backup := backupArgs(args)
for _, o := range funcs {
mfn := *fn
mfn.Val.(*ast.SelectorExpr).Sel = ident(o.Name())
if (flags & instrFlagOpFunc) != 0 { // from callOpFunc
mfn.Type = o.Type()
} else {
mfn.Type = methodTypeOf(o.Type())
if fex, ok := CheckFuncEx(t); ok {
switch ft := fex.(type) {
case *TyOverloadFunc:
backup := backupArgs(args)
for _, o := range ft.Funcs {
if ret, err = matchFuncCall(pkg, toObject(pkg, o, fn.Src), args, flags); err == nil {
if ret.CVal == nil && isUntyped(pkg, ret.Type) {
ret.CVal = builtinCall(fn, args)
}
return
}
restoreArgs(args, backup)
}
if ret, err = matchFuncCall(pkg, &mfn, args, flags); err == nil {
fn.Val, fn.Type = mfn.Val, mfn.Type
return
return
case *TyOverloadMethod:
backup := backupArgs(args)
for _, o := range ft.Methods {
mfn := *fn
mfn.Val.(*ast.SelectorExpr).Sel = ident(o.Name())
if (flags & instrFlagOpFunc) != 0 { // from callOpFunc
mfn.Type = o.Type()
} else {
mfn.Type = methodTypeOf(o.Type())
}
if ret, err = matchFuncCall(pkg, &mfn, args, flags); err == nil {
fn.Val, fn.Type = mfn.Val, mfn.Type
return
}
restoreArgs(args, backup)
}
return

Check warning on line 629 in ast.go

View check run for this annotation

Codecov / codecov/patch

ast.go#L629

Added line #L629 was not covered by tests
case *TyTemplateRecvMethod:
if mth, ok := fn.Val.(*ast.SelectorExpr); ok {
if recv := denoteRecv(mth); recv != nil {
backup := backupArgs(args)
for i := 0; i < 2; i++ {
tfn := toObject(pkg, ft.Func, nil)
targs := make([]*internal.Elem, len(args)+1)
targ0 := *recv
if i == 1 {
targ0.Val = &ast.UnaryExpr{Op: token.AND, X: targ0.Val}
targ0.Type = types.NewPointer(targ0.Type)
}
targs[0] = &targ0
for j, arg := range args {
targs[j+1] = arg
}
if ret, err = matchFuncCall(pkg, tfn, targs, flags); err == nil {
return
}
if isPointer(targ0.Type) {
break
}
restoreArgs(args, backup)
}
}
}
restoreArgs(args, backup)
fatal("TODO: unmatched TyTemplateRecvMethod")
}
return
} else if IsCSignature(t) {
sig = typesutil.NewSignatureType(nil, nil, nil, t.Params(), t.Results(), t.Variadic())
} else {
Expand All @@ -629,45 +671,6 @@
} else if t.hasApproxType() {
flags |= instrFlagApproxType
}
case *overloadFuncType:
backup := backupArgs(args)
for _, o := range t.funcs {
if ret, err = matchFuncCall(pkg, toObject(pkg, o, fn.Src), args, flags); err == nil {
if ret.CVal == nil && isUntyped(pkg, ret.Type) {
ret.CVal = builtinCall(fn, args)
}
return
}
restoreArgs(args, backup)
}
return
case *templateRecvMethodType:
if mth, ok := fn.Val.(*ast.SelectorExpr); ok {
if recv := denoteRecv(mth); recv != nil {
backup := backupArgs(args)
for i := 0; i < 2; i++ {
tfn := toObject(pkg, t.fn, nil)
targs := make([]*internal.Elem, len(args)+1)
targ0 := *recv
if i == 1 {
targ0.Val = &ast.UnaryExpr{Op: token.AND, X: targ0.Val}
targ0.Type = types.NewPointer(targ0.Type)
}
targs[0] = &targ0
for j, arg := range args {
targs[j+1] = arg
}
if ret, err = matchFuncCall(pkg, tfn, targs, flags); err == nil {
return
}
if isPointer(targ0.Type) {
break
}
restoreArgs(args, backup)
}
}
}
fatal("TODO: unmatched templateRecvMethodType")
case *instructionType:
return t.instr.Call(pkg, args, flags, fn.Src)
case *types.Named:
Expand Down
41 changes: 32 additions & 9 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go/constant"
"go/token"
"go/types"
"log"
"math/big"
"testing"
"unsafe"
Expand Down Expand Up @@ -285,10 +286,6 @@ func TestIsFunc(t *testing.T) {
if !IsFunc(typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)) {
t.Fatal("func() is not func?")
}
fn := types.NewFunc(token.NoPos, nil, "fn", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false))
if !IsFunc(&templateRecvMethodType{fn: fn}) {
t.Fatal("templateRecvMethodType is not func?")
}
if HasAutoProperty(nil) {
t.Fatal("nil has autoprop?")
}
Expand Down Expand Up @@ -476,6 +473,25 @@ func typString(pkg *Package, t types.Type) string {
return b.String()
}

func TestMethodAutoProperty(t *testing.T) {
typs := []types.Type{
tyInt,
sigFuncEx(nil, &TyOverloadFunc{}),
sigFuncEx(nil, &TyTemplateRecvMethod{types.NewParam(0, nil, "", tyInt)}),
}
for _, typ := range typs {
if methodHasAutoProperty(typ, 0) {
t.Fatal("TestMethodAutoProperty:", typ)
}
}
}

func TestIsType(t *testing.T) {
if isType(sigFuncEx(nil, &TyOverloadFunc{})) {
t.Fatal("TestIsType: isType(TyOverloadFunc)")
}
}

func TestUnderlying(t *testing.T) {
subst := &SubstType{}
bfReft := &bfRefType{typ: tyInt}
Expand All @@ -488,8 +504,9 @@ func TestUnderlying(t *testing.T) {
bfReft,
&unboundType{},
&unboundMapElemType{},
&overloadFuncType{},
&templateRecvMethodType{},
&TyOverloadFunc{},
&TyOverloadMethod{},
&TyTemplateRecvMethod{},
&instructionType{},
&TypeType{},
&unboundFuncParam{},
Expand All @@ -510,7 +527,13 @@ func TestUnderlying(t *testing.T) {
t.Fatal("TestUnderlying failed: no error?")
}
}()
typ.Underlying()
log.Println("type:", typ.String())
if fex, ok := typ.(TyFuncEx); ok {
fex.funcEx()
}
if typ.Underlying() == typ {
panic("noop Underlying")
}
}()
}
}
Expand Down Expand Up @@ -798,7 +821,7 @@ func TestCheckSignature(t *testing.T) {
arg := types.NewParam(token.NoPos, pkg, "", sig)
sig2 := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg), nil, false)
o := types.NewFunc(token.NoPos, pkg, "bar", sig2)
if CheckSignature(&templateRecvMethodType{fn: o}, 0, 0) == nil {
if CheckSignature(sigFuncEx(pkg, &TyTemplateRecvMethod{Func: o}), 0, 0) == nil {
t.Fatal("TestCheckSignature failed: CheckSignature == nil")
}

Expand Down Expand Up @@ -836,7 +859,7 @@ func TestCheckSignatures(t *testing.T) {
arg := types.NewParam(token.NoPos, pkg, "", sig)
sig2 := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg), nil, false)
o := types.NewFunc(token.NoPos, pkg, "bar", sig2)
if CheckSignatures(&templateRecvMethodType{fn: o}, 0, 0) == nil {
if CheckSignatures(sigFuncEx(pkg, &TyTemplateRecvMethod{Func: o}), 0, 0) == nil {
t.Fatal("TestCheckSignatures failed: CheckSignatures == nil")
}
sig3 := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg, arg), nil, false)
Expand Down
7 changes: 1 addition & 6 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,13 +1733,8 @@ func (p *CodeBuilder) field(

func methodTypeOf(typ types.Type) types.Type {
sig := typ.(*types.Signature)
switch t := sig.Recv().Type(); t.(type) {
case *overloadFuncType:
// is overload method
if _, ok := CheckFuncEx(sig); ok {
return typ
case *templateRecvMethodType:
// is template recv method
return t
}
return typesutil.NewSignatureType(nil, nil, nil, sig.Params(), sig.Results(), sig.Variadic())
}
Expand Down
Loading