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

fix overload methods of generic type #255

Closed
wants to merge 3 commits into from
Closed
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
45 changes: 33 additions & 12 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,10 @@ func (p *CodeBuilder) startStmtAt(stmt ast.Stmt) int {
}

// Usage:
// idx := cb.startStmtAt(stmt)
// ...
// cb.commitStmt(idx)
//
// idx := cb.startStmtAt(stmt)
// ...
// cb.commitStmt(idx)
func (p *CodeBuilder) commitStmt(idx int) {
stmts := p.current.stmts
n := len(stmts) - 1
Expand Down Expand Up @@ -1703,13 +1704,30 @@ func (p *CodeBuilder) field(

func methodTypeOf(typ types.Type) types.Type {
sig := typ.(*types.Signature)
switch t := sig.Recv().Type(); t.(type) {
switch t := sig.Recv().Type().(type) {
case *overloadFuncType:
// is overload method
return typ
case *templateRecvMethodType:
// is template recv method
return t
case *types.Named:
name := sig.Recv().Name()
if strings.HasPrefix(name, gop_overload) {
funcs := strings.Split(name[len(gop_overload):], ";")
oft := &overloadFuncType{}
for i := 0; i < t.NumMethods(); i++ {
name := t.Method(i).Name()
for _, fn := range funcs {
if fn == name {
oft.funcs = append(oft.funcs, t.Method(i))
break
}
}
}
recv := types.NewParam(token.NoPos, sig.Recv().Pkg(), "", oft)
return types.NewSignature(recv, nil, nil, false)
}
}
return types.NewSignature(nil, sig.Params(), sig.Results(), sig.Variadic())
}
Expand Down Expand Up @@ -2059,9 +2077,9 @@ func (p *CodeBuilder) CompareNil(op token.Token, src ...ast.Node) *CodeBuilder {
}

// UnaryOp:
// - cb.UnaryOp(op token.Token)
// - cb.UnaryOp(op token.Token, twoValue bool)
// - cb.UnaryOp(op token.Token, twoValue bool, src ast.Node)
// - cb.UnaryOp(op token.Token)
// - cb.UnaryOp(op token.Token, twoValue bool)
// - cb.UnaryOp(op token.Token, twoValue bool, src ast.Node)
func (p *CodeBuilder) UnaryOp(op token.Token, params ...interface{}) *CodeBuilder {
var src ast.Node
var flags InstrFlags
Expand Down Expand Up @@ -2194,14 +2212,17 @@ func (p *CodeBuilder) Else() *CodeBuilder {
// <pre>
// typeSwitch(name) init; expr typeAssertThen
// type1, type2, ... typeN typeCase(N)
// ...
// end
//
// ...
// end
//
// type1, type2, ... typeM typeCase(M)
// ...
// end
//
// ...
// end
//
// end
// </pre>
//
func (p *CodeBuilder) TypeSwitch(name string) *CodeBuilder {
if debugInstr {
log.Println("TypeSwitch")
Expand Down
9 changes: 8 additions & 1 deletion func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go/token"
"go/types"
"log"
"strings"

"github.com/goplus/gox/internal"
)
Expand Down Expand Up @@ -281,9 +282,15 @@ func NewOverloadFunc(pos token.Pos, pkg *types.Package, name string, funcs ...ty
return types.NewTypeName(pos, pkg, name, &overloadFuncType{funcs})
}

const gop_overload = "__gop_overload__:"

func NewOverloadMethod(typ *types.Named, pos token.Pos, pkg *types.Package, name string, funcs ...types.Object) *types.Func {
oft := &overloadFuncType{funcs}
recv := types.NewParam(token.NoPos, pkg, "", oft)
var nameList []string
for _, f := range funcs {
nameList = append(nameList, f.Name())
}
recv := types.NewParam(token.NoPos, pkg, gop_overload+strings.Join(nameList, ";"), oft)
sig := types.NewSignature(recv, nil, nil, false)
ofn := types.NewFunc(pos, pkg, name, sig)
typ.AddMethod(ofn)
Expand Down
55 changes: 55 additions & 0 deletions typeparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,58 @@ func main() {
}
`)
}

func TestTypeParamsOverload(t *testing.T) {
const src = `package foo

const GopPackage = true

type Data[T any] struct {
v T
}

func (p *Data[T]) Foo__0() {
}
func (p *Data[T]) Foo__1(a string, t T) {
}
`
gt := newGoxTest()
_, err := gt.LoadGoPackage("foo", "foo.go", src)
if err != nil {
t.Fatal(err)
}
//gox.InitGopPkg(fooPkg)
pkg := gt.NewPackage("", "main")
pkgRef := pkg.Import("foo")
tyData := pkgRef.Ref("Data").Type()
tyInt := types.Typ[types.Int]
tyDataInt, err := types.Instantiate(nil, tyData, []types.Type{tyInt}, true)
if err != nil {
t.Fatal(err)
}
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.NewPointer(tyDataInt), "data").
Val(ctxRef(pkg, "data")).
Debug(
func(cb *gox.CodeBuilder) {
cb.Member("foo", gox.MemberFlagMethodAlias)
}).
Call(0).EndStmt().
Val(ctxRef(pkg, "data")).
Debug(
func(cb *gox.CodeBuilder) {
cb.Member("foo", gox.MemberFlagMethodAlias)
}).
Val("hello").Val(100).Call(2).EndStmt().
End()
domTest(t, pkg, `package main

import foo "foo"

func main() {
var data *foo.Data[int]
data.Foo__0()
data.Foo__1("hello", 100)
}
`)
}