Skip to content

Commit

Permalink
gox/typesutil
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 28, 2023
1 parent 55c076c commit e16c3dc
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 50 deletions.
3 changes: 2 additions & 1 deletion ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"

"github.com/goplus/gox/internal"
"github.com/goplus/gox/typesutil"
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -613,7 +614,7 @@ retry:
}
return
} else if IsCSignature(t) {
sig = types.NewSignatureType(nil, nil, nil, t.Params(), t.Results(), t.Variadic())
sig = typesutil.NewSignatureType(nil, nil, nil, t.Params(), t.Results(), t.Variadic())
} else {
sig = t
}
Expand Down
11 changes: 6 additions & 5 deletions builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"syscall"

"github.com/goplus/gox/typesutil"
"golang.org/x/tools/go/types/typeutil"
)

Expand Down Expand Up @@ -378,10 +379,10 @@ func initBuiltinFuncs(builtin *types.Package) {
emptyIntfSlice := types.NewSlice(TyEmptyInterface)
emptyIntfSliceVar := types.NewVar(token.NoPos, builtin, "args", emptyIntfSlice)
emptyIntfSliceTuple := types.NewTuple(emptyIntfSliceVar)
gbl.Insert(types.NewFunc(token.NoPos, builtin, "panic", types.NewSignatureType(nil, nil, nil, emptyIntfTuple, nil, false)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "recover", types.NewSignatureType(nil, nil, nil, nil, emptyIntfTuple, false)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "print", types.NewSignatureType(nil, nil, nil, emptyIntfSliceTuple, nil, true)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "println", types.NewSignatureType(nil, nil, nil, emptyIntfSliceTuple, nil, true)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "panic", typesutil.NewSignatureType(nil, nil, nil, emptyIntfTuple, nil, false)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "recover", typesutil.NewSignatureType(nil, nil, nil, nil, emptyIntfTuple, false)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "print", typesutil.NewSignatureType(nil, nil, nil, emptyIntfSliceTuple, nil, true)))
gbl.Insert(types.NewFunc(token.NoPos, builtin, "println", typesutil.NewSignatureType(nil, nil, nil, emptyIntfSliceTuple, nil, true)))

// new & make are special cases, they require to pass a type.
gbl.Insert(NewInstruction(token.NoPos, builtin, "new", newInstr{}))
Expand All @@ -406,7 +407,7 @@ func newBFunc(builtin *types.Package, name string, t typeBFunc) types.Object {
vars[i] = types.NewParam(token.NoPos, builtin, param.name, types.Typ[param.typ])
}
result := types.NewParam(token.NoPos, builtin, "", types.Typ[t.result])
sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(vars...), types.NewTuple(result), false)
sig := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(vars...), types.NewTuple(result), false)
return types.NewFunc(token.NoPos, builtin, name, sig)
}

Expand Down
27 changes: 14 additions & 13 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/goplus/gox/internal"
"github.com/goplus/gox/internal/go/format"
"github.com/goplus/gox/packages"
"github.com/goplus/gox/typesutil"
)

