Repository: goplus/gop
Pull request #1594 (2023-12-29 11:38:28)

types record support command call for overload


Patch:
diff --git a/cl/expr.go b/cl/expr.go
index 1d1576e9..fc9698ee 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -435,20 +435,23 @@ func compilePkgRef(ctx *blockCtx, at *gox.PkgRef, x *ast.Ident, flags, pkgKind i
 	if v, alias := lookupPkgRef(ctx, at, x, pkgKind); v != nil {
 		cb := ctx.cb
 		if (flags & clIdentLHS) != 0 {
+			if rec := ctx.recorder(); rec != nil {
+				rec.Use(x, v)
+			}
 			cb.VarRef(v, x)
 		} else {
 			autoprop := alias && (flags&clIdentCanAutoCall) != 0
 			if autoprop && !gox.HasAutoProperty(v.Type()) {
 				return false
 			}
+			if rec := ctx.recorder(); rec != nil {
+				rec.Use(x, v)
+			}
 			cb.Val(v, x)
 			if autoprop {
-				cb.Call(0)
+				cb.CallWith(0, 0, x)
 			}
 		}
-		if rec := ctx.recorder(); rec != nil {
-			rec.Use(x, v)
-		}
 		return true
 	}
 	return false
diff --git a/cl/recorder.go b/cl/recorder.go
index ba29ef9e..5ce8e9cb 100644
--- a/cl/recorder.go
+++ b/cl/recorder.go
@@ -49,6 +49,12 @@ func (p *goxRecorder) Member(id ast.Node, obj types.Object) {
 
 func (p *goxRecorder) Call(id ast.Node, obj types.Object) {
 	switch v := id.(type) {
+	case *ast.Ident:
+		p.rec.Use(v, obj)
+		p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
+	case *ast.SelectorExpr:
+		p.rec.Use(v.Sel, obj)
+		p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
 	case *ast.CallExpr:
 		switch id := v.Fun.(type) {
 		case *ast.Ident:
diff --git a/cl/stmt.go b/cl/stmt.go
index 8bb4a30c..62136acc 100644
--- a/cl/stmt.go
+++ b/cl/stmt.go
@@ -126,7 +126,7 @@ func compileStmt(ctx *blockCtx, stmt ast.Stmt) {
 		}
 		compileExpr(ctx, v.X, inFlags)
 		if inFlags != 0 && gox.IsFunc(ctx.cb.InternalStack().Get(-1).Type) {
-			ctx.cb.Call(0)
+			ctx.cb.CallWith(0, 0, v.X)
 		}
 	case *ast.AssignStmt:
 		compileAssignStmt(ctx, v)


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 40c8cac1..952b57ee 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -5090,3 +5090,33 @@ func main() {
 }
 `)
 }
+
+func TestMixedOverloadCommand(t *testing.T) {
+	gopMixedClTest(t, "main", `package main
+
+func Test__0() {
+}
+func Test__1(n int) {
+}
+type N struct {
+}
+func (p *N) Test__0() {
+}
+func (p *N) Test__1(n int) {
+}`, `
+Test
+Test 100
+var n N
+n.test
+n.test 100
+`, `package main
+
+func main() {
+	Test__0()
+	Test__1(100)
+	var n N
+	n.Test__0()
+	n.Test__1(100)
+}
+`)
+}
diff --git a/x/typesutil/info_test.go b/x/typesutil/info_test.go
index 585d8500..4632f22f 100644
--- a/x/typesutil/info_test.go
+++ b/x/typesutil/info_test.go
@@ -1340,3 +1340,48 @@ func OnKey__a(a, b string, v ...int) {
 030: 33: 1 | n                   | var n *main.N
 031: 33: 3 | onKey               | func (*main.N).OnKey__8(x int, y int)`)
 }
+
+func TestMixedOverload3(t *testing.T) {
+	testGopInfo(t, `
+Test
+Test 100
+var n N
+n.test
+n.test 100
+`, `
+package main
+
+func Test__0() {
+}
+func Test__1(n int) {
+}
+type N struct {
+}
+func (p *N) Test__0() {
+}
+func (p *N) Test__1(n int) {
+}
+`, `== types ==
+000:  2: 1 | Test                *ast.Ident                     | value   : func() | value
+001:  3: 1 | Test                *ast.Ident                     | value   : func(n int) | value
+002:  3: 1 | Test 100            *ast.CallExpr                  | void    : () | no value
+003:  3: 6 | 100                 *ast.BasicLit                  | value   : untyped int = 100 | constant
+004:  4: 7 | N                   *ast.Ident                     | type    : main.N | type
+005:  5: 1 | n                   *ast.Ident                     | var     : main.N | variable
+006:  5: 1 | n.test              *ast.SelectorExpr              | value   : func() | value
+007:  6: 1 | n                   *ast.Ident                     | var     : main.N | variable
+008:  6: 1 | n.test              *ast.SelectorExpr              | value   : func(n int) | value
+009:  6: 1 | n.test 100          *ast.CallExpr                  | void    : () | no value
+010:  6: 8 | 100                 *ast.BasicLit                  | value   : untyped int = 100 | constant
+== defs ==
+000:  2: 1 | main                | func main.main()
+001:  4: 5 | n                   | var n main.N
+== uses ==
+000:  2: 1 | Test                | func main.Test__0()
+001:  3: 1 | Test                | func main.Test__1(n int)
+002:  4: 7 | N                   | type main.N struct{}
+003:  5: 1 | n                   | var n main.N
+004:  5: 3 | test                | func (*main.N).Test__0()
+005:  6: 1 | n                   | var n main.N
+006:  6: 3 | test                | func (*main.N).Test__1(n int)`)
+}