Repository: goplus/gop
Pull request #1671 (2024-01-26 02:05:32)

ast.Walk: support OverloadFuncDecl


Patch:
diff --git a/ast/walk.go b/ast/walk.go
index fc9f98d1..08874611 100644
--- a/ast/walk.go
+++ b/ast/walk.go
@@ -98,9 +98,18 @@ func Walk(v Visitor, node Node) {
 		}
 
 	// Expressions
-	case *BadExpr, *Ident, *BasicLit:
+	case *BadExpr, *Ident:
 		// nothing to do
 
+	case *BasicLit:
+		if n.Extra != nil { // Go+ extended
+			for _, part := range n.Extra.Parts {
+				if e, ok := part.(Expr); ok {
+					Walk(v, e)
+				}
+			}
+		}
+
 	case *Ellipsis:
 		if n.Elt != nil {
 			Walk(v, n.Elt)
@@ -377,7 +386,7 @@ func Walk(v Visitor, node Node) {
 			Walk(v, f)
 		}
 
-	// Go+ expr and stmt
+	// Go+ extended expr and stmt
 	case *SliceLit:
 		walkExprList(v, n.Elts)
 
@@ -433,6 +442,16 @@ func Walk(v Visitor, node Node) {
 			Walk(v, n.Default)
 		}
 
+	case *OverloadFuncDecl:
+		if n.Doc != nil {
+			Walk(v, n.Doc)
+		}
+		if n.Recv != nil {
+			Walk(v, n.Recv)
+		}
+		Walk(v, n.Name)
+		walkExprList(v, n.Funcs)
+
 	default:
 		panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
 	}
diff --git a/cl/expr.go b/cl/expr.go
index ee2cddb3..fb6cc143 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -1175,7 +1175,7 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
 			retName = "_gop_ret" + strconv.Itoa(i+1)
 		}
 	}
-	sig := types.NewSignature(nil, nil, types.NewTuple(ret...), false)
+	sig := types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(ret...), false)
 	if useClosure {
 		cb.NewClosureWith(sig).BodyStart(pkg)
 	} else {
diff --git a/cl/internal/overload/bar/bar.go b/cl/internal/overload/bar/bar.go
deleted file mode 100644
index b675663b..00000000
--- a/cl/internal/overload/bar/bar.go
+++ /dev/null
@@ -1,33 +0,0 @@
-package bar
-
-const GopPackage = true
-
-type M = map[string]any
-
-type basetype interface {
-	string | int | bool | float64
-}
-
-type Var__0[T basetype] struct {
-	val T
-}
-
-func (p *Var__0[T]) Value() T {
-	return p.val
-}
-
-type Var__1[T map[string]any] struct {
-	val T
-}
-
-func (p *Var__1[T]) Value() T {
-	return p.val
-}
-
-func Gopx_Var_Cast__0[T basetype]() *Var__0[T] {
-	return new(Var__0[T])
-}
-
-func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] {
-	return new(Var__1[T])
-}
diff --git a/parser/interface.go b/parser/interface.go
index 1b61209b..7fd616af 100644
--- a/parser/interface.go
+++ b/parser/interface.go
@@ -46,12 +46,15 @@ const (
 	AllErrors = Mode(goparser.AllErrors)
 	// SkipObjectResolution - don't resolve identifiers to objects - see ParseFile
 	SkipObjectResolution = Mode(goparser.SkipObjectResolution)
+
 	// ParseGoAsGoPlus - parse Go files by gop/parser
 	ParseGoAsGoPlus Mode = 1 << 16
 	// ParserGoPlusClass - parse Go+ classfile by gop/parser
 	ParseGoPlusClass Mode = 1 << 17
 	// SaveAbsFile - parse and save absolute path to pkg.Files
 	SaveAbsFile Mode = 1 << 18
+
+	goReservedFlags Mode = ((1 << 16) - 1)
 )
 
 // -----------------------------------------------------------------------------
diff --git a/parser/parser_gop.go b/parser/parser_gop.go
index d3fe3e99..34f549f2 100644
--- a/parser/parser_gop.go
+++ b/parser/parser_gop.go
@@ -161,7 +161,7 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, dir string, conf Config) (pk
 			filename := fs.Join(dir, fname)
 			if useGoParser {
 				if filedata, err := fs.ReadFile(filename); err == nil {
-					if src, err := goparser.ParseFile(fset, filename, filedata, goparser.Mode(conf.Mode)); err == nil {
+					if src, err := goparser.ParseFile(fset, filename, filedata, goparser.Mode(conf.Mode&goReservedFlags)); err == nil {
 						pkg := reqPkg(pkgs, src.Name.Name)
 						if pkg.GoFiles == nil {
 							pkg.GoFiles = make(map[string]*goast.File)


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 6889a30b..031ee2e0 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -5140,70 +5140,3 @@ func main() {
 }
 `)
 }
-
-func TestOverloadNamed(t *testing.T) {
-	gopClTest(t, `
-import "github.com/goplus/gop/cl/internal/overload/bar"
-
-var a bar.Var[int]
-var b bar.Var[bar.M]
-c := bar.Var(string)
-d := bar.Var(bar.M)
-`, `package main
-
-import "github.com/goplus/gop/cl/internal/overload/bar"
-
-var a bar.Var__0[int]
-var b bar.Var__1[map[string]any]
-
-func main() {
-	c := bar.Gopx_Var_Cast__0[string]()
-	d := bar.Gopx_Var_Cast__1[map[string]any]()
-}
-`)
-}
-
-func TestMixedOverloadNamed(t *testing.T) {
-	gopMixedClTest(t, "main", `package main
-
-const GopPackage = true
-
-type M = map[string]any
-
-type basetype interface {
-	string | int | bool | float64
-}
-
-type Var__0[T basetype] struct {
-	val T
-}
-
-type Var__1[T map[string]any] struct {
-	val T
-}
-
-func Gopx_Var_Cast__0[T basetype]() *Var__0[T] {
-	return new(Var__0[T])
-}
-
-func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] {
-	return new(Var__1[T])
-}
-`, `
-var a Var[int]
-var b Var[M]
-c := Var(string)
-d := Var(M)
-`, `package main
-
-var a Var__0[int]
-var b Var__1[map[string]interface {
-}]
-
-func main() {
-	c := Gopx_Var_Cast__0[string]()
-	d := Gopx_Var_Cast__1[map[string]interface {
-	}]()
-}
-`)
-}