var (
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestContract(t *testing.T) {
{comparable, types.NewSlice(tyInt), false},
{comparable, types.NewMap(tyInt, tyInt), false},
{comparable, types.NewChan(0, tyInt), true},
{comparable, types.NewSignatureType(nil, nil, nil, nil, nil, false), false},
{comparable, typesutil.NewSignatureType(nil, nil, nil, nil, nil, false), false},
{comparable, NewTemplateSignature(nil, nil, nil, nil, false), false},
{addable, types.NewNamed(types.NewTypeName(0, at, "bar", nil), types.Typ[types.Bool], nil), false},
{addable, tfoo, true},
Expand Down Expand Up @@ -187,10 +188,10 @@ func TestComparableTo(t *testing.T) {
func TestComparableTo2(t *testing.T) {
pkg := NewPackage("foo", "foo", gblConf)
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
types.NewFunc(token.NoPos, pkg.Types, "Bar", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
methods2 := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "F", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
types.NewFunc(token.NoPos, pkg.Types, "F", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
tyInterfF := types.NewInterfaceType(methods2, nil).Complete()
Expand Down Expand Up @@ -271,7 +272,7 @@ func TestToIndex(t *testing.T) {
}

func TestCheckOverloadMethod(t *testing.T) {
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
sig := typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)
if _, ok := CheckOverloadMethod(sig); ok {
t.Fatal("TestCheckOverloadMethod failed:")
}
Expand All @@ -281,17 +282,17 @@ func TestIsFunc(t *testing.T) {
if IsFunc(nil) {
t.Fatal("nil is func?")
}
if !IsFunc(types.NewSignatureType(nil, nil, nil, nil, nil, false)) {
if !IsFunc(typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)) {
t.Fatal("func() is not func?")
}
fn := types.NewFunc(token.NoPos, nil, "fn", types.NewSignatureType(nil, nil, nil, nil, nil, false))
fn := types.NewFunc(token.NoPos, nil, "fn", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false))
if !IsFunc(&templateRecvMethodType{fn: fn}) {
t.Fatal("templateRecvMethodType is not func?")
}
if HasAutoProperty(nil) {
t.Fatal("nil has autoprop?")
}
if !HasAutoProperty(types.NewSignatureType(nil, nil, nil, nil, nil, false)) {
if !HasAutoProperty(typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)) {
t.Fatal("func() has not autoprop?")
}
}
Expand Down Expand Up @@ -789,13 +790,13 @@ func TestCheckSignature(t *testing.T) {
if CheckSignature(nil, 0, 0) != nil {
t.Fatal("TestCheckSignature failed: CheckSignature(nil) != nil")
}
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
sig := typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)
if CheckSignature(sig, 0, 0) != sig {
t.Fatal("TestCheckSignature failed: CheckSignature(sig) != sig")
}
pkg := types.NewPackage("", "foo")
arg := types.NewParam(token.NoPos, pkg, "", sig)
sig2 := types.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg), nil, false)
sig2 := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg), nil, false)
o := types.NewFunc(token.NoPos, pkg, "bar", sig2)
if CheckSignature(&templateRecvMethodType{fn: o}, 0, 0) == nil {
t.Fatal("TestCheckSignature failed: CheckSignature == nil")
Expand Down Expand Up @@ -827,18 +828,18 @@ func TestCheckSignatures(t *testing.T) {
if CheckSignatures(nil, 0, 0) != nil {
t.Fatal("TestCheckSignatures failed: CheckSignatures(nil) != nil")
}
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
sig := typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)
if v := CheckSignatures(sig, 0, 0); len(v) != 1 || v[0] != sig {
t.Fatal("TestCheckSignatures failed: CheckSignatures(sig)[0] != sig")
}
pkg := types.NewPackage("", "foo")
arg := types.NewParam(token.NoPos, pkg, "", sig)
sig2 := types.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg), nil, false)
sig2 := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg), nil, false)
o := types.NewFunc(token.NoPos, pkg, "bar", sig2)
if CheckSignatures(&templateRecvMethodType{fn: o}, 0, 0) == nil {
t.Fatal("TestCheckSignatures failed: CheckSignatures == nil")
}
sig3 := types.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg, arg), nil, false)
sig3 := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(arg, arg, arg), nil, false)
o2 := types.NewFunc(token.NoPos, pkg, "bar", sig3)
of := NewOverloadFunc(token.NoPos, pkg, "bar", o, o2)
if v := CheckSignatures(of.Type(), 0, 0); len(v) != 2 {
Expand Down Expand Up @@ -946,7 +947,7 @@ func TestNewFuncDeclPanic(t *testing.T) {
}()
pkg := NewPackage("", "foo", gblConf)
a := types.NewParam(token.NoPos, pkg.Types, "", types.Typ[types.Int])
sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(a), nil, false)
sig := typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(a), nil, false)
pkg.NewFuncDecl(token.NoPos, "init", sig)
}

Expand Down
3 changes: 2 additions & 1 deletion c.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"

"github.com/goplus/gox/internal"
"github.com/goplus/gox/typesutil"
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -250,7 +251,7 @@ func (p *UnionFields) FieldRef(cb *CodeBuilder, tfld *types.Named, name string,
// NewCSignature creates prototype of a C function.
func NewCSignature(params, results *types.Tuple, variadic bool) *types.Signature {
crecv := types.NewParam(token.NoPos, nil, "", types.Typ[types.UntypedNil])
return types.NewSignatureType(crecv, nil, nil, params, results, variadic)
return typesutil.NewSignatureType(crecv, nil, nil, params, results, variadic)
}

// IsCSignature checks a prototype is C function or not.
Expand Down
7 changes: 4 additions & 3 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"

"github.com/goplus/gox/internal"
"github.com/goplus/gox/typesutil"
"golang.org/x/tools/go/types/typeutil"
)

