From 97ececdbd853e067601acc8ab84b9f9720f291c3 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 2 Jan 2025 11:12:14 +0800 Subject: [PATCH] classfile: allow a work class to specify its project class --- cl/classfile.go | 17 +++++++++++------ cl/compile.go | 25 ++++++++++++++++--------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/cl/classfile.go b/cl/classfile.go index 0356b363b..073f059cc 100644 --- a/cl/classfile.go +++ b/cl/classfile.go @@ -41,11 +41,16 @@ type gmxClass struct { proj *gmxProject } +type spxObj struct { + obj gogen.Ref + proj string +} + type gmxProject struct { - gameClass string // .gmx - game gogen.Ref // Game (project base class) - sprite map[string]gogen.Ref // .spx => Sprite - sptypes []string // .spx + gameClass string // .gmx + game gogen.Ref // Game (project base class) + sprite map[string]spxObj // .spx => Sprite + sptypes []string // .spx scheds []string schedStmts []goast.Stmt // nil or len(scheds) == 2 (delayload) pkgImps []gogen.PkgRef @@ -161,10 +166,10 @@ func loadClass(ctx *pkgCtx, pkg *gogen.Package, file string, f *ast.File, conf * if gt.Class != "" { p.game, p.gameIsPtr = spxRef(spx, gt.Class) } - p.sprite = make(map[string]types.Object) + p.sprite = make(map[string]spxObj) for _, v := range gt.Works { obj, _ := spxRef(spx, v.Class) - p.sprite[v.Ext] = obj + p.sprite[v.Ext] = spxObj{obj, v.Project} } if x := getStringConst(spx, "Gop_sched"); x != "" { p.scheds, p.hasScheds = strings.SplitN(x, ",", 2), true diff --git a/cl/compile.go b/cl/compile.go index 377988999..afe723cc0 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -716,6 +716,7 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c var testType string var baseTypeName string var baseType types.Type + var spxProj string var spxClass bool var goxTestFile bool var parent = ctx.pkgCtx @@ -745,9 +746,10 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c baseType = types.NewPointer(baseType) } } else { - o := proj.sprite[c.ext] + sp := proj.sprite[c.ext] + o := sp.obj ctx.baseClass = o - baseTypeName, baseType, spxClass = o.Name(), o.Type(), true + baseTypeName, baseType, spxProj, spxClass = o.Name(), o.Type(), sp.proj, true } } } @@ -790,13 +792,18 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c tags = append(tags, "") chk.chkRedecl(ctx, baseTypeName, pos) } - if spxClass && proj.gameClass != "" { - typ := toType(ctx, &ast.StarExpr{X: &ast.Ident{Name: proj.gameClass}}) - name := getTypeName(typ) - if !chk.chkRedecl(ctx, name, pos) { - fld := types.NewField(pos, pkg, name, typ, true) - flds = append(flds, fld) - tags = append(tags, "") + if spxClass { + if gameClass := proj.gameClass; gameClass != "" { + if spxProj == "" { // if spxProj is empty, use gameClass + spxProj = gameClass + } + typ := toType(ctx, &ast.StarExpr{X: &ast.Ident{Name: spxProj}}) + name := getTypeName(typ) + if !chk.chkRedecl(ctx, name, pos) { + fld := types.NewField(pos, pkg, name, typ, true) + flds = append(flds, fld) + tags = append(tags, "") + } } } rec := ctx.recorder()