Skip to content

Commit

Permalink
Merge remote-tracking branch 'gop/main' into q
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Apr 12, 2024
2 parents e1ef219 + 25b53c7 commit 06a9b23
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 17 deletions.
74 changes: 74 additions & 0 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,80 @@ func TestClassNameAndExt(t *testing.T) {
}
}

func TestFileClassType(t *testing.T) {
type testData struct {
isClass bool
isNormalGox bool
isProj bool
fileName string
classType string
isTest bool
found bool
}
tests := []*testData{
{false, false, false, "abc.gop", "", false, false},
{false, false, false, "abc_test.gop", "", true, false},

{true, true, false, "abc.gox", "abc", false, true},
{true, true, false, "Abc.gox", "Abc", false, true},
{true, true, false, "abc_demo.gox", "abc", false, true},
{true, true, false, "Abc_demo.gox", "Abc", false, true},

{true, true, false, "main.gox", "_main", false, true},
{true, true, false, "main_demo.gox", "_main", false, true},
{true, true, false, "abc_xtest.gox", "abc", false, true},
{true, true, false, "main_xtest.gox", "_main", false, true},

{true, true, false, "abc_test.gox", "case_abc", true, true},
{true, true, false, "Abc_test.gox", "caseAbc", true, true},
{true, true, false, "main_test.gox", "case_main", true, true},

{true, false, false, "get.yap", "get", false, true},
{true, false, false, "get_p_#id.yap", "get_p_id", false, true},
{true, false, true, "main.yap", "AppV2", false, true},

{true, false, false, "abc_yap.gox", "abc", false, true},
{true, false, false, "Abc_yap.gox", "Abc", false, true},
{true, false, true, "main_yap.gox", "App", false, true},

{true, false, false, "abc_ytest.gox", "case_abc", true, true},
{true, false, false, "Abc_ytest.gox", "caseAbc", true, true},
{true, false, true, "main_ytest.gox", "App", true, true},
}
lookupClass := func(ext string) (c *Project, ok bool) {
switch ext {
case ".yap":
return &modfile.Project{
Ext: ".yap", Class: "AppV2",
Works: []*modfile.Class{{Ext: ".yap", Class: "Handler"}},
PkgPaths: []string{"github.com/goplus/yap"}}, true
case "_yap.gox":
return &modfile.Project{
Ext: "_yap.gox", Class: "App",
PkgPaths: []string{"github.com/goplus/yap"}}, true
case "_ytest.gox":
return &modfile.Project{
Ext: "_ytest.gox", Class: "App",
Works: []*modfile.Class{{Ext: "_ytest.gox", Class: "Case"}},
PkgPaths: []string{"github.com/goplus/yap/ytest", "testing"}}, true
}
return
}
for _, test := range tests {
f := &ast.File{IsClass: test.isClass, IsNormalGox: test.isNormalGox, IsProj: test.isProj}
classType, isTest, found := GetFileClassType(f, test.fileName, lookupClass)
if found != test.found {
t.Fatalf("%v found classType want %v, got %v.", test.fileName, test.found, found)
}
if isTest != test.isTest {
t.Fatalf("%v check classType isTest want %v, got %v.", test.fileName, test.isTest, isTest)
}
if classType != test.classType {
t.Fatalf("%v getClassType want %v, got %v.", test.fileName, test.classType, classType)
}
}
}

func TestErrMultiStarRecv(t *testing.T) {
defer func() {
if e := recover(); e == nil {
Expand Down
28 changes: 28 additions & 0 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ func ClassNameAndExt(file string) (name, clsfile, ext string) {
return
}

// GetFileClassType get ast.File classType
func GetFileClassType(file *ast.File, filename string, lookupClass func(ext string) (c *Project, ok bool)) (classType string, isTest bool, ok bool) {
if file.IsClass {
var ext string
classType, _, ext = ClassNameAndExt(filename)
ok = true
if file.IsNormalGox {
isTest = strings.HasSuffix(ext, "_test.gox")
if !isTest && classType == "main" {
classType = "_main"
}
} else {
isTest = strings.HasSuffix(ext, "test.gox")
}
if file.IsProj {
if gt, ok := lookupClass(ext); ok {
classType = gt.Class
}
} else if isTest {
classType = casePrefix + testNameSuffix(classType)
}
} else if strings.HasSuffix(filename, "_test.gop") {
isTest = true
}
return
}

func isGoxTestFile(ext string) bool {
return strings.HasSuffix(ext, "test.gox")
}
Expand Down Expand Up @@ -427,6 +454,7 @@ func astFnClassfname(c *gmxClass) *ast.FuncDecl {
},
},
},
Shadow: true,
}
}

Expand Down
20 changes: 17 additions & 3 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gogen.Packag
if conf.Recorder != nil {
rec = newRecorder(conf.Recorder)
confGox.Recorder = rec
defer func() {
rec.Complete(p.Types.Scope())
}()
}
if enableRecover {
defer func() {
Expand Down Expand Up @@ -1080,6 +1083,9 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
log.Panicln("TODO - cl.preloadFile OverloadFuncDecl: invalid recv")
}
recv = otyp
if ctx.rec != nil {
ctx.rec.Refer(recv, recv.Name)
}
}
onames := make([]string, 0, 4)
exov := false
Expand All @@ -1091,20 +1097,27 @@ func preloadFile(p *gogen.Package, ctx *blockCtx, f *ast.File, goFile string, ge
onames = append(onames, expr.Name)
ctx.lbinames = append(ctx.lbinames, expr.Name)
exov = true
if ctx.rec != nil {
ctx.rec.Refer(expr, expr.Name)
}
case *ast.SelectorExpr:
checkOverloadMethod(d)
checkOverloadMethodRecvType(recv, expr.X)
rtyp := checkOverloadMethodRecvType(recv, expr.X)
onames = append(onames, "."+expr.Sel.Name)
ctx.lbinames = append(ctx.lbinames, recv)
exov = true
if ctx.rec != nil {
ctx.rec.Refer(rtyp, rtyp.Name)
ctx.rec.Refer(expr.Sel, rtyp.Name+"."+expr.Sel.Name)
}
case *ast.FuncLit:
checkOverloadFunc(d)
name1 := overloadFuncName(name.Name, idx)
onames = append(onames, "") // const Gopo_xxx = "xxxInt,,xxxFloat"
ctx.lbinames = append(ctx.lbinames, name1)
preloadFuncDecl(&ast.FuncDecl{
Doc: d.Doc,
Name: &ast.Ident{NamePos: name.NamePos, Name: name1},
Name: &ast.Ident{NamePos: expr.Pos(), Name: name1},
Type: expr.Type,
Body: expr.Body,
})
Expand Down Expand Up @@ -1146,12 +1159,13 @@ func checkOverloadMethod(d *ast.OverloadFuncDecl) {
}
}

func checkOverloadMethodRecvType(ot *ast.Ident, recv ast.Expr) {
func checkOverloadMethodRecvType(ot *ast.Ident, recv ast.Expr) *ast.Ident {
rtyp, _ := getRecvType(recv)
rt, ok := rtyp.(*ast.Ident)
if !ok || ot.Name != rt.Name {
log.Panicln("TODO - checkOverloadMethodRecvType:", recv)
}
return rt
}

const (
Expand Down
42 changes: 40 additions & 2 deletions cl/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cl

import (
"go/types"
"strings"

"github.com/goplus/gogen"
"github.com/goplus/gop/ast"
Expand All @@ -28,12 +29,49 @@ import (

type goxRecorder struct {
Recorder
types map[ast.Expr]types.TypeAndValue
types map[ast.Expr]types.TypeAndValue
refers map[string][]*ast.Ident
}

func newRecorder(rec Recorder) *goxRecorder {
types := make(map[ast.Expr]types.TypeAndValue)
return &goxRecorder{rec, types}
refers := make(map[string][]*ast.Ident)
return &goxRecorder{rec, types, refers}
}

// Refer maps identifiers to name for ast.OverloadFuncDecl.
func (p *goxRecorder) Refer(ident *ast.Ident, name string) {
p.refers[name] = append(p.refers[name], ident)
}

// Complete computes the types record.
func (p *goxRecorder) Complete(scope *types.Scope) {
for name, idents := range p.refers {
pos := strings.Index(name, ".")
if pos == -1 {
if obj := scope.Lookup(name); obj != nil {
for _, id := range idents {
p.Use(id, obj)
}
}
continue
}
if obj := scope.Lookup(name[:pos]); obj != nil {
if named, ok := obj.Type().(*types.Named); ok {
n := named.NumMethods()
for i := 0; i < n; i++ {
if m := named.Method(i); m.Name() == name[pos+1:] {
for _, id := range idents {
p.Use(id, m)
}
break
}
}
}
}
}
p.types = nil
p.refers = nil
}

// Member maps identifiers to the objects they denote.
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.25
github.com/goplus/gogen v1.15.2-0.20240401172158-4586769ba6d4
github.com/goplus/mod v0.13.9
github.com/qiniu/x v1.13.9
github.com/goplus/gogen v1.15.2
github.com/goplus/mod v0.13.10
github.com/qiniu/x v1.13.10
golang.org/x/tools v0.19.0
)

require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)

Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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.25 h1:QvQfOwGVGKFYOTLry8i4p9U55jnIOQWeLsnSyFg0hhc=
github.com/goplus/c2go v0.7.25/go.mod h1:e9oe4jDVhGFMJLEGmPSrVkLuXbLZAEmAu0/uD6fSz5E=
github.com/goplus/gogen v1.15.2-0.20240401172158-4586769ba6d4 h1:u4aI5iMvIEBHg3Ity6LtqM2JZeHecMtINqXyi563YHo=
github.com/goplus/gogen v1.15.2-0.20240401172158-4586769ba6d4/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/mod v0.13.9 h1:B9zZoHi2AzMltTSOFqZNVjqGlSMlhhNTWwEzVqhTQzg=
github.com/goplus/mod v0.13.9/go.mod h1:MibsLSftGmxaQq78YzUzNviyFwB9RtpMaoscufvEKH4=
github.com/goplus/gogen v1.15.2 h1:Q6XaSx/Zi5tWnjfAziYsQI6Jv6MgODRpFtOYqNkiiqM=
github.com/goplus/gogen v1.15.2/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand All @@ -18,8 +18,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qiniu/x v1.13.9 h1:OUcQZSze1oTO5pzrhsepTUvEb9K9WtOiqZomWffFGWE=
github.com/qiniu/x v1.13.9/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
github.com/qiniu/x v1.13.10/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -30,8 +30,8 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
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.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.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=
Expand Down
7 changes: 7 additions & 0 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,10 @@ var (
)

// -----------------------------------------------------------------------------

// GetFileClassType get gop module file classType.
func GetFileClassType(mod *gopmod.Module, file *ast.File, filename string) (classType string, isTest bool, ok bool) {
return cl.GetFileClassType(file, filename, mod.LookupClass)
}

// -----------------------------------------------------------------------------
6 changes: 6 additions & 0 deletions printer/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func TestFuncs(t *testing.T) {
Name: &ast.Ident{Name: "bar"},
Body: &ast.BlockStmt{},
},
&ast.FuncDecl{
Type: &ast.FuncType{Params: &ast.FieldList{}},
Name: &ast.Ident{Name: "Classname"},
Body: &ast.BlockStmt{},
Shadow: true,
},
},
NoPkgDecl: true,
}); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,10 @@ func declToken(decl ast.Decl) (tok token.Token) {
func (p *printer) declList(list []ast.Decl) {
tok := token.ILLEGAL
for _, d := range list {
// skip no entry shadow
if decl, ok := d.(*ast.FuncDecl); ok && decl.Shadow && decl != p.shadowEntry {
continue
}
prev := tok
tok = declToken(d)
// If the declaration token changed (e.g., from CONST to TYPE)
Expand Down
Loading

0 comments on commit 06a9b23

Please sign in to comment.