Expand Down Expand Up @@ -566,7 +567,7 @@ func (p *CodeBuilder) emitVar(pkg *Package, closure *Func, param *types.Var, wit

// NewClosure func
func (p *CodeBuilder) NewClosure(params, results *Tuple, variadic bool) *Func {
sig := types.NewSignatureType(nil, nil, nil, params, results, variadic)
sig := typesutil.NewSignatureType(nil, nil, nil, params, results, variadic)
return p.NewClosureWith(sig)
}

Expand Down Expand Up @@ -1510,7 +1511,7 @@ func (p *CodeBuilder) Member(name string, flag MemberFlag, src ...ast.Node) (kin
for i := 0; i < spLen; i++ {
vars[i+1] = sp.At(i)
}
e.Type = types.NewSignatureType(nil, nil, nil, types.NewTuple(vars...), sig.Results(), sig.Variadic())
e.Type = typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(vars...), sig.Results(), sig.Variadic())
return
}
}
Expand Down Expand Up @@ -1732,7 +1733,7 @@ func methodTypeOf(typ types.Type) types.Type {
// is template recv method
return t
}
return types.NewSignatureType(nil, nil, nil, sig.Params(), sig.Results(), sig.Variadic())
return typesutil.NewSignatureType(nil, nil, nil, sig.Params(), sig.Results(), sig.Variadic())
}

func indirect(typ types.Type) types.Type {
Expand Down
13 changes: 7 additions & 6 deletions error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/goplus/gox"
"github.com/goplus/gox/typesutil"
)

