From af21347bea61cad92cbf9682ac0fd4d4668ff94f Mon Sep 17 00:00:00 2001 From: visualfc Date: Tue, 12 Dec 2023 13:46:49 +0800 Subject: [PATCH] visit: exportedTypeHack for golang.org/x/tools v0.16 --- go.mod | 2 +- go.sum | 4 ++-- visit.go | 39 ++++++++++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 689257ee..41497f30 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,6 @@ require ( github.com/visualfc/gid v0.1.0 github.com/visualfc/goembed v0.3.2 github.com/visualfc/xtype v0.2.0 - golang.org/x/mod v0.13.0 + golang.org/x/mod v0.14.0 golang.org/x/tools v0.14.0 ) diff --git a/go.sum b/go.sum index 82917a9c..e5ab00da 100644 --- a/go.sum +++ b/go.sum @@ -56,8 +56,9 @@ golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -103,7 +104,6 @@ golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58 golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/visit.go b/visit.go index 7ef1d520..30df285f 100644 --- a/visit.go +++ b/visit.go @@ -18,6 +18,7 @@ package igop import ( "fmt" + "go/ast" "go/token" "go/types" "log" @@ -68,23 +69,23 @@ func (visit *visitor) program() { chks[""] = true // anonymous struct embed named type for pkg := range visit.pkgs { chks[pkg.Pkg.Path()] = true - for _, mem := range pkg.Members { - if fn, ok := mem.(*ssa.Function); ok { - visit.function(fn) - } - } } + isExtern := func(typ reflect.Type) bool { if typ.Kind() == reflect.Ptr { typ = typ.Elem() } return !chks[typ.PkgPath()] } - for _, T := range visit.prog.RuntimeTypes() { + + methodsOf := func(T types.Type) { + if types.IsInterface(T) { + return + } typ := visit.intp.preToType(T) // skip extern type if isExtern(typ) { - continue + return } mmap := make(map[string]*ssa.Function) mset := visit.prog.MethodSets.MethodSet(T) @@ -101,6 +102,30 @@ func (visit *visitor) program() { } visit.intp.msets[typ] = mmap } + + exportedTypeHack := func(t *ssa.Type) { + if ast.IsExported(t.Name()) && !types.IsInterface(t.Type()) { + if named, ok := t.Type().(*types.Named); ok && !hasTypeParam(named) { + methodsOf(named) // T + methodsOf(types.NewPointer(named)) // *T + } + } + } + + for pkg := range visit.pkgs { + for _, mem := range pkg.Members { + switch mem := mem.(type) { + case *ssa.Function: + visit.function(mem) + case *ssa.Type: + exportedTypeHack(mem) + } + } + } + + for _, T := range visit.prog.RuntimeTypes() { + methodsOf(T) + } } func (visit *visitor) findLinkSym(fn *ssa.Function) (*load.LinkSym, bool) {