Skip to content

Commit

Permalink
update:support named function in classfile's overload decl
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed May 17, 2024
1 parent 9b76eb9 commit 9bc8681
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
38 changes: 31 additions & 7 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,11 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
old, _ := p.SetCurFile(goFile, true)
defer p.RestoreCurFile(old)
var skipClassFields bool
var classRecvIdent *ast.Ident

if f.IsClass {
skipClassFields = true
classRecvIdent = ctx.classRecv.List[0].Type.(*ast.StarExpr).X.(*ast.Ident)
}

preloadFuncDecl := func(d *ast.FuncDecl) {
Expand Down Expand Up @@ -1080,8 +1083,15 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge

case *ast.OverloadFuncDecl:
var recv *ast.Ident
if d.Recv != nil {
otyp, ok := d.Recv.List[0].Type.(*ast.Ident)
if d.Recv != nil || ctx.isClass {
var otyp *ast.Ident
var ok bool
if ctx.isClass && classRecvIdent != nil {
otyp = classRecvIdent
ok = true
} else {
otyp, ok = d.Recv.List[0].Type.(*ast.Ident)
}
if !ok {
ctx.handleErrorf(d.Recv.List[0].Type.Pos(), "invalid recv type %v", ctx.LoadExpr(d.Recv.List[0].Type))
break
Expand All @@ -1102,12 +1112,23 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
ctx.handleErrorf(expr.Pos(), "invalid method %v", ctx.LoadExpr(expr))
break LoopFunc
}
onames = append(onames, expr.Name)
ctx.lbinames = append(ctx.lbinames, expr.Name)
exov = true
if ctx.rec != nil {
ctx.rec.ReferUse(expr, expr.Name)
if ctx.isClass { //goxls:for classfile func -> method
onames = append(onames, "."+expr.Name)
ctx.lbinames = append(ctx.lbinames, classRecvIdent)
exov = true
if ctx.rec != nil {
ctx.rec.ReferUse(classRecvIdent, classRecvIdent.Name)
ctx.rec.ReferUse(expr, classRecvIdent.Name+"."+expr.Name) //goxls: func ident as method ident
}
} else {
onames = append(onames, expr.Name)
ctx.lbinames = append(ctx.lbinames, expr.Name)
exov = true
if ctx.rec != nil {
ctx.rec.ReferUse(expr, expr.Name)
}
}

case *ast.SelectorExpr:
if d.Recv == nil {
ctx.handleErrorf(expr.Pos(), "invalid func %v", ctx.LoadExpr(expr))
Expand Down Expand Up @@ -1150,6 +1171,9 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
}
}
if exov { // need Gopo_xxx
if ctx.isClass {
recv = classRecvIdent
}
oname, err := overloadName(recv, name.Name, d.Operator)
if err != nil {
ctx.handleErrorf(name.NamePos, "%v", err)
Expand Down
30 changes: 30 additions & 0 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,36 @@ type foo struct {
`, "foo.gox")
}

func TestGopxOverload(t *testing.T) {
gopClTestFile(t, `
func addString(a, b string) string {
return a + b
}
func addInt(a, b int) int {
return a + b
}
func add = (
addInt
addString
)
`, `package main
const Gopo_Rect_add = ".addInt,.addString"
type Rect struct {
}
func (this *Rect) addString(a string, b string) string {
return a + b
}
func (this *Rect) addInt(a int, b int) int {
return a + b
}
`, "Rect.gox")
}

func TestClassFileGopx(t *testing.T) {
gopClTestFile(t, `
var (
Expand Down

0 comments on commit 9bc8681

Please sign in to comment.