Skip to content

Commit 397272d

Browse files
committed
update:support named function in classfile's overload decl
1 parent 01bb888 commit 397272d

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed

cl/compile.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,26 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
10711071

10721072
case *ast.OverloadFuncDecl:
10731073
var recv *ast.Ident
1074+
if ctx.classRecv != nil { // in class file (.spx/.gmx)
1075+
if recv := d.Recv; recv == nil || len(recv.List) == 0 {
1076+
d.Recv = &ast.FieldList{
1077+
List: []*ast.Field{
1078+
{
1079+
Type: ctx.classRecv.List[0].Type.(*ast.StarExpr).X,
1080+
},
1081+
},
1082+
}
1083+
d.IsClass = true
1084+
}
1085+
}
10741086
if d.Recv != nil {
1075-
otyp, ok := d.Recv.List[0].Type.(*ast.Ident)
1087+
var ok bool
1088+
recv, ok = d.Recv.List[0].Type.(*ast.Ident)
10761089
if !ok {
10771090
ctx.handleErrorf(d.Recv.List[0].Type.Pos(), "invalid recv type %v", ctx.LoadExpr(d.Recv.List[0].Type))
10781091
break
10791092
}
1080-
recv = otyp
1093+
ctx.lbinames = append(ctx.lbinames, recv)
10811094
if ctx.rec != nil {
10821095
ctx.rec.ReferUse(recv, recv.Name)
10831096
}
@@ -1086,18 +1099,27 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
10861099
exov := false
10871100
name := d.Name
10881101
LoopFunc:
1102+
10891103
for idx, fn := range d.Funcs {
10901104
switch expr := fn.(type) {
10911105
case *ast.Ident:
1092-
if d.Recv != nil && !d.Operator {
1106+
if d.Recv != nil && !d.Operator && !d.IsClass {
10931107
ctx.handleErrorf(expr.Pos(), "invalid method %v", ctx.LoadExpr(expr))
10941108
break LoopFunc
10951109
}
1096-
onames = append(onames, expr.Name)
1097-
ctx.lbinames = append(ctx.lbinames, expr.Name)
10981110
exov = true
1111+
if d.IsClass {
1112+
onames = append(onames, "."+expr.Name)
1113+
} else {
1114+
onames = append(onames, expr.Name)
1115+
ctx.lbinames = append(ctx.lbinames, expr.Name)
1116+
}
10991117
if ctx.rec != nil {
1100-
ctx.rec.ReferUse(expr, expr.Name)
1118+
if d.IsClass {
1119+
ctx.rec.ReferUse(expr, recv.Name+"."+expr.Name)
1120+
} else {
1121+
ctx.rec.ReferUse(expr, expr.Name)
1122+
}
11011123
}
11021124
case *ast.SelectorExpr:
11031125
if d.Recv == nil {
@@ -1111,14 +1133,13 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
11111133
}
11121134

11131135
onames = append(onames, "."+expr.Sel.Name)
1114-
ctx.lbinames = append(ctx.lbinames, recv)
11151136
exov = true
11161137
if ctx.rec != nil {
11171138
ctx.rec.ReferUse(rtyp, rtyp.Name)
11181139
ctx.rec.ReferUse(expr.Sel, rtyp.Name+"."+expr.Sel.Name)
11191140
}
11201141
case *ast.FuncLit:
1121-
if d.Recv != nil && !d.Operator {
1142+
if d.Recv != nil && !d.Operator && !d.IsClass {
11221143
ctx.handleErrorf(expr.Pos(), "invalid method %v", ctx.LoadExpr(expr))
11231144
break LoopFunc
11241145
}

cl/compile_spx_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,42 @@ type foo struct {
13971397
`, "foo.gox")
13981398
}
13991399

1400+
func TestGopxOverload(t *testing.T) {
1401+
gopClTestFile(t, `
1402+
func addString(a, b string) string {
1403+
return a + b
1404+
}
1405+
1406+
func addInt(a, b int) int {
1407+
return a + b
1408+
}
1409+
1410+
func add = (
1411+
addInt
1412+
func(a, b float64) float64 {
1413+
return a + b
1414+
}
1415+
addString
1416+
)
1417+
`, `package main
1418+
1419+
const Gopo_Rect_add = ".addInt,,.addString"
1420+
1421+
type Rect struct {
1422+
}
1423+
1424+
func (this *Rect) addString(a string, b string) string {
1425+
return a + b
1426+
}
1427+
func (this *Rect) addInt(a int, b int) int {
1428+
return a + b
1429+
}
1430+
func (this *Rect) add__1(a float64, b float64) float64 {
1431+
return a + b
1432+
}
1433+
`, "Rect.gox")
1434+
}
1435+
14001436
func TestClassFileGopx(t *testing.T) {
14011437
gopClTestFile(t, `
14021438
var (

x/typesutil/info_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -2382,3 +2382,69 @@ func init() {
23822382
000: 12: 2 | add | func main.add(__gop_overload_args__ interface{_()})
23832383
001: 13: 2 | add | func main.add(__gop_overload_args__ interface{_()})`)
23842384
}
2385+
2386+
func TestGoxOverloadInfo(t *testing.T) {
2387+
testSpxInfo(t, "Rect.gox", `
2388+
func addInt(a, b int) int {
2389+
return a + b
2390+
}
2391+
2392+
func addString(a, b string) string {
2393+
return a + b
2394+
}
2395+
2396+
func add = (
2397+
addInt
2398+
func(a, b float64) float64 {
2399+
return a + b
2400+
}
2401+
addString
2402+
)
2403+
`, `== types ==
2404+
000: 0: 0 | ".addInt,,.addString" *ast.BasicLit | value : untyped string = ".addInt,,.addString" | constant
2405+
001: 0: 0 | Rect *ast.Ident | type : main.Rect | type
2406+
002: 2:18 | int *ast.Ident | type : int | type
2407+
003: 2:23 | int *ast.Ident | type : int | type
2408+
004: 3: 9 | a *ast.Ident | var : int | variable
2409+
005: 3: 9 | a + b *ast.BinaryExpr | value : int | value
2410+
006: 3:13 | b *ast.Ident | var : int | variable
2411+
007: 6:21 | string *ast.Ident | type : string | type
2412+
008: 6:29 | string *ast.Ident | type : string | type
2413+
009: 7: 9 | a *ast.Ident | var : string | variable
2414+
010: 7: 9 | a + b *ast.BinaryExpr | value : string | value
2415+
011: 7:13 | b *ast.Ident | var : string | variable
2416+
012: 12:12 | float64 *ast.Ident | type : float64 | type
2417+
013: 12:21 | float64 *ast.Ident | type : float64 | type
2418+
014: 13:10 | a *ast.Ident | var : float64 | variable
2419+
015: 13:10 | a + b *ast.BinaryExpr | value : float64 | value
2420+
016: 13:14 | b *ast.Ident | var : float64 | variable
2421+
== defs ==
2422+
000: 0: 0 | Gopo_Rect_add | const main.Gopo_Rect_add untyped string
2423+
001: 0: 0 | this | var this *main.Rect
2424+
002: 2: 6 | addInt | func (*main.Rect).addInt(a int, b int) int
2425+
003: 2:13 | a | var a int
2426+
004: 2:16 | b | var b int
2427+
005: 6: 6 | addString | func (*main.Rect).addString(a string, b string) string
2428+
006: 6:16 | a | var a string
2429+
007: 6:19 | b | var b string
2430+
008: 10: 6 | add | func (main.Rect).add(__gop_overload_args__ interface{_()})
2431+
009: 12: 2 | add__1 | func (*main.Rect).add__1(a float64, b float64) float64
2432+
010: 12: 7 | a | var a float64
2433+
011: 12:10 | b | var b float64
2434+
== uses ==
2435+
000: 0: 0 | Rect | type main.Rect struct{}
2436+
001: 2:18 | int | type int
2437+
002: 2:23 | int | type int
2438+
003: 3: 9 | a | var a int
2439+
004: 3:13 | b | var b int
2440+
005: 6:21 | string | type string
2441+
006: 6:29 | string | type string
2442+
007: 7: 9 | a | var a string
2443+
008: 7:13 | b | var b string
2444+
009: 11: 2 | addInt | func (*main.Rect).addInt(a int, b int) int
2445+
010: 12:12 | float64 | type float64
2446+
011: 12:21 | float64 | type float64
2447+
012: 13:10 | a | var a float64
2448+
013: 13:14 | b | var b float64
2449+
014: 15: 2 | addString | func (*main.Rect).addString(a string, b string) string`)
2450+
}

0 commit comments

Comments
 (0)