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

overload method #1677

Merged
merged 1 commit into from
Jan 28, 2024
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
13 changes: 4 additions & 9 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,11 +1071,6 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile
preloadFuncDecl(d)

case *ast.OverloadFuncDecl:
const (
markIdent = 1 << iota
markSelector
markFuncLit
)
var recv *ast.Ident
if d.Recv != nil {
otyp, ok := d.Recv.List[0].Type.(*ast.Ident)
Expand All @@ -1085,20 +1080,21 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile
recv = otyp
}
onames := make([]string, 0, 4)
mark := 0
exov := false
name := d.Name
for idx, fn := range d.Funcs {
switch expr := fn.(type) {
case *ast.Ident:
checkOverloadFunc(d)
onames = append(onames, expr.Name)
ctx.lbinames = append(ctx.lbinames, expr.Name)
mark |= markIdent
exov = true
case *ast.SelectorExpr:
checkOverloadMethod(d)
checkOverloadMethodRecvType(recv, expr.X)
onames = append(onames, "."+expr.Sel.Name)
ctx.lbinames = append(ctx.lbinames, recv)
exov = true
case *ast.FuncLit:
checkOverloadFunc(d)
name1 := overloadFuncName(name.Name, idx)
Expand All @@ -1109,12 +1105,11 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile
Type: expr.Type,
Body: expr.Body,
})
mark |= markFuncLit
default:
log.Panicf("TODO - cl.preloadFile OverloadFuncDecl: unknown func - %T\n", expr)
}
}
if (mark & (markIdent | markSelector)) != 0 {
if exov { // need Gopo_xxx
oname := overloadName(recv, name.Name, d.Operator)
oval := strings.Join(onames, ",")
preloadConst(&ast.GenDecl{
Expand Down
47 changes: 47 additions & 0 deletions cl/compile_gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,53 @@ func main() {
`)
}

func TestOverloadMethod(t *testing.T) {
gopClTest(t, `
type foo struct {
}

func (a *foo) mulInt(b int) *foo {
println "mulInt"
return a
}

func (a *foo) mulFoo(b *foo) *foo {
println "mulFoo"
return a
}

func (foo).mul = (
(foo).mulInt
(foo).mulFoo
)

var a, b foo
var c = a.mul(100)
var d = a.mul(c)
`, `package main

import "fmt"

type foo struct {
}

const Gopo_foo_mul = ".mulInt,.mulFoo"

func (a *foo) mulInt(b int) *foo {
fmt.Println("mulInt")
return a
}
func (a *foo) mulFoo(b *foo) *foo {
fmt.Println("mulFoo")
return a
}

var a, b foo
var c = a.mulInt(100)
var d = a.mulFoo(c)
`)
}

func TestOverloadFunc(t *testing.T) {
gopClTest(t, `
func add = (
Expand Down
21 changes: 21 additions & 0 deletions testdata/overloadmethod/method.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type foo struct {
}

func (a *foo) mulInt(b int) *foo {
println "mulInt"
return a
}

func (a *foo) mulFoo(b *foo) *foo {
println "mulFoo"
return a
}

func (foo).mul = (
(foo).mulInt
(foo).mulFoo
)

var a, b *foo
var c = a.mul(100)
var d = a.mul(c)