diff --git a/cl/classfile.go b/cl/classfile.go index 6f09e2379..e0c7579c8 100644 --- a/cl/classfile.go +++ b/cl/classfile.go @@ -22,6 +22,7 @@ import ( "go/types" "log" "path/filepath" + "strconv" "strings" "github.com/goplus/gogen" @@ -338,4 +339,42 @@ func makeMainSig(recv *types.Var, f *types.Func) *types.Signature { return types.NewSignatureType(recv, nil, nil, types.NewTuple(params...), nil, false) } +func astFnClassfname(c *gmxClass) *ast.FuncDecl { + return &ast.FuncDecl{ + Name: &ast.Ident{ + Name: "Classfname", + }, + Type: &ast.FuncType{ + Params: &ast.FieldList{}, + Results: &ast.FieldList{ + List: []*ast.Field{ + {Type: &ast.Ident{Name: "string"}}, + }, + }, + }, + Body: &ast.BlockStmt{ + List: []ast.Stmt{ + &ast.ReturnStmt{ + Results: []ast.Expr{ + &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(c.clsfile)}, + }, + }, + }, + }, + } +} + +func astEmptyFunc(entry string) *ast.FuncDecl { + return &ast.FuncDecl{ + Name: &ast.Ident{ + Name: entry, + }, + Type: &ast.FuncType{ + Params: &ast.FieldList{}, + }, + Body: &ast.BlockStmt{}, + Shadow: true, + } +} + // ----------------------------------------------------------------------------- diff --git a/cl/compile.go b/cl/compile.go index 5359e50c2..c80bfdea4 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -854,28 +854,7 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c }}} // func Classfname() string if spxClass { - f.Decls = append(f.Decls, &ast.FuncDecl{ - Name: &ast.Ident{ - Name: "Classfname", - }, - Type: &ast.FuncType{ - Params: &ast.FieldList{}, - Results: &ast.FieldList{ - List: []*ast.Field{ - {Type: &ast.Ident{Name: "string"}}, - }, - }, - }, - Body: &ast.BlockStmt{ - List: []ast.Stmt{ - &ast.ReturnStmt{ - Results: []ast.Expr{ - &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(c.clsfile)}, - }, - }, - }, - }, - }) + f.Decls = append(f.Decls, astFnClassfname(c)) } } if d := f.ShadowEntry; d != nil { @@ -892,16 +871,7 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c } } if !hasEntry { - f.Decls = append(f.Decls, &ast.FuncDecl{ - Name: &ast.Ident{ - Name: entry, - }, - Type: &ast.FuncType{ - Params: &ast.FieldList{}, - }, - Body: &ast.BlockStmt{}, - Shadow: true, - }) + f.Decls = append(f.Decls, astEmptyFunc(entry)) } } preloadFile(p, ctx, f, goFile, !conf.Outline)