Skip to content

Commit

Permalink
x/typesutil: info.Overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 29, 2024
1 parent 9979a40 commit 54e6365
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions x/typesutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func checkInfo(fset *token.FileSet, files []*ast.File, gofiles []*goast.File) (*
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
Overloads: make(map[*ast.Ident][]types.Object),
}
ginfo := &types.Info{
Types: make(map[goast.Expr]types.TypeAndValue),
Expand Down
38 changes: 38 additions & 0 deletions x/typesutil/gopinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package typesutil
import (
"go/types"

_ "unsafe"

"github.com/goplus/gop/ast"
"github.com/goplus/gox"
"github.com/qiniu/x/log"
)

Expand Down Expand Up @@ -128,6 +131,9 @@ type Info struct {
// in source order. Variables without an initialization expression do not
// appear in this list.
// InitOrder []*Initializer

// Overloads maps identifiers to the overload objects.
Overloads map[*ast.Ident][]types.Object
}

// ObjectOf returns the object denoted by the specified id,
Expand Down Expand Up @@ -226,6 +232,9 @@ func (info gopRecorder) Def(id *ast.Ident, obj types.Object) {
}
}

//go:linkname checkSigFuncEx github.com/goplus/gox.checkSigFuncEx
func checkSigFuncEx(sig *types.Signature) (types.Type, bool)

// Use maps identifiers to the objects they denote.
//
// For an embedded field, Use maps the *TypeName it denotes.
Expand All @@ -238,6 +247,35 @@ func (info gopRecorder) Use(id *ast.Ident, obj types.Object) {
if info.Uses != nil {
info.Uses[id] = obj
}
if info.Overloads != nil {
if sig, ok := obj.Type().(*types.Signature); ok {
if ext, ok := checkSigFuncEx(sig); ok {
if debugVerbose {
log.Println("==> Overloads:", id, ext)
}
switch t := ext.(type) {
case *gox.TyOverloadFunc:
info.Overloads[id] = t.Funcs
case *gox.TyOverloadMethod:
info.Overloads[id] = t.Methods
case *gox.TyTemplateRecvMethod:
if tsig, ok := t.Func.Type().(*types.Signature); ok {
if ex, ok := checkSigFuncEx(tsig); ok {
if t, ok := ex.(*gox.TyOverloadFunc); ok {
info.Overloads[id] = t.Funcs
}

Check warning on line 266 in x/typesutil/gopinfo.go

View check run for this annotation

Codecov / codecov/patch

x/typesutil/gopinfo.go#L261-L266

Added lines #L261 - L266 were not covered by tests
}
}
case *gox.TyOverloadNamed:
objs := make([]types.Object, len(t.Types))
for i, typ := range t.Types {
objs[i] = typ.Obj()
}
info.Overloads[id] = objs
}
}
}
}
}

// Implicit maps nodes to their implicitly declared objects, if any.
Expand Down
3 changes: 2 additions & 1 deletion x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func parserMixedSource(fset *token.FileSet, src string, gosrc string) (*typesuti
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
Overloads: make(map[*ast.Ident][]types.Object),
}
ginfo := &types.Info{
Types: make(map[goast.Expr]types.TypeAndValue),
Expand Down Expand Up @@ -91,6 +92,7 @@ func parserSource(fset *token.FileSet, filename string, src interface{}, mode pa
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
Overloads: make(map[*ast.Ident][]types.Object),
}
check := typesutil.NewChecker(conf, chkOpts, nil, info)
err = check.Files(nil, []*ast.File{f})
Expand Down Expand Up @@ -1494,7 +1496,6 @@ func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] {
}

func TestMixedRawNamed(t *testing.T) {
//gox.SetDebug(gox.DbgFlagAll)
testGopInfo(t, `
var a Var__0[int]
var b Var__1[M]
Expand Down

0 comments on commit 54e6365

Please sign in to comment.