Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeAsParamsFunc/Method #1706

Merged
merged 6 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ func initGopPkg(ctx *pkgCtx, pkg *gox.Package, gopSyms map[string]bool) {
ctx.loadType(lbi.(*ast.Ident).Name)
}
}
if pkg.Types.Scope().Lookup(gopPackage) == nil {
pkg.Types.Scope().Insert(types.NewConst(token.NoPos, pkg.Types, gopPackage, types.Typ[types.UntypedBool], constant.MakeBool(true)))
if scope := pkg.Types.Scope(); scope.Lookup(gopPackage) == nil {
scope.Insert(types.NewConst(token.NoPos, pkg.Types, gopPackage, types.Typ[types.UntypedBool], constant.MakeBool(true)))
}
gox.InitThisGopPkg(pkg.Types)
}
Expand Down Expand Up @@ -933,11 +933,11 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile
if debugLoad {
log.Println("==> Preload func", fname)
}
initLoader(parent, syms, name.Pos(), fname, fn, genFnBody) /* {
if strings.HasSuffix(fname, "Gopx_") { // Gopx_xxx func
if initLoader(parent, syms, name.Pos(), fname, fn, genFnBody) {
if strings.HasPrefix(fname, "Gopx_") { // Gopx_xxx func
ctx.lbinames = append(ctx.lbinames, fname)
}
} */
}
}
} else {
if name, ok := getRecvTypeName(parent, d.Recv, true); ok {
Expand Down
95 changes: 95 additions & 0 deletions cl/compile_gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,46 @@ import (
"testing"
)

func TestVargCommand(t *testing.T) {
gopClTest(t, `
type foo int

func (f foo) Ls(args ...string) {
}

var f foo
f.ls
`, `package main

type foo int

func (f foo) Ls(args ...string) {
}

var f foo

func main() {
f.Ls()
}
`)
}

func TestCommandInPkg(t *testing.T) {
gopClTest(t, `
func Ls(args ...string) {
}

ls
`, `package main

func Ls(args ...string) {
}
func main() {
Ls()
}
`)
}

func TestFuncAlias(t *testing.T) {
gopClTest(t, `
func Foo(a ...int) {}
Expand Down Expand Up @@ -1042,6 +1082,61 @@ func main() {
`, false)
}

func TestTypeAsParamsFunc(t *testing.T) {
gopMixedClTest(t, "main", `
package main

import (
"fmt"
"reflect"
)

type basetype interface {
int | string
}

func Gopx_Row__0[T basetype](name string) {
}

func Gopx_Row__1[Array any](v int) {
}

func Gopx_Col[T any](name string) {
fmt.Printf("%v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name)
}

type Table struct {
}

func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
}

func Gopt_Table_Gopx_Col__1[Array any](p *Table, v int) {
}
`, `
var tbl *Table

col string, "name"
col int, "age"

row string, 100

tbl.col string, "foo"
tbl.col int, 100
`, `package main

var tbl *Table

func main() {
Gopx_Col[string]("name")
Gopx_Col[int]("age")
Gopx_Row__1[string](100)
Gopt_Table_Gopx_Col__0[string](tbl, "foo")
Gopt_Table_Gopx_Col__1[int](tbl, 100)
}
`)
}

func Test_RangeExpressionIf_Issue1243(t *testing.T) {
gopClTest(t, `
for i <- :10, i%3 == 0 {
Expand Down
34 changes: 0 additions & 34 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,6 @@ func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, e
}
}

func TestVargCommand(t *testing.T) {
gopClTest(t, `
type foo int

func (f foo) Ls(args ...string) {
}

var f foo
f.ls
`, `package main

type foo int

func (f foo) Ls(args ...string) {
}

var f foo

func main() {
f.Ls()
}
`)
}

func _TestCommandInPkg(t *testing.T) {
gopClTest(t, `
func Ls(args ...string) {
}

ls
`, `
`)
}

func Test_CastSlice_Issue1240(t *testing.T) {
gopClTest(t, `
type fvec []float64
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.21
github.com/goplus/gox v1.14.2
github.com/goplus/gox v1.14.7
github.com/goplus/mod v0.13.0
github.com/qiniu/x v1.13.3
golang.org/x/tools v0.17.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyT
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/goplus/c2go v0.7.21 h1:bRWda6KtQdY0ph+tnPEOGzZMq3OUkAI9cJBzrr5N6AM=
github.com/goplus/c2go v0.7.21/go.mod h1:45zjbWGW8NDuGfTOrFchCIpH9TbiH7TPg8/zF6EdEyk=
github.com/goplus/gox v1.14.2 h1:sc2W+R3D6j1EZ6G+/KWMUDSu3m6dcscZCvnTH2M4XG4=
github.com/goplus/gox v1.14.2/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/gox v1.14.7 h1:C70XxjEVRjrmkblNkbM5oSZOJTOgimsXRtK6/XVlk5s=
github.com/goplus/gox v1.14.7/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/mod v0.13.0 h1:M/VsI/NWTcs2+vtWBG7793zmlCimKlSMY5LXaiztXV8=
github.com/goplus/mod v0.13.0/go.mod h1:Qin2SxKoK+3cNFwtg4Jnk1F9Qn5vR6Kwlvr6lTCRJNI=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
10 changes: 10 additions & 0 deletions testdata/typeasparamsfunc/col.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
"reflect"
)

func Gopx_Col[T any](name string) {
fmt.Printf("%v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name)
}
2 changes: 2 additions & 0 deletions testdata/typeasparamsfunc/typeAsParamsFunc.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
col string, "name"
col int, "age"
21 changes: 21 additions & 0 deletions testdata/typeasparamsmethod/col.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"reflect"
)

type basetype interface {
int | string
}

type Table struct {
}

func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
fmt.Printf("Gopt_Table_Gopx_Col__0 %v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name)
}

func Gopt_Table_Gopx_Col__1[Array any](p *Table, name string) {
fmt.Printf("Gopt_Table_Gopx_Col__1 %v: %s\n", reflect.TypeOf((*Array)(nil)).Elem(), name)
}
4 changes: 4 additions & 0 deletions testdata/typeasparamsmethod/typeAsParamsMethod.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var tbl Table

tbl.col int, "age"
tbl.col bool, "male"