type txtNode struct {
Expand Down Expand Up @@ -125,7 +126,7 @@ func newFunc(
recv *gox.Param, name string, params, results *types.Tuple, variadic bool) *gox.Func {
pos := position(line, column)
fn, err := pkg.NewFuncWith(
pos, name, types.NewSignatureType(recv, nil, nil, params, results, variadic), func() token.Pos {
pos, name, typesutil.NewSignatureType(recv, nil, nil, params, results, variadic), func() token.Pos {
return position(rline, rcolumn)
})
if err != nil {
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestErrTypeSwitch(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: impossible type switch case: v (type interface{Bar()}) cannot have dynamic type int (missing Bar method)",
func(pkg *gox.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
types.NewFunc(token.NoPos, pkg.Types, "Bar", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
v := pkg.NewParam(token.NoPos, "v", tyInterf)
Expand Down Expand Up @@ -208,7 +209,7 @@ func TestErrBinaryOp(t *testing.T) {
codeErrorTest(t, `./foo.gop:2:9: invalid operation: v != 3 (mismatched types interface{Bar()} and untyped int)`,
func(pkg *gox.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
types.NewFunc(token.NoPos, pkg.Types, "Bar", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
params := types.NewTuple(pkg.NewParam(token.NoPos, "v", tyInterf))
Expand All @@ -220,7 +221,7 @@ func TestErrBinaryOp(t *testing.T) {
codeErrorTest(t, `./foo.gop:2:9: invalid operation: sl == v (mismatched types []int and interface{Bar()})`,
func(pkg *gox.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
types.NewFunc(token.NoPos, pkg.Types, "Bar", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
params := types.NewTuple(pkg.NewParam(token.NoPos, "v", tyInterf))
Expand All @@ -243,7 +244,7 @@ func TestErrTypeAssert(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: impossible type assertion:\n\tstring does not implement bar (missing Bar method)",
func(pkg *gox.Package) {
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Bar", types.NewSignatureType(nil, nil, nil, nil, nil, false)),
types.NewFunc(token.NoPos, pkg.Types, "Bar", typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
bar := pkg.NewType("bar").InitType(pkg, tyInterf)
Expand Down Expand Up @@ -499,7 +500,7 @@ func TestErrFunc(t *testing.T) {
codeErrorTest(t, `./foo.gop:5:1: main redeclared in this block
./foo.gop:1:10: other declaration of main`,
func(pkg *gox.Package) {
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
sig := typesutil.NewSignatureType(nil, nil, nil, nil, nil, false)
pkg.NewFuncDecl(position(1, 10), "main", sig).BodyStart(pkg).End()
pkg.NewFuncDecl(position(5, 1), "main", sig).BodyStart(pkg).End()
})
Expand Down
11 changes: 6 additions & 5 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"

"github.com/goplus/gox/internal"
"github.com/goplus/gox/typesutil"
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -133,7 +134,7 @@ func (p *Package) NewFuncDecl(pos token.Pos, name string, sig *types.Signature)

// NewFunc func
func (p *Package) NewFunc(recv *Param, name string, params, results *Tuple, variadic bool) *Func {
sig := types.NewSignatureType(recv, nil, nil, params, results, variadic)
sig := typesutil.NewSignatureType(recv, nil, nil, params, results, variadic)
f, err := p.NewFuncWith(token.NoPos, name, sig, nil)
if err != nil {
panic(err)
Expand Down Expand Up @@ -289,7 +290,7 @@ func NewOverloadFunc(pos token.Pos, pkg *types.Package, name string, funcs ...ty
func NewOverloadMethod(typ *types.Named, pos token.Pos, pkg *types.Package, name string, funcs ...types.Object) *types.Func {
oft := &overloadFuncType{funcs}
recv := types.NewParam(token.NoPos, pkg, "", oft)
sig := types.NewSignatureType(recv, nil, nil, nil, nil, false)
sig := typesutil.NewSignatureType(recv, nil, nil, nil, nil, false)
ofn := types.NewFunc(pos, pkg, name, sig)
typ.AddMethod(ofn)
return ofn
Expand All @@ -308,7 +309,7 @@ func CheckOverloadMethod(sig *types.Signature) (funcs []types.Object, ok bool) {
func NewTemplateRecvMethod(typ *types.Named, pos token.Pos, pkg *types.Package, name string, fn types.Object) *types.Func {
trmt := &templateRecvMethodType{fn}
recv := types.NewParam(token.NoPos, pkg, "", trmt)
sig := types.NewSignatureType(recv, nil, nil, nil, nil, false)
sig := typesutil.NewSignatureType(recv, nil, nil, nil, nil, false)
ofn := types.NewFunc(pos, pkg, name, sig)
typ.AddMethod(ofn)
return ofn
Expand Down Expand Up @@ -336,7 +337,7 @@ func CheckSignature(typ types.Type, idx, nin int) *types.Signature {
for i := range mparams {
mparams[i] = params.At(i + 1)
}
return types.NewSignatureType(nil, nil, nil, types.NewTuple(mparams...), sig.Results(), sig.Variadic())
return typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(mparams...), sig.Results(), sig.Variadic())
}
}
return nil
Expand Down Expand Up @@ -372,7 +373,7 @@ func CheckSignatures(typ types.Type, idx, nin int) []*types.Signature {
for i := range mparams {
mparams[i] = params.At(i + 1)
}
return []*types.Signature{types.NewSignatureType(nil, nil, nil, types.NewTuple(mparams...), sig.Results(), sig.Variadic())}
return []*types.Signature{typesutil.NewSignatureType(nil, nil, nil, types.NewTuple(mparams...), sig.Results(), sig.Variadic())}
}
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/goplus/gox"
"github.com/goplus/gox/typesutil"
)

func initGopBuiltin(big *gox.PkgRef, conf *gox.Config) {
Expand Down Expand Up @@ -1047,7 +1048,7 @@ func main() {
func TestUntypedBigIntToInterface(t *testing.T) {
pkg := newGopMainPackage()
methods := []*types.Func{
types.NewFunc(token.NoPos, pkg.Types, "Int64", types.NewSignatureType(nil, nil, nil, nil,
types.NewFunc(token.NoPos, pkg.Types, "Int64", typesutil.NewSignatureType(nil, nil, nil, nil,
types.NewTuple(types.NewVar(token.NoPos, nil, "v", types.Typ[types.Int64])), false)),
}
tyInterf := types.NewInterfaceType(methods, nil).Complete()
Expand Down
Loading

0 comments on commit e16c3dc

Please sign in to comment.