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

classfile support auto-generated clone #2178

Merged
merged 9 commits into from
Mar 14, 2025
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
7 changes: 7 additions & 0 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func getGoxConf() *gogen.Config {
return &gogen.Config{Fset: fset, Importer: imp}
}

func TestLoadExpr(t *testing.T) {
var ni nodeInterp
if v := ni.LoadExpr(&ast.Ident{Name: "x"}); v != "" {
t.Fatal("LoadExpr:", v)
}
}

func TestSimplifyPkgPath(t *testing.T) {
if simplifyPkgPath("c/lua") != "github.com/goplus/llgo/c/lua" {
t.Fatal("simplifyPkgPath: c/lua")
Expand Down
77 changes: 75 additions & 2 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ type spxObj struct {
}

type gmxProject struct {
gameClass string // <gmtype>.gmx
game gogen.Ref // Game (project base class)
gameClass string // <gmtype>.gmx
game gogen.Ref // Game (project base class)
spfeats spriteFeat
sprite map[string]spxObj // .spx => Sprite
sptypes []string // <sptype>.spx
scheds []string
Expand All @@ -62,6 +63,43 @@ type gmxProject struct {
hasMain_ bool
}

type spriteFeat uint

const (
spriteClassfname spriteFeat = 1 << iota
spriteClassclone
)

func spriteFeatures(game gogen.Ref) (feats spriteFeat) {
if mainFn := findMethod(game, "Main"); mainFn != nil {
sig := mainFn.Type().(*types.Signature)
if t, ok := gogen.CheckSigFuncEx(sig); ok {
if t, ok := t.(*gogen.TyTemplateRecvMethod); ok {
sig = t.Func.Type().(*types.Signature)
}
}
if sig.Variadic() {
in := sig.Params()
last := in.At(in.Len() - 1)
elt := last.Type().(*types.Slice).Elem()
if tn, ok := elt.(*types.Named); ok {
elt = tn.Underlying()
}
if intf, ok := elt.(*types.Interface); ok {
for i, n := 0, intf.NumMethods(); i < n; i++ {
switch intf.Method(i).Name() {
case "Classfname":
feats |= spriteClassfname
case "Classclone":
feats |= spriteClassclone
}
}
}
}
}
return
}

func (p *gmxProject) hasMain() bool {
if !p.hasMain_ {
imps := p.pkgImps
Expand Down Expand Up @@ -165,6 +203,7 @@ func loadClass(ctx *pkgCtx, pkg *gogen.Package, file string, f *ast.File, conf *
spx := p.pkgImps[0]
if gt.Class != "" {
p.game, p.gameIsPtr = spxRef(spx, gt.Class)
p.spfeats = spriteFeatures(p.game)
}
p.sprite = make(map[string]spxObj)
for _, v := range gt.Works {
Expand Down Expand Up @@ -460,6 +499,40 @@ func astFnClassfname(c *gmxClass) *ast.FuncDecl {
}
}

func astFnClassclone() *ast.FuncDecl {
ret := &ast.Ident{Name: "_gop_ret"}
return &ast.FuncDecl{
Name: &ast.Ident{
Name: "Classclone",
},
Type: &ast.FuncType{
Params: &ast.FieldList{},
Results: &ast.FieldList{
List: []*ast.Field{
{Type: &ast.Ident{Name: "any"}},
},
},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.AssignStmt{
Lhs: []ast.Expr{ret},
Tok: token.DEFINE,
Rhs: []ast.Expr{
&ast.StarExpr{X: &ast.Ident{Name: "this"}},
},
},
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.UnaryExpr{Op: token.AND, X: ret},
},
},
},
},
Shadow: true,
}
}

func astEmptyEntrypoint(f *ast.File) {
var entry = getEntrypoint(f)
var hasEntry bool
Expand Down
18 changes: 15 additions & 3 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func (p *nodeInterp) Caller(node ast.Node) string {

func (p *nodeInterp) LoadExpr(node ast.Node) string {
start := node.Pos()
if start == token.NoPos {
return ""
}
pos := p.fset.Position(start)
f := p.files[pos.Filename]
n := int(node.End() - start)
Expand Down Expand Up @@ -725,6 +728,8 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
var baseType types.Type
var spxProj string
var spxClass bool
var spxClassfname bool
var spxClassclone bool
var goxTestFile bool
var parent = ctx.pkgCtx
if f.IsClass {
Expand Down Expand Up @@ -760,7 +765,10 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
sp := proj.sprite[c.ext]
o := sp.obj
ctx.baseClass = o
baseTypeName, baseType, spxProj, spxClass = o.Name(), o.Type(), sp.proj, true
baseTypeName, baseType, spxProj = o.Name(), o.Type(), sp.proj
spxClassfname = (proj.spfeats & spriteClassfname) != 0
spxClassclone = (proj.spfeats & spriteClassclone) != 0
spxClass = true
}
}
}
Expand Down Expand Up @@ -864,9 +872,13 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
},
}}}
// func Classfname() string
if spxClass {
if spxClassfname {
f.Decls = append(f.Decls, astFnClassfname(c))
}
// func Classclone() any
if spxClassclone {
f.Decls = append(f.Decls, astFnClassclone())
}
}

if d := f.ShadowEntry; d != nil {
Expand Down Expand Up @@ -1298,7 +1310,7 @@ func loadFunc(ctx *blockCtx, recv *types.Var, name string, d *ast.FuncDecl, genB
var pkg = ctx.pkg
var sigBase *types.Signature
if d.Shadow {
if recv != nil {
if recv != nil && (name == "Main" || name == "MainEntry") {
if base := ctx.baseClass; base != nil {
if f := findMethod(base, name); f != nil {
sigBase = makeMainSig(recv, f)
Expand Down
Loading