Repository: goplus/gop
Pull request #1677 (2024-01-28 12:09:43)

overload method


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index f95aa1d3..50761274 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -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)
@@ -1085,7 +1080,7 @@ 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) {
@@ -1093,12 +1088,13 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile
 					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)
@@ -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{


Test patch:
diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go
index 0784fd27..e3ff38b4 100644
--- a/cl/compile_gop_test.go
+++ b/cl/compile_gop_test.go
@@ -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 = (
diff --git a/testdata/overloadmethod/method.gop b/testdata/overloadmethod/method.gop
new file mode 100644
index 00000000..a55ee9bb
--- /dev/null
+++ b/testdata/overloadmethod/method.gop
@@ -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)