Skip to content

Commit

Permalink
classfile: allow a work class to specify its project class
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 2, 2025
1 parent 9988985 commit 97ececd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
17 changes: 11 additions & 6 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ type gmxClass struct {
proj *gmxProject
}

type spxObj struct {
obj gogen.Ref
proj string
}

type gmxProject struct {
gameClass string // <gmtype>.gmx
game gogen.Ref // Game (project base class)
sprite map[string]gogen.Ref // .spx => Sprite
sptypes []string // <sptype>.spx
gameClass string // <gmtype>.gmx
game gogen.Ref // Game (project base class)
sprite map[string]spxObj // .spx => Sprite
sptypes []string // <sptype>.spx
scheds []string
schedStmts []goast.Stmt // nil or len(scheds) == 2 (delayload)
pkgImps []gogen.PkgRef
Expand Down Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 97ececd

Please sign in to